Jump to content

Recommended Posts

Hello.

I'm new to the Don't Starve modding, and am just trying to get into it by looking at other mods. I've worked with c# and Java before, so I understand basic scripting. However Lua is a bit tricky for me sometimes, as sometimes the writing is too simple for me.

Anyway. I looked at the mod "[DST+ROG]Unlimited Clothes" by A-Mod and thought I could optimize it a bit with a foreach loop, so I tried copying the code and fixing it up a bit. The modmain now looks like this:

--===============================================================
-- Vars
--===============================================================

PrefabFiles = {

}

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local RECIPETABS = GLOBAL.RECIPETABS
local Recipe = GLOBAL.Recipe
local Ingredient = GLOBAL.Ingredient
local TECH = GLOBAL.TECH
local clothesNormal = { "beehat", "tophat", "flowerhat", "strawhat", "beefalohat", "earmuffshat", "winterhat", "spiderhat" }
local clothesROG = { "featherhat", "icehat", "watermelonhat", "eyebrellahat", "rainhat", "catcoonhat", "walrushat", "molehat" }
--===============================================================
-- Functions
--===============================================================

function SetupNetwork(inst)
	inst.entity:AddNetwork()
end

function Is_ROG_Enabled()
    return GLOBAL.SEASONS.AUTUMN
end

-----------------------------------------------------------
-- Clothes
local function remove_durability_fromcloth(inst)
	if inst.components.fueled then
		inst:RemoveComponent("fueled")
	end

	if inst.components.perishable then
		inst:RemoveComponent("perishable")
	end
end


------------------------------------------------------------
-- Config Data
for key, value in clothesNormal do
	if(GetModConfigData("Unlimited_"+key) == 1) then
		AddPrefabPostInit(key, remove_durability_fromcloth)
	end
end

if(Is_ROG_Enabled) then
	for key, value in clothesROG do
		if(GetModConfigData("Unlimited_"+key) == 1) then
			AddPrefabPostInit(key, remove_durability_fromcloth)
		end
	end
end

First of all, I'm not sure if I did the foreach loop correctly. I made an string array called clothesNormal and clothesROG, but none of the strings inside of it has any keys, so I'm actually not sure if I should be using 'key' or 'value' in the places where I need it. In this piece of code I chose 'key' as my word. (I just tried using 'value' instead of 'key'. No difference)

Needless to say I started Don't Starve, made myself a Garland and saw it perish slowly. So it did not work. What am I doing wrong?

Edited by FlawlessHair

In Lua, table elements without a specified key are assigned numeric values, in the order they were added, starting at 1. So:

local clothesNormal = { "beehat", "tophat", "flowerhat", "strawhat", "beefalohat", "earmuffshat", "winterhat", "spiderhat" }

--Is equivalent to:

local clothesNormal = {}
clothesNormal[1] = "beehat"
clothesNormal[2] = "tophat"
clothesNormal[3] = "flowerhat"
clothesNormal[4] = "strawhat"
clothesNormal[5] = "beefalohat"
clothesNormal[6] = "earmuffshat"
clothesNormal[7] = "winterhat"
clothesNormal[8] = "spiderhat"

So your loop would be better off like this:

for i,v in ipairs(clothesNormal) do --This loop only does number keys, but is guaranteed to do them in order (good practice to use this for lists)
	--Do stuff with 'v'
end

 

Thank you for helping me with the loop Arkathorn. I tried testing my mod in Don't Starve, where it did not seem to work.

Then for the sake of it I tested it in Don't Starve Together, where it seemed to work just fine. Do you know why?

1 hour ago, Arkathorn said:

You have code concerning networks in it, which doesn't exist in normal DS.

So the mod disables itself in normal DS, even though that function is never called?

(I'm not really sure what the function does. It was in the mod I copied, so I figured I'd leave it in to not mess with something I didn't know about)

6 minutes ago, Arkathorn said:

No, it wouldn't. The error is likely something else. What was the error? (Find in 'Documents/Klei/DoNotStarve/log.txt')

Oh, hehe.

That was a little too simple. The api_version was simply the wrong number, as DST has a higher number than DS. Thank you for your help!

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