Jump to content

Custom Aura for Mobs


Recommended Posts

i wanted to add an Aura to Butterflies that gives me Sanity standing close to them and losing it when i'm far from them;

i did both parts, but the problem is that i wanted to add a timer where, if i'm far from any butterflies, first i don't lose any Sanity, then i start losing it after 5 seconds, like i have a countdown to get close to one

This is my code, but it doesn't work, Delta remains 0 even after few seconds, it's like the NightmareMode function doesn't even start

Spoiler

local function sanityfn(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, (15), {"butterfly"})
    local delta = 0
    
    local nightmaremode = function(inst)
        delta = 0 - TUNING.SANITYAURA_HUGE
    end
    
    inst:DoPeriodicTask(0.0, nightmaremode, 5.0)
    
    for k, v in pairs(ents) do
        delta = 0 + TUNING.SANITYAURA_TINY
    end
    return delta
end

Any ideas?

Link to comment
Share on other sites

52 minutes ago, Ezerald said:

i wanted to add an Aura to Butterflies that gives me Sanity standing close to them and losing it when i'm far from them;

i did both parts, but the problem is that i wanted to add a timer where, if i'm far from any butterflies, first i don't lose any Sanity, then i start losing it after 5 seconds, like i have a countdown to get close to one

This is my code, but it doesn't work, Delta remains 0 even after few seconds, it's like the NightmareMode function doesn't even start

  Reveal hidden contents

local function sanityfn(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, (15), {"butterfly"})
    local delta = 0
    
    local nightmaremode = function(inst)
        delta = 0 - TUNING.SANITYAURA_HUGE
    end
    
    inst:DoPeriodicTask(0.0, nightmaremode, 5.0)
    
    for k, v in pairs(ents) do
        delta = 0 + TUNING.SANITYAURA_TINY
    end
    return delta
end

Any ideas?

local function nightmaremode(inst)
    -- Turn on penalty mode
    inst.PREFIX_nightmaremode = true
end
local function sanityfn(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, 15, {"butterfly"})
    local entscount = #ents -- The number of butterflies returned in ents
    local delta
    if entscount > 0
    then
        -- If at least one butterfly exists, then delta is positive and multiplicative by the number of butterflies
        delta = TUNING.SANITYAURA_TINY * entscount
        -- If the timer exists, then cancel it and clear it
        if inst.PREFIX_nightmaretimer ~= nil
        then
            inst.PREFIX_nightmaretimer:Cancel()
            inst.PREFIX_nightmaretimer = nil
        end
        -- Clear penalty mode
        inst.PREFIX_nightmaremode = nil
    else
        if inst.PREFIX_nightmaremode
        then
            -- Penalty mode is on, suffer sanity loss
            delta = -TUNING.SANITYAURA_HUGE
        elseif inst.PREFIX_nightmaretimer == nil
        then
            -- Penalty mode is off and there is no timer, start timer
            inst.PREFIX_nightmaretimer = inst:DoTaskInTime(5.0, nightmaremode)
        end
    end
    return delta
end

I've annotated it, but this should be the gist of it.

Replace "PREFIX" with something unique to your mod as a form of a namespace on the player entity, or go another step by using a custom-named table to store all of your variables on for the player entity.  This is done for higher mod-mod compatibility.

Link to comment
Share on other sites

1 hour ago, CarlZalph said:

local function nightmaremode(inst)
    -- Turn on penalty mode
    inst.PREFIX_nightmaremode = true
end
local function sanityfn(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, 15, {"butterfly"})
    local entscount = #ents -- The number of butterflies returned in ents
    local delta
    if entscount > 0
    then
        -- If at least one butterfly exists, then delta is positive and multiplicative by the number of butterflies
        delta = TUNING.SANITYAURA_TINY * entscount
        -- If the timer exists, then cancel it and clear it
        if inst.PREFIX_nightmaretimer ~= nil
        then
            inst.PREFIX_nightmaretimer:Cancel()
            inst.PREFIX_nightmaretimer = nil
        end
        -- Clear penalty mode
        inst.PREFIX_nightmaremode = nil
    else
        if inst.PREFIX_nightmaremode
        then
            -- Penalty mode is on, suffer sanity loss
            delta = -TUNING.SANITYAURA_HUGE
        elseif inst.PREFIX_nightmaretimer == nil
        then
            -- Penalty mode is off and there is no timer, start timer
            inst.PREFIX_nightmaretimer = inst:DoTaskInTime(5.0, nightmaremode)
        end
    end
    return delta
end

I've annotated it, but this should be the gist of it.

Replace "PREFIX" with something unique to your mod as a form of a namespace on the player entity, or go another step by using a custom-named table to store all of your variables on for the player entity.  This is done for higher mod-mod compatibility.

i replaced "PREFIX" with "dream" but the custom-named table you were talking about i'm not really sure how to do it, also, i've implemented your code but it gave me an error:

my character start with a butterfly in the inventory, when i killed it to see if my sanity would start dropping, my game crashed;

the same happened if i dropped the butterfly and got away from it;

it's like the game crash when i'm not near to any butterfly

Screenshot (810).png

Edited by Ezerald
Link to comment
Share on other sites

1 hour ago, Ezerald said:

it's like the game crash when i'm not near to any butterfly

        elseif inst.PREFIX_nightmaretimer == nil
        then
            -- Penalty mode is off and there is no timer, start timer
            inst.PREFIX_nightmaretimer = inst:DoTaskInTime(5.0, nightmaremode)
            delta = 0
        end

 

Link to comment
Share on other sites

17 minutes ago, CarlZalph said:

        elseif inst.PREFIX_nightmaretimer == nil
        then
            -- Penalty mode is off and there is no timer, start timer
            inst.PREFIX_nightmaretimer = inst:DoTaskInTime(5.0, nightmaremode)
            delta = 0
        end

 

i added this part but it keeps giving me the same error, don't know why

OKOKOK, i found the problem

"local delta" was considerated nil so it was giving error, so i fixed setting "delta = 0"

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...