Jump to content

Recommended Posts

I would like to add a new item that raises the character's sanity while it is in his inventory. (The item will be included in the starting inventory of the characters) A similar principle as with the Willow teddy bear, but without its features. Just a mind-boosting object. Unfortunately, I understand this programming language quite poorly, so I wanted to ask for your help.

I immediately thought of Wurt. I remember that Wurt is happy when she's carrying around a live fish in her inventory.

in Wurt.lua (in prefabs) there are these lines which seem to be exactly what you're looking for:

    inst:AddComponent("itemaffinity")
    inst.components.itemaffinity:AddAffinity("hutch_fishbowl", nil, TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fish", TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fishmeat", -TUNING.DAPPERNESS_MED_LARGE, 2)
    inst.components.itemaffinity:AddAffinity(nil, "spoiled_fish", -TUNING.DAPPERNESS_MED_LARGE, 2)

(notice that those MED_LARGE are negative. Doesn't like carrying around dead fish)

looking in itemaffinity.lua (in components), the top commented lines are 

-- Gives characters a sanity bonus from carrying a specific item in their inventory
-- It does not stack though, only one item bonus will be active at a time, so items have a priority

if there's only one item, it seems like it would as simple as adding the

inst:AddComponent("itemaffinity")
inst.components.itemaffinity:AddAffinity("your_item_name_here", nil, TUNING.DAPPERNESS_MED, 1)

lines inside your local function master_postinit(inst)

Tuning.lua (that contains all the default numbers and bonuses for stuff, is in the main scripts folder), says the DAPPERNESS levels are:

        DAPPERNESS_TINY = 100/(day_time*15),
        DAPPERNESS_SMALL = 100/(day_time*10),
        DAPPERNESS_MED = 100/(day_time*6),
        DAPPERNESS_MED_LARGE = 100/(day_time*4.5),
        DAPPERNESS_LARGE = 100/(day_time*3),
        DAPPERNESS_HUGE = 100/(day_time),
        DAPPERNESS_SUPERHUGE = 100/(day_time*0.5),

so, trying to figure out exactly what that means...l know Garland gives a little bit of sanity and the Top Hat gives more, so let's check Hats.lua and referencing https://dontstarve.fandom.com/wiki/Sanity:

Winter hat and Garland are "tiny" (1.33/min), Feather says "small" (2/min), Walrus says "LARGE" (guessing that's Tam O' Shanter, which is 6.67/min), Spider Hat (from Spider Queen) is -small (-2/min), Top Hat is Medium, etc.

Tiny = 1.33/min, Small = 2/min, Med = 3.33/min, Med_Large = 4.44/min, Large = 6.67/min..hmm...Huge is 3x what Large is so that's 20 (playing as Maxwell), double that would be 40 which is Hutch's aura.

So, decide just how good your item is compared to others and pick one of those.

If you want to learn more from the game's scripts, they're in your Don't Starve Together\data\databundles folder. Open the Scripts.zip file and then open the lua files from there or unzip it somewhere else to be safe. I use SciTE to work with lua and xml files.

  • Like 1
  • Thanks 1
  • Wavey 1
12 hours ago, Astralite said:

I immediately thought of Wurt. I remember that Wurt is happy when she's carrying around a live fish in her inventory.

in Wurt.lua (in prefabs) there are these lines which seem to be exactly what you're looking for:


    inst:AddComponent("itemaffinity")
    inst.components.itemaffinity:AddAffinity("hutch_fishbowl", nil, TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fish", TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fishmeat", -TUNING.DAPPERNESS_MED_LARGE, 2)
    inst.components.itemaffinity:AddAffinity(nil, "spoiled_fish", -TUNING.DAPPERNESS_MED_LARGE, 2)

(notice that those MED_LARGE are negative. Doesn't like carrying around dead fish)

looking in itemaffinity.lua (in components), the top commented lines are 


-- Gives characters a sanity bonus from carrying a specific item in their inventory
-- It does not stack though, only one item bonus will be active at a time, so items have a priority

if there's only one item, it seems like it would as simple as adding the


inst:AddComponent("itemaffinity")
inst.components.itemaffinity:AddAffinity("your_item_name_here", nil, TUNING.DAPPERNESS_MED, 1)

lines inside your local function master_postinit(inst)

Tuning.lua (that contains all the default numbers and bonuses for stuff, is in the main scripts folder), says the DAPPERNESS levels are:


        DAPPERNESS_TINY = 100/(day_time*15),
        DAPPERNESS_SMALL = 100/(day_time*10),
        DAPPERNESS_MED = 100/(day_time*6),
        DAPPERNESS_MED_LARGE = 100/(day_time*4.5),
        DAPPERNESS_LARGE = 100/(day_time*3),
        DAPPERNESS_HUGE = 100/(day_time),
        DAPPERNESS_SUPERHUGE = 100/(day_time*0.5),

so, trying to figure out exactly what that means...l know Garland gives a little bit of sanity and the Top Hat gives more, so let's check Hats.lua and referencing https://dontstarve.fandom.com/wiki/Sanity:

Winter hat and Garland are "tiny" (1.33/min), Feather says "small" (2/min), Walrus says "LARGE" (guessing that's Tam O' Shanter, which is 6.67/min), Spider Hat (from Spider Queen) is -small (-2/min), Top Hat is Medium, etc.

Tiny = 1.33/min, Small = 2/min, Med = 3.33/min, Med_Large = 4.44/min, Large = 6.67/min..hmm...Huge is 3x what Large is so that's 20 (playing as Maxwell), double that would be 40 which is Hutch's aura.

So, decide just how good your item is compared to others and pick one of those.

If you want to learn more from the game's scripts, they're in your Don't Starve Together\data\databundles folder. Open the Scripts.zip file and then open the lua files from there or unzip it somewhere else to be safe. I use SciTE to work with lua and xml files.



Thanks! I'll try to check it out. I wanted this object (a plushy) to restore about +4 or +5 sanity per minute. It's because the character who will have this plushy will have a rather small amount of sanity. And so I also wanted to ask - is it possible to use the code from the original characters if I want to add this item for a new character. (I'm sorry if I'm asking stupid questions. I just want to better understand how it all works)

Pretty much, yeah. You could start with a copy of Wilson.lua, remove the beard stuff and that'd be pretty vanilla. Then just learn what components you want to add to your character to give them abilities and perks, and of course add the Plush to your starting inventory and give your character the affinity for it.

TUNING.GAMEMODE_STARTING_ITEMS.DEFAULT.YOURCHARACTERNAME = {
    "plush",
}

 

  • Health 1
2 hours ago, Astralite said:

Pretty much, yeah. You could start with a copy of Wilson.lua, remove the beard stuff and that'd be pretty vanilla. Then just learn what components you want to add to your character to give them abilities and perks, and of course add the Plush to your starting inventory and give your character the affinity for it.


TUNING.GAMEMODE_STARTING_ITEMS.DEFAULT.YOURCHARACTERNAME = {
    "plush",
}

Thank you for your help ^^ I was able to make this item and that it increases the sanity!

However, I had to do this by replacing one of the trinkets from the graves ^^'

So I would like to ask - is it possible to create a new item if you duplicate the original ".lua" file and make changes to it? Or do you need some other way? And if this is possible, then what other folders also need to be supplemented so that there are no errors when starting the game? (Sorry for the intrusiveness)

 

Early version of my reply was actually grabbing the code from the trinkets.lua file because they're mostly blank items with little code or interaction (exceptions being the ones that work in sea fishing, and special trades), but I trimmed it out.

By creating a new .lua file in your prefab folder, "..Don't Starve Together\mods\yourmod\scripts\prefabs\plush.lua", as long as it's not named the same as another prefab, it will create an entirely different item.

The easiest way to make a new item is to take another item that's closest to it, make a copy, rename and edit to your needs.

This is the gears.lua with the extra stuff cut out and names changed to plush. All you'd (basically) need to do is make the art.

local assets =
{
    Asset("ANIM", "anim/plush.zip"), 
}

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("plush") 
    inst.AnimState:SetBuild("plush")
    inst.AnimState:PlayAnimation("idle")

    MakeInventoryFloatable(inst, "med", nil, 0.7) 

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    MakeHauntableLaunch(inst)

    return inst
end

return Prefab("plush", fn, assets)

 

1 hour ago, Astralite said:

Early version of my reply was actually grabbing the code from the trinkets.lua file because they're mostly blank items with little code or interaction (exceptions being the ones that work in sea fishing, and special trades), but I trimmed it out.

By creating a new .lua file in your prefab folder, "..Don't Starve Together\mods\yourmod\scripts\prefabs\plush.lua", as long as it's not named the same as another prefab, it will create an entirely different item.

The easiest way to make a new item is to take another item that's closest to it, make a copy, rename and edit to your needs.

This is the gears.lua with the extra stuff cut out and names changed to plush. All you'd (basically) need to do is make the art.


local assets =
{
    Asset("ANIM", "anim/plush.zip"), 
}

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("plush") 
    inst.AnimState:SetBuild("plush")
    inst.AnimState:PlayAnimation("idle")

    MakeInventoryFloatable(inst, "med", nil, 0.7) 

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    MakeHauntableLaunch(inst)

    return inst
end

return Prefab("plush", fn, assets)

 

Thank you again. You have helped me a lot ^^

On 6/25/2021 at 5:57 PM, Astralite said:

I immediately thought of Wurt. I remember that Wurt is happy when she's carrying around a live fish in her inventory.

in Wurt.lua (in prefabs) there are these lines which seem to be exactly what you're looking for:


    inst:AddComponent("itemaffinity")
    inst.components.itemaffinity:AddAffinity("hutch_fishbowl", nil, TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fish", TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fishmeat", -TUNING.DAPPERNESS_MED_LARGE, 2)
    inst.components.itemaffinity:AddAffinity(nil, "spoiled_fish", -TUNING.DAPPERNESS_MED_LARGE, 2)

(notice that those MED_LARGE are negative. Doesn't like carrying around dead fish)

looking in itemaffinity.lua (in components), the top commented lines are 


-- Gives characters a sanity bonus from carrying a specific item in their inventory
-- It does not stack though, only one item bonus will be active at a time, so items have a priority

if there's only one item, it seems like it would as simple as adding the


inst:AddComponent("itemaffinity")
inst.components.itemaffinity:AddAffinity("your_item_name_here", nil, TUNING.DAPPERNESS_MED, 1)

lines inside your local function master_postinit(inst)

Tuning.lua (that contains all the default numbers and bonuses for stuff, is in the main scripts folder), says the DAPPERNESS levels are:


        DAPPERNESS_TINY = 100/(day_time*15),
        DAPPERNESS_SMALL = 100/(day_time*10),
        DAPPERNESS_MED = 100/(day_time*6),
        DAPPERNESS_MED_LARGE = 100/(day_time*4.5),
        DAPPERNESS_LARGE = 100/(day_time*3),
        DAPPERNESS_HUGE = 100/(day_time),
        DAPPERNESS_SUPERHUGE = 100/(day_time*0.5),

so, trying to figure out exactly what that means...l know Garland gives a little bit of sanity and the Top Hat gives more, so let's check Hats.lua and referencing https://dontstarve.fandom.com/wiki/Sanity:

Winter hat and Garland are "tiny" (1.33/min), Feather says "small" (2/min), Walrus says "LARGE" (guessing that's Tam O' Shanter, which is 6.67/min), Spider Hat (from Spider Queen) is -small (-2/min), Top Hat is Medium, etc.

Tiny = 1.33/min, Small = 2/min, Med = 3.33/min, Med_Large = 4.44/min, Large = 6.67/min..hmm...Huge is 3x what Large is so that's 20 (playing as Maxwell), double that would be 40 which is Hutch's aura.

So, decide just how good your item is compared to others and pick one of those.

If you want to learn more from the game's scripts, they're in your Don't Starve Together\data\databundles folder. Open the Scripts.zip file and then open the lua files from there or unzip it somewhere else to be safe. I use SciTE to work with lua and xml files.

Hi! I apologize to butt in like this, but I'm also pretty much new to the coding and things, though I somewhat understand the functions and things like that-

This thread however caught my attention, as it's exactly what I would like to do for the character that I'm currently working on. Everything will load into the game perfectly fine, no crashes or anything, it's just that I don't see this line of code actually working like it's supposed to, unless I'm just impatient.

Anyways, I tried out this piece of code in my charactername.lua (in this case "fractal") file under the local function master_postinit(inst) as you had directed;
image.thumb.png.5f695b0c89755c5117cc7f814cb20e0e.png 
for some reason this does not work when I go to play the game? Or it's just not as significant as I would like it to be. I have been playing around with the numbers as you can tell, but still, no sign of even the sanity meter going up at all.

Is there something that I'm missing, or some other kind of code that would help? Or did I place it in the wrong file/need to add something there to make it work?

Well, your lines that say: 

 inst:AddComponent("itemaffinity")
 inst.components.itemaffinity:AddAffinity("fishingrod", nil, TUNING.DAPPERNESS_MED, 14)
 inst.components.itemaffinity:AddAffinity(nil, "salt", TUNING.DAPPERNESS_MED, 14)
 inst.components.itemaffinity:AddAffinity(nil, "shears", -TUNING.DAPPERNESS_MED_LARGE, 4)
 inst.components.itemaffinity:AddAffinity(nil, "torch", -TUNING.DAPPERNESS_MED_LARGE, 4)

"fishingrod" is correct, but there is no "salt". If you mean Salt Crystals (https://dontstarve.fandom.com/wiki/Salt_Crystals) that are used by Warly, that has to be "saltrock". If you mean Season Salt (https://dontstarve.fandom.com/wiki/Seasoning_Salt) that is made from 3 Salt Crystals, that would be "spice_salt". If you look up the item on the wiki, you'll see on the bottom of the info card on the side, the "code".  That's the name of the item you need to refer to.

Your second problem is that you used (nil, "salt", TUNING.DAPPERNESS_MED, 14)

the first part is the item name, the second is the item type, or tag that it has. If you're referring to a specific item, it has to be "name, nil". Unless "salt" was a tag that multiple items have, and you'd get the affinity from all of them. (like having an affinity for nil, "bird" would return all 5 or so types of birds).

"shears" and "torch" are correct, but also in the wrong space. So...

 inst:AddComponent("itemaffinity")
 inst.components.itemaffinity:AddAffinity("fishingrod", nil, TUNING.DAPPERNESS_MED, 2)
 inst.components.itemaffinity:AddAffinity("spice_salt", nil, TUNING.DAPPERNESS_MED, 2)
 inst.components.itemaffinity:AddAffinity("shears", nil, -TUNING.DAPPERNESS_SUPERHUGE, 1)
 inst.components.itemaffinity:AddAffinity("torch", nil, -TUNING.DAPPERNESS_SUPERHUGE, 1)

Also, you don't need the last numbers so high. The code just orders them by highest to lowest, and I'm guessing if it's the same number it doesn't really matter. If either exists (and have the same DAPPERNESS), it's good enough.

Like in Wurt's code. The hutch_fishbowl is a specific item. "fish" is the tag that multiple items have (there are lot more fish with the ocean update), same with fishmeat and spoiled fish. They're tags given to multiple items. Her priorities at the end of these lines are basically saying the fish meat (ie, dead fish) and spoiled_fish (also dead fish) make her unhappy, so the game checks for them first, and only if they're not there (since only 1 itemaffinity can be active at one time), it then checks the lower numbers to see if she's happy for carrying a live fish.

    inst:AddComponent("itemaffinity")
    inst.components.itemaffinity:AddAffinity("hutch_fishbowl", nil, TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fish", TUNING.DAPPERNESS_MED, 1)
    inst.components.itemaffinity:AddAffinity(nil, "fishmeat", -TUNING.DAPPERNESS_MED_LARGE, 2)
    inst.components.itemaffinity:AddAffinity(nil, "spoiled_fish", -TUNING.DAPPERNESS_MED_LARGE, 2)

 

  • Like 1

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