Jump to content

Recommended Posts

I was testing out my character mod "douglas" (my first mod, mainly using it to teach myself how to mod in this game), because I wanted to test out a new function I gave him. Previously, I made it so that whenever his hunger is below 50%, he'll begin losing sanity at one point per second. Here's what the original code for that looks like:

    --Lose sanity when hungry
local function onhungerchange(inst, data, forcesilent)
    if inst.components.hunger.current < 50 then
    inst.components.sanity.dapperness = -1
    elseif inst.components.hunger.current > 50 then
    inst.components.sanity.dapperness = 0
    end
end

Obviously, this isn't in the ModMain, it's in his prefab. And it works just fine! But recently I decided that it might be cool to make it also he also does more damage when at low hunger, so I altered the code into this:

    --Lose sanity when hungry, do more damage
local function onhungerchange(inst, data, forcesilent)
    if inst.components.hunger.current < 50 then
    inst.components.sanity.dapperness = -1
    inst.components.combat.damagemultiplier = 3
    elseif inst.components.hunger.current > 50 then
    inst.components.sanity.dapperness = 0
    inst.components.combat.damagemultiplier = 1
    end
end

I loaded the game up, tested it out by changing his hunger and spawning spiders to punch, and it worked fine! So I disconnected from the server, went to show my friends this cool new thing I did, and bam:

Untitled.thumb.png.b9718b06d66fbb52ec7a4965cf38f44c.png

"Uh oh."

I did some more testing with this, and yes, the mod loads up perfectly fine the first time, but then when I reconnect to the server, it always gives this same error. Which is weird because I haven't touched the ModMain at all, for a long time. I don't know why exactly this is happening now, but here's the rundown of the ModMain.

In it, I have a big long line of AddPrefabPostInit's. One of Douglas's functions is that he loses far more sanity from eating raw meat, but gains double the hunger that other characters would normally get. For example, "smallmeat" now gives 20 hunger instead of 10, and does 15 damage to his sanity. I did this for every single meat in the game, including monster meat. And here we have the problem.

For some reason, after I added his damage multiplier function, the line in his modmain that affects "monstermeat" is broken...but only after I reconnect to the server. Here is what that code looks like:

        AddPrefabPostInit("monstermeat", function(inst)
            if GetPlayer().prefab == "douglas" then
                inst.components.edible.healthvalue = 5
                inst.components.edible.hungervalue = 30
                inst.components.edible.sanityvalue = -15
            end
        end)

Line 106 is "if GetPlayer().prefab == "douglas" then," so apparently Douglas is now a nil value, in this one line of code, while every other line works just fine. Removing the damage multiplier function, somehow, fixes this crash. So the ModMain itself appears to be fine, yet that damage multiplier affects it somehow...I'm completely baffled on this one, lol.

I'm planning on doing my own testing for a while to see if I can figure it out, but this forum hasn't failed me yet, so I figured I'd drop a little S.O.S.

Thanks for reading this long post, and any help would always be welcome and appreciated!

@PuzzlePeace

This is an issue with how DST works versus DS.

When you're in DST land everything about the player entity as you know it is very unsafe to use.

GetPlayer() is now ThePlayer, and only really in reference when as a client doing client side things like UI.  ThePlayer is nil on the server.

You'll also want a check in the AddPrefabPostInit to make sure that the code is running on a mastersim instance.  This is where components (mainly) exist.  The `if not TheWorld.ismastersim then return end` check.  Add this check to all of your event callbacks, too, when you're touching components.

 

For what you want to do, you'll have to either hook the meat's edible's GetHealth/GetHunger/GetSanity functions and edit the edible if the eater's prefab is yours, or hook your character's eater component's Eat function and modify the food's values there based on prefab.

You can learn more about function hooking here:

 

  • Big Ups 1
12 hours ago, CarlZalph said:

@PuzzlePeace

This is an issue with how DST works versus DS.

When you're in DST land everything about the player entity as you know it is very unsafe to use.

GetPlayer() is now ThePlayer, and only really in reference when as a client doing client side things like UI.  ThePlayer is nil on the server.

You'll also want a check in the AddPrefabPostInit to make sure that the code is running on a mastersim instance.  This is where components (mainly) exist.  The `if not TheWorld.ismastersim then return end` check.  Add this check to all of your event callbacks, too, when you're touching components.

 

For what you want to do, you'll have to either hook the meat's edible's GetHealth/GetHunger/GetSanity functions and edit the edible if the eater's prefab is yours, or hook your character's eater component's Eat function and modify the food's values there based on prefab.

You can learn more about function hooking here:

 

Oh wow, I had no idea! I guess while I was hopping back and forth between guides, I must have accidentally used some knowledge from Don't Starve, instead of Don't Starve Together, since they can be pretty similar sometimes. And considering that it worked (for a while, anyway), I just didn't notice!

Thank you so so much for leaving such a detailed reply, and linking me to that guide! This is extremely helpful! :-D

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