Jump to content

Recommended Posts

Hello again all, I am currently in the process of reworking my character Arashi Otter and was wondering if there was a way to call a function into another function in a clean simple manner, I already have a custom sanity function, and want another function to when I reach certain sanity levels gain certain stat's or loose certain stats.

Again any help is greatly appreciated thanks in advance,

-Arashi

@ArashiOtter, without your current code and the location in which the second function is located it's difficult to tell you how to hook it up properly.

 

However, if the function is in the same file as the other function simply use FUNCTIONNAME(FUNCTION, ARGUMENTS, TO, PASS) and it will call it. If you are using a local function then the local function needs to be at the beginning of the file. If it's located in another file then you need to attach it to a component or instances and call it via component:FunctionName() or inst:FunctionName() to pass the self variable automatically or component.FunctionName() or inst.FunctionName() to not pass the self variable automatically. 

local function sanityfn(inst)

local delta = 0

if TheWorld.state.isday then

delta = -0.18

elseif TheWorld.state.isdusk then

delta = 0.5

elseif TheWorld.state.isnight then

delta = 0.95

end

return delta

end

local function sanityeffects(sanityfn, inst)

would that be the proper way to tie in it?
local function sanityfn(inst)    local delta = 0        if TheWorld.state.isday then        delta = -0.18     elseif TheWorld.state.isdusk then        delta = 0.5        elseif TheWorld.state.isnight then        delta = 0.95            end    return deltaendlocal function sanityeffects(sanityfn, inst)
would that be the proper way to tie in it?

 

 

This code doesn't really demonstrate what you are trying to accomplish. However there are some mistakes.

local function sanityfn(inst)    local delta = 0        if TheWorld.state.isday then        delta = -0.18     elseif TheWorld.state.isdusk then        delta = 0.5        elseif TheWorld.state.isnight then        delta = 0.95            end    return deltaendlocal function sanityeffects(inst, sanitydelta)

First inst is the first function parameter always. Second don't name the second parameter the same name as a function, that's bad coding. Name it what it's meant to represent.

 

In order to call this function you could use:

sanityeffects(inst, sanityfn(inst))

or alternatively you can do something like:

sanityeffects(inst)local delta = sanityfn(inst)

By using the second method the only function parameter for sanityeffects needed is inst.

Edited by Kzisor

I'm trying to make it so that based on the output of sanityfn arashi gains looses state, so during the day as he is loosing sanity he looses health or has a lower base health. likewise during the dusk and night he gains increasing health or a slow regen, and increase locmotor speed.

Is there an easier way to write these two function to make it simpler?

Like I've said before I really am not that good at writing script

Edited by ArashiOtter

@ArashiOtter, there is an easier way of accomplishing that.

local function snaityeffects(inst, phase)if phase == "day" then        -- CHANGE THE DAY EFFECTS HERE.    elseif phase = "dusk" then        -- CHANGE THE DUSK EFFECTS HERE       elseif phase == "night then        -- CHANGE THE NIGHT EFFECTS HERE     endendinst:WatchWorldState("phase", sanityeffects)   

This code doesn't require you to have two function, it should do it all in the sanity effects function.

 

Edited by Kzisor

Ok using this script how would I change the sanity rate, I've tried

inst.components.sanity:DoDelta(#)
and have tried
inst.components.sanity:SetRate(#)
SetRateCrashes the game on the second day and DoDelta does nothing.

@ArashiOtter, the reason the second code crashes the game is because the sanity component does not have a SetRate function.

 

You set it by using the following code:

inst.components.sanity.rate = NEW_RATE 

Whenever you are trying to modify a component, you should first look at the component in question and determine how it works. Then you should adjust your code to work with the component the way you want it to be modified.

 

Ok so I have modified the codes for sanity rate

local function sanityeffects(inst, phase)

if phase == "day" then

inst.components.sanity.rate = -0.18

elseif phase == "dusk" then

inst.components.sanity.rate = 0.5

elseif phase == "night" then

inst.components.sanity.rate = 0.95

end

end

but still the game wants to use it's sanity rate. I have your
inst:WatchWorldState("phase", sanityeffects)
in my master_postinit cause it crashes my game if I place it anywhere else.

@ArashiOtter, try replacing rate with dapperness. However, if you are going to do that you will need to adjust your equation to take the current dapperness of the character and reduce it by the amount you want so something like the following;

inst.components.sanity.dapperness = inst.components.sanity.dapperness - AMOUNT_TO_REDUCE 

I believe that dapperness is actually the variable we want to set because rate is being recalculated several times now that I took a closer look.

Ok, so I have been playing with this code most of the day now, and can not get the drain at night to not count. Here is my current sanityeffects function:

local function sanityeffects(inst, phase)    if phase == "day" then        inst.components.sanity.dapperness = inst.components.sanity.dapperness - 0.10        inst.components.combat.damagemultiplier = 0.5    elseif phase == "dusk" then        inst.components.sanity.dapperness = inst.components.sanity.dapperness + 5    elseif phase == "night" then        inst.components.sanity.dapperness = inst.combat.sanity.dapperness + 8    endend

@Kzisor Thank you for the help so far.

If anyone can help me fix this problem I thank you in advance

@ArashiOtter, You will need to keep playing with it in order to determine how much to add. Check out the tuning.lua file and search for SANITY and it should give you an idea on how much it naturally drains in those periods. 

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