Jump to content

Help with a custom mod perk?


Recommended Posts

So I'm trying to make a character mod perk where if the hunger is above a certain amount the character runs faster. Fairly straightforward I would think but I'm having trouble implementing the code. Could someone help me write the code needed to make this work?

Link to comment
Share on other sites

In all honesty I've done this code over eight times these past few months and I guess the other modders are out so I'll say it again....

local function hungerspeed(inst)
    if (inst.components.hunger:GetPercent() > .7)  then --greater than 70% hunger fastest speed
    inst.components.locomotor.walkspeed = 6
    inst.components.locomotor.runspeed = 8.5
    elseif (inst.components.hunger:GetPercent() > .5)  then --greater than 50% hunger fast speed
    inst.components.locomotor.walkspeed = 5
    inst.components.locomotor.runspeed = 7.5
    else
    inst.components.locomotor.walkspeed = 4 --regular speed otherwise
    inst.components.locomotor.runspeed = 6
end
end

 

inst:DoPeriodicTask(2, hungerspeed, nil, inst) --last part

the first function can be put at the very top of the master_postinit(or outside of it), and the last part near the inst.OnLoad = onload.

Edited by K1NGT1GER609
Link to comment
Share on other sites

1 hour ago, K1NGT1GER609 said:

In all honesty I've done this code over eight times these past few months and I guess the other modders are out so I'll say it again....

local function hungerspeed(inst)
    if (inst.components.hunger:GetPercent() > .7)  then --greater than 70% hunger fastest speed
    inst.components.locomotor.walkspeed = 6
    inst.components.locomotor.runspeed = 8.5
    elseif (inst.components.hunger:GetPercent() > .5)  then --greater than 50% hunger fast speed
    inst.components.locomotor.walkspeed = 5
    inst.components.locomotor.runspeed = 7.5
    else
    inst.components.locomotor.walkspeed = 4 --regular speed otherwise
    inst.components.locomotor.runspeed = 6
end
end

 

inst:DoPeriodicTask(2, hungerspeed, nil, inst) --last part

the first function can be put at the very top of the master_postinit(or outside of it), and the last part near the inst.OnLoad = onload.

why not use inst:ListenForEvent("hungerdelta", hungerspeed) instead of DoPeriodicTask?

Link to comment
Share on other sites

1 hour ago, DarkKingBoo said:

why not use inst:ListenForEvent("hungerdelta", hungerspeed) instead of DoPeriodicTask?

 That can also work as well so use either listenforevent or periodictask, as for why I sometimes get annoyed trying to remember certain events and getting the event's names exactly right. If I get the name wrong I spend more time browsing files trying to find the correct name, and last note which is completely unrelated to this forum you get some interesting results with that in don't starve(not together). By interesting I mean you're frozen in place since that event happens every millisecond.

Edited by K1NGT1GER609
Link to comment
Share on other sites

8 hours ago, K1NGT1GER609 said:

 That can also work as well so use either listenforevent or periodictask, as for why I sometimes get annoyed trying to remember certain events and getting the event's names exactly right. If I get the name wrong I spend more time browsing files trying to find the correct name, and last note which is completely unrelated to this forum you get some interesting results with that in don't starve(not together). By interesting I mean you're frozen in place since that event happens every millisecond.

Seems to work fine on Wolfgang (in DS)?

As for DoPeriodicTask, you might want to tie it to a variable so you can cancel it via inst.mytask:Cancel() incase you need to stop it for whatever reason. Iirc theres no other way to stop it (besides despawning). Please correct me on that if I'm wrong though.

 

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