Jump to content

Looking for help coding abilities/finding mods with similar abilities


Recommended Posts

This is my first time posting on the forums but I'm working on a character mod and have run into a wall with her abilities. Even though this isn't the first character I've made it's by far the biggest one yet and these three below are giving me problems since I've never really seen them around, in mods or forum posts, just thought I'd ask here and see if anyone has anyone else has. 

The abilities I'm trying to find how to code are:

  • Crafting cost reduced to what other characters would have wearing the construction amulet or just lower than it regularly is, possibly only when sane enough (like say over halfway sane)
  • Either something like the ability to fix burned buildings or an item with that ability to fix say a burnt science machine (here I mostly just want to know if it's possible and which one would be best)
  • Sanity gain from food reduced by half/removed completely

I don't want anyone to have to write code for me, mostly i just want help finding out if they are possible and if so being pointed in the right direction. I've been trying to find characters with similar ones on the workshop but haven't really been able to on my own. For the last one I've been looking into wortox a lot since i know he has something similar and have found how to halv all the effects from food but I am unsure on how it would be done for only sanity. Have any of you made anything similar or know a mod that utilizes these abilities? I'm more than happy to look through implementing/fitting them to my character myself! Thanks a lot!

Link to comment
Share on other sites

The builder component locate in databundles/scripts/scripts/components has exactly the stats you're looking for under 

local Builder = Class(function(self, inst)

 

Just take a look at all the values and you'll find it. Note that every "self" in the component looks like "inst.components.prefab" when you're actually putting it into the character's prefab. E.g "self.science_bonus = VALUE" should be written as "inst.components.builder.science_bonus = VALUE" (And no that's not the thing you're looking for, I didn't want to spoil it :P)

I would say look at the staffs prefab for the green staff's code. You could copy that into the prefab of your own item but instead of deconstruct the object, check to see if it's been burnt and set it to not be burnt/replace it with a fresh version.

As for the hunger, that exact function is also listed in the eater component! Same directory and rules as the builder component. Hope this helps point you in the right direction without being too much! 

Link to comment
Share on other sites

On 6.12.2019 at 3:45 AM, snakeyarts said:
  • Crafting cost reduced to what other characters would have wearing the construction amulet or just lower than it regularly is, possibly only when sane enough (like say over halfway sane)

The builder component has a variable called ingredientmod, which defaults to 1 (100%). If you set this to 0.5, your building cost is reduced to half (or set it to 0.49 to floor halves instead of ceiling them). You can easily keep updating this variable depending on your current sanity, either by listening for "sanitydelta" or making a periodic task which checks every second or so whether the variable should be changed (MUCH less CPU usage than listening to "sanitydelta" which could be called every frame).

On 6.12.2019 at 3:45 AM, snakeyarts said:
  • Either something like the ability to fix burned buildings or an item with that ability to fix say a burnt science machine (here I mostly just want to know if it's possible and which one would be best)

Well, you'd probably have to create your own ACTION in order to interact with destroyed machines. An alternative is to make an AoE effect, which searches for certain entities within a range, records their position and rotation, destroys them, and then replaces them by spawning new versions of them with their position and rotation, potentially costing something per building that was replaced.

On 6.12.2019 at 3:45 AM, snakeyarts said:
  • Sanity gain from food reduced by half/removed completely

Take a look at the chapter "Changing Food Values Based On Foodtype (CHARACTER)" in my post here. It's a bunch of code, but it takes care of a lot of grief. You just have to concentrate on the code between the lines

---------- ONLY EDIT BELOW THIS LINE ----------
and
---------- ONLY EDIT ABOVE THIS LINE ----------.

You can basically just replace it with:

sanityval = 0

if you just want to remove both negative AND positive sanity effects from food. You can do whatever you want to calculate new values for the foods.

Edited by Ultroman
Link to comment
Share on other sites

Thank you for all the great pointers! I got the reduced crafting cost working without a problem and am looking into the other things you recommended! 

7 hours ago, Ultroman said:

Take a look at the chapter "Changing Food Values Based On Foodtype (CHARACTER)" in my post here.

I actually looked into it earlier! It's a really impressive post, I have to admit though it goes over my head slightly on how and where it should be implemented (call it a double language barrier since I'm not a native english speaker and only formally learned a bit of C#). I mostly struggle with the part at the beginning of the tutorial with the different options to go with the code snippets and explain where to put them, correct me if i'm wrong but I went with: 

Applying Changes Specific To Your Own Character Mod

Put the code snippet in the prefab Lua code for your character, at the bottom of the master_postinit function.

and did just that, without changing anything about the "Changing Food Values Based On Foodtype (CHARACTER)" snippet just to test it out and just got a game error reading:

[string "scripts/mainfunctions.lua"]:150: Error loading file prefabs/genny
[string "../mods/Genny/scripts/prefabs/genny.lua"]:99: ')' expected near ','

line 99 being:

local calculateFoodValues(player, food)

I take it I might have went wrong/something should have been changed before it was able to run? Sorry for the inconvenience! I'll attach the entire prefab file down below if you want to check that I didn't put it in the wrong place or messed something up.

genny.lua

Link to comment
Share on other sites

Oh I was hoping to only reduce positive sanity gain for all foods if possible, I maybe didn't make that very clear in the post, sorry about that. I was able to work out reducing sanity gain and loss for all foods from your tip with the eater component though and am keeping it as a plan B just in case!

Link to comment
Share on other sites

11 minutes ago, snakeyarts said:

Oh I was hoping to only reduce positive sanity gain for all foods if possible, I maybe didn't make that very clear in the post, sorry about that. I was able to work out reducing sanity gain and loss for all foods from your tip with the eater component though and am keeping it as a plan B just in case!

Gotcha! That's easy:

In the hunger component you'll find:

 

function Eater:SetAbsorptionModifiers(health, hunger, sanity)
    self.healthabsorption = health
    self.hungerabsorption = hunger
    self.sanityabsorption = sanity
end

 

In Wortox's prefab it looks a bit more like this:

    if inst.components.eater ~= nil then
        inst.components.eater:SetAbsorptionModifiers(TUNING.WORTOX_FOOD_MULT, TUNING.WORTOX_FOOD_MULT, TUNING.WORTOX_FOOD_MULT)
    end

If you look between it and the function from the eater component, you'll see the first value is health, the second is hunger, and the third is sanity. So setting the first two as 1 will just make them default, while less than 1 for the sanity will make it give less.

 

Ultroman's code is a bit more complex to specifically make different kinds of food do different things, since surprisingly we don't seem to have the ability to do it too easily at the moment. 

 

Edited by Mr. Tiddles
Link to comment
Share on other sites

29 minutes ago, Mr. Tiddles said:

If you look between it and the function from the eater component, you'll see the first value is health, the second is hunger, and the third is sanity. So setting the first two as 1 will just make them default, while less than 1 for the sanity will make it give less.

Yea! What I wrote to get it to work with reducing both positive and negative sanity from foods was:

        if inst.components.eater ~= nil then
        --No sanity from food
        inst.components.eater:SetAbsorptionModifiers(1, 1, 0.5)
        end

The setup is largely inspired by Wormwood who I remembered after the initial post only had hunger reduction and then did just that, unless I've somehow done my math wrong while playtesting it does cut both in half, while I was aiming to leave all the sanity she'd lose from any foods alone completely. As I said it is my plan B to just go with that if I can't figure something more specific out. 

33 minutes ago, Mr. Tiddles said:

Ultroman's code is a bit more complex to specifically make different kinds of food do different things, since surprisingly we don't seem to have the ability to do it too easily at the moment.

And oh? if that's the case I must have misunderstood completely from reading through it all. I'd still like to see if Ultroman responds to confirm or deny anything but if it is then I'll just end up going with reducing both. Really though thank you for all the help, I really appreciate it! 

Link to comment
Share on other sites

Just now, snakeyarts said:

Yea! What I wrote to get it to work with reducing both positive and negative sanity from foods was:

        if inst.components.eater ~= nil then
        --No sanity from food
        inst.components.eater:SetAbsorptionModifiers(1, 1, 0.5)
        end

The setup is largely inspired by Wormwood who I remembered after the initial post only had hunger reduction and then did just that, unless I've somehow done my math wrong while playtesting it does cut both in half, while I was aiming to leave all the sanity she'd lose from any foods alone completely. As I said it is my plan B to just go with that if I can't figure something more specific out. 

And oh? if that's the case I must have misunderstood completely from reading through it all. I'd still like to see if Ultroman responds to confirm or deny anything but if it is then I'll just end up going with reducing both. Really though thank you for all the help, I really appreciate it! 

Oh, positives, right. My solution to that would be to listen out for when the character eats something to check for a negative sanity value on the food and then take it off. Which is possible and quite simple! The biggest issue is not just showing the code, since you said you don't just want to be given the answers. Lol.

 

Link to comment
Share on other sites

12 minutes ago, Mr. Tiddles said:

The biggest issue is not just showing the code, since you said you don't just want to be given the answers

Normally I like to figure things out as much as I can on my own and feel bad for making people write out code specifically for me but this entire ability is getting on my nerves because of how lost I am with everything. I'd highly appreciate any flat out code I'm given for this, I just don't want people going out of their way and wasting their time on writing long passages of code when they could be doing something else with their time.

Link to comment
Share on other sites

5 minutes ago, snakeyarts said:

Normally I like to figure things out as much as I can on my own and feel bad for making people write out code specifically for me but this entire ability is getting on my nerves because of how lost I am with everything. I'd highly appreciate any flat out code I'm given for this, I just don't want people going out of their way and wasting their time on writing long passages of code when they could be doing something else with their time.

 

No worries! I'll just be sure to explain as much as I can, then

Firstly I set the modifier to 0 since it's all covered by the function below:

        inst.components.eater:SetAbsorptionModifiers(1, 1, 0)

 

All this stuff is rambling that may not make sense because I'm bad at explaining

Then in the eater component we have this little gem:

function Eater:SetOnEatFn(fn)
    self.oneatfn = fn
end

The (fn) can either be the name of a function elsewhere in the prefab, or actually contain a function is you write it as SetOnEatFn(function(inst, food) THINGS TO DO end)

 

This is what matters:

        inst.components.eater:SetOnEatFn(function(inst, food)
       local delta = food.components.edible.sanityvalue

        if delta > 0 then delta = delta*0.5 end 

    inst.components.sanity:DoDelta(delta)
    end)
    end

The local delta thing is just to make it take up less room. Basically it looks at the sanityvalue, if it's more than 0 it halves it.

All of that just goes in the master_postinit right below the code from Wortox.

 

 

Spoiler

 

    if inst.components.eater ~= nil then
        inst.components.eater:SetAbsorptionModifiers(1, 1, 0)

        inst.components.eater:SetOnEatFn(function(inst, food)
       local delta = food.components.edible.sanityvalue

        if delta > 0 then delta = delta*0.5 end 

    inst.components.sanity:DoDelta(delta)
    end)
    end

 

Edited by Mr. Tiddles
Link to comment
Share on other sites

34 minutes ago, Mr. Tiddles said:

    if inst.components.eater ~= nil then
        inst.components.eater:SetAbsorptionModifiers(1, 1, 0)

        inst.components.eater:SetOnEatFn(function(inst, food)
       local delta = food.components.edible.sanityvalue

        if delta > 0 then delta = delta*0.5 end 

    inst.components.sanity:DoDelta(delta)
    end)
    end

I tried it but it seems to be giving me an error when I try to run the game sadly

error2.thumb.PNG.1899e781c7f1e20b2d0ee077e780d232.PNG

here's the entire master_postinit section, "local master_postinit = function(inst)" being line 67

Spoiler

 

local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(175)
    inst.components.hunger:SetMax(150)
    inst.components.sanity:SetMax(200)
    
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
    inst.components.health.fire_damage_scale = 1
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
        --Sanity rate
    inst.components.sanity.custom_rate_fn = sanityfn
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
    --reduced crafting cost
    inst.components.builder.ingredientmod = 0.5

    --sanity food reduction 
    if inst.components.eater ~= nil then
        inst.components.eater:SetAbsorptionModifiers(1, 1, 0)

        inst.components.eater:SetOnEatFn(function(inst, food)
       local delta = food.components.edible.sanityvalue

        if delta > 0 then delta = delta*0.5 end 

    inst.components.sanity:DoDelta(delta)
    end)
    end
    
return MakePlayerCharacter("genny", prefabs, assets, common_postinit, master_postinit, start_inv)

 

 

Link to comment
Share on other sites

2 minutes ago, snakeyarts said:

I tried it but it seems to be giving me an error when I try to run the game sadly

error2.thumb.PNG.1899e781c7f1e20b2d0ee077e780d232.PNG

here's the entire master_postinit section, "local master_postinit = function(inst)" being line 67

  Hide contents

 

local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(175)
    inst.components.hunger:SetMax(150)
    inst.components.sanity:SetMax(200)
    
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
    inst.components.health.fire_damage_scale = 1
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
        --Sanity rate
    inst.components.sanity.custom_rate_fn = sanityfn
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
    --reduced crafting cost
    inst.components.builder.ingredientmod = 0.5

    --sanity food reduction 
    if inst.components.eater ~= nil then
        inst.components.eater:SetAbsorptionModifiers(1, 1, 0)

        inst.components.eater:SetOnEatFn(function(inst, food)
       local delta = food.components.edible.sanityvalue

        if delta > 0 then delta = delta*0.5 end 

    inst.components.sanity:DoDelta(delta)
    end)
    end
    
return MakePlayerCharacter("genny", prefabs, assets, common_postinit, master_postinit, start_inv)

 

 

Looks like you accidentally replaced the end needed to finish the master_postinit with the end to finish the "if inst.components.eater" check :p Just adding another end above the return MakePlayer will fix that

Link to comment
Share on other sites

10 hours ago, Mr. Tiddles said:

In the hunger component you'll find:

 


function Eater:SetAbsorptionModifiers(health, hunger, sanity)
    self.healthabsorption = health
    self.hungerabsorption = hunger
    self.sanityabsorption = sanity
end

In Wortox's prefab it looks a bit more like this:


    if inst.components.eater ~= nil then
        inst.components.eater:SetAbsorptionModifiers(TUNING.WORTOX_FOOD_MULT, TUNING.WORTOX_FOOD_MULT, TUNING.WORTOX_FOOD_MULT)
    end

If you look between it and the function from the eater component, you'll see the first value is health, the second is hunger, and the third is sanity. So setting the first two as 1 will just make them default, while less than 1 for the sanity will make it give less.

 

Ultroman's code is a bit more complex to specifically make different kinds of food do different things, since surprisingly we don't seem to have the ability to do it too easily at the moment. 

 

How did I not know about absorption modifiers?!? That's awesome! And obviously a much cleaner solution than mine. I guess one shouldn't rely too heavily on existing scripts, without checking if this particular problem could be solved much easier. Sorry about that.

I guess you don't need me to explain about my code, then ;)

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