HurtMsg Example

Status
Not open for further replies.

Tamkis

Lua Grasshopper
Just a quick but somewhat dumb question on LUA's HurtMsg Hook: What is the best way to print the message itself for getting damaged by weapons? In a vanilla netgame match, if for example, I (Tamkis) get hit by user RandomDude's RailRing, the message that prints is "Tamkis got hit by RandomDude's Rail Ring". Does the function automatically print the string in the console?

I am trying to edit the message printed when user are hit with some custom weapons. Could I have an example on how to use (and most importantly, print a message) in the same format above using a Rail Ring as an example?

Hook format: addHook("HurtMsg", functionname)
Function format: function(player_t player, mobj_t inflictor, mobj_t source)
Used to create custom hurt messages for players, or to change an existing one. Executes when an object is being damaged during the use of P_DamageMobj. player is the player that was hurt, inflictor is the mobj that hurt the player, and source is the mobj responsible for the inflictor's existence. Note that inflictor and/or source may be nil, and should be checked for existence and validity before being accessed or checked to prevent errors. Returning true will override the message printed to the console by taking damage in multiplayer, and returning false will run alongside it instead.
 
Last edited:
You just wanna use the print function to say whatever you want, then return true to override the default hit message text. One thing to keep in mind, though, is to check and make sure that the player was hit by something, as opposed to some sector or something:

if inflictor

that ring was in fact what hit the player:

if inflictor.type == MT_THROWNFALAFEL

that the ring was fired by something:

if source

and that the something in question is a player:

if source.player

The best way to do that in a few lines is something like this:

Code:
if inflictor and inflictor.type == MT_THROWNFALAFEL and source and source.player then
    if player.mo.health then
        print(("%s's thrown falafel pegged %s."):format(source.player.name, player.name))
    else
        print(("%s's thrown falafel obliterated %s."):format(source.player.name, player.name))
    end
    return true
end
That uses the string metatable's format function to cleanly process the string in question. Quite handy IMO.
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top