Jump to content

Damage per enemies hp


Recommended Posts

I want to add my character an ability to deal damage depend on enemies health points. But every time I hit someone an error log says that target value is nil. Could anyone help me with that? I believe it's ridiculously easy when you have list of variables and functions and know how they works. Unfortunately I don't have it and idk where I can even find one. Being unable to know what exactly those vars and funcs doing is super frustrating 

 

inst:ListenForEvent("onattackother", function(inst, data, target)
    local health = target.components.health
    
    if data.target ~= nil then
    
    inst.components.combat.damagemultiplier = 1 + target.components.health * 0.03
    end
    end) 

Link to comment
Share on other sites

On 4/19/2020 at 1:00 PM, InvarkuI said:

I believe it's ridiculously easy when you have list of variables and functions and know how they works. Unfortunately I don't have it and idk where I can even find one. Being unable to know what exactly those vars and funcs doing is super frustrating.

Well, the game isn't completely a black box. The backend is hidden from us, sure, but all the Lua code for behavior and such is coded in Lua and is readily available for you to peruse, in order to learn how things work. You just have to study a bunch. Learn how Lua works. It'll make it much easier for you to understand what's happening. Lua is kind of special compared to other programming languages, so you have to get familiar with it.

The problem at hand is very simple. You're setting up a listener for an event and the anonymous function you've defined for it has 3 parameters: inst, data and target. Go search the game code for PushEvent("onattackother" and see what comes up.

I can save you some time, since combat.lua is the only place it shows up:

self.inst:PushEvent("onattackother", { target = targ, weapon = weapon, projectile = projectile, stimuli = stimuli })

The way this works, is that PushEvent reports that "onattackother" was triggered and sends the affected inst plus some data along with the push. The stuff in the {} actually creates a struct-like object, which just has the variables: "target", "weapon", "projectile" and "stimuli". This is what appears in the "data" parameter of our anonymous function. What you don't see here, is the 3rd parameter you added. The "target" parameter is on the data-object, but you're using it by just saying: target.blabla

You should check if data.target is there and if it is, then you can do something to it.

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
 Share

×
  • Create New...