Jump to content

Checking current health?


Recommended Posts

I want a character to become mighty/wimpy depending on health (mighty on low health, wimpy on high health). I tried doing the following:

Spoiler

local function onhealthchange(inst, data, forcesilent)
    if inst.sg:HasStateTag("nomorph") or
        inst:HasTag("playerghost") or
        inst.components.health:IsDead() then
        return
    end

    local silent = inst.sg:HasStateTag("silentmorph") or not inst.entity:IsVisible() or forcesilent

    if inst.strength == "mighty" then
        if inst.components.health.current > 75 then -- Determines when mightyness ends
            if silent and inst.components.health.current < 70 then -- Determines when mightyness starts
                becomemighty(inst, true)
            else
                becomenormal(inst, silent)
            end
        end
    elseif inst.strength == "wimpy" then
        if inst.components.health.current < 115 then -- Determines when wimpyness ends
            if silent and inst.components.health.current > 120 then -- Determines when wimpyness starts
                becomewimpy(inst, true)
            else
                becomenormal(inst, silent)
            end
        end
    elseif inst.components.health.current < 50 then -- log says error at this line: "attempt to compare nil with number"
        becomemighty(inst, silent)
    elseif inst.components.health.current > 120 then
        becomewimpy(inst, silent)
    end

    applymightiness(inst)
end

 

but that gives me a crash upon spawning, I included the error ("attempt to compare nil with number") in the code, commented next to the relevant line. The function gets called and mentioned the following way, in case you're worrying about that:

Spoiler

local function onnewstate(inst)
    if inst._wasnomorph ~= inst.sg:HasStateTag("nomorph") then
        inst._wasnomorph = not inst._wasnomorph
        if not inst._wasnomorph then
            onhealthchange(inst)
        end
    end
end

local function onbecamehuman(inst)
    if inst._wasnomorph == nil then
        inst.strength = "normal"
        inst._wasnomorph = inst.sg:HasStateTag("nomorph")
        inst:ListenForEvent("healthdelta", onhealthchange)
        inst:ListenForEvent("newstate", onnewstate)
        onhealthchange(inst, nil, true)
    end
end

local function onbecameghost(inst)
    if inst._wasnomorph ~= nil then
        inst.strength = "normal"
        inst._wasnomorph = nil
        inst:RemoveEventCallback("healthdelta", onhealthchange)
        inst:RemoveEventCallback("newstate", onnewstate)
    end
end

 

I expected the code to work since the following, using sanity and changing "health" to "sanity" accordingly, does work perfectly:

Spoiler

local function onsanitychange(inst, data, forcesilent)
    if inst.sg:HasStateTag("nomorph") or
        inst:HasTag("playerghost") or
        inst.components.health:IsDead() then
        return
    end

    local silent = inst.sg:HasStateTag("silentmorph") or not inst.entity:IsVisible() or forcesilent

    if inst.strength == "mighty" then
        if inst.components.sanity.current < 115 then -- Determines when mightyness ends
            if silent and inst.components.sanity.current > 120 then -- Determines when mightyness starts
                becomemighty(inst, true)
            else
                becomenormal(inst, silent)
            end
        end
    elseif inst.strength == "wimpy" then
        if inst.components.sanity.current > 75 then -- Determines when wimpyness ends
            if silent and inst.components.sanity.current < 70 then -- Determines when wimpyness starts
                becomewimpy(inst, true)
            else
                becomenormal(inst, silent)
            end
        end
    elseif inst.components.sanity.current > 120 then
        becomemighty(inst, silent)
    elseif inst.components.sanity.current < 50 then
        becomewimpy(inst, silent)
    end

    applymightiness(inst)
end

 

 

Link to comment
Share on other sites

Easy :) In the sanity- and hunger-components the current value is stored in a variable called "current", but in the health-component it's called "currenthealth".

The error tells you that it tries to compare nil with a number, which is a perfect description of the problem: nil < 50
It basically tells you, that your variable is not set, so (barring any typos) checking that the variable is right should be your first step, since there can't be a problem with a written value like 50. LUA may be difficult sometimes, but compared to e.g. C++, its errors are mostly spot-on.

Edited by Ultroman
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...