Jump to content

Question about the OnWeaponHitCharacter event


cerati

Recommended Posts

Hi! does this mean when you hit a different player, or a hit a zombie? Also, How do I satisfy the Float parameter called damageSplit? Can I just fill in any number with my own variable or do I have to call a specific method that returns the value? I'm defintely new to LUA coding, so thanks for your time replying to this!

Link to comment
Share on other sites

After doing some of my own tests I can confirm that it includes hitting other players and zombies.

 

OnWeaponHitCharacter calls functions added to it when triggered. You must name the 4 parameters in the function you give to the OnWeaponHitCharacter Event.

You can call those parameters any variable name you want in your lua functions.

 

Here's a simple example lua code that prints out the attacker's name, target's name, weapon name, and the damageSplit variable.

Feel free to modify/use this code.

--[[
    Triggered when a "IsoGameCharacter" has been hit by a "HandWeapon".
    https://pzwiki.net/wiki/Modding:Lua_Event/OnWeaponHitCharacter
    
    Parameters:
        IsoGameCharacter attacker
        IsoGameCharacter target
        HandWeapon weapon
        Float damageSplit
]]
local function onWeaponHit(attacker, target, weapon, damageSplit)
    local attackerName = tostring( attacker:getFullName() )
    local targetName = tostring( target:getFullName() )
    local weaponName = tostring( weapon:getName() )
    
    -- Print out the attacker's name, target's name, weapon name, and the damageSplit variable.
    print(string.format("%s | %s | %s | %f", attackerName, targetName, weaponName, damageSplit))

    --print(attackerName.." | "..targetName.." | "..weaponName.." | "..damageSplit)
end

-- Add the function named "onWeaponHit" to the OnWeaponHitCharacter Event.
Events.OnWeaponHitCharacter.Add(onWeaponHit)

 

It prints out this on the Build 40 console. (I am hitting super survivors and zombies with a hammer. Apparently zombies have names.)

image.png.e80c87cf2d31c0f5f821b6db85915df2.png

Edited by ATPHHe
Link to comment
Share on other sites

You’ll want to use getType() to check if it’s an IsoZombie or an IsoPlayer, iirc.  So, attacker.getType() should return a string with the class name that you can compare.
 

IsoGameCharacter is the just patent class these two inherit from, rather than what’s actually passed to the function.

 

Zombies have full player descriptions, but they’re not used for much (if anything) in practice. It’s inherited from IsoGameCharacter.
 

I’ve not touched this stuff in a long time though, personally. I might well have it confused with getClass() or toString(). There should be examples of this in the game code regardless.

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...