Jump to content

[beginner question] Trying to give my spawn data to hounded:SetSpawnData but it keeps crashing


Recommended Posts

Hi,
 
Firsly, I'd like to say that I'm a very beginner modder, since I've just started a couple days ago.
I don't have a solid background with programming languages, but I can usually figure out what I'm reading.
 
Here is what I want to do for some practice, and where I'm stuck:
 
Goal: Adjust the spawn data in hounded.lua for practice. In hounded.lua, there is comment that says to use hounded:SetSpawnData so I figured I'd use it.
 
My problem: I'm absolutely not sure where to put my new spawn data and how to call hounded:SetSpawnData
 
Because of that, I've just put everything in modmain.lua, as follows:

local mySpawn = {  base_prefab = "hound",  winter_prefab = "icehound",  summer_prefab = "firehound",  attack_levels =  {    intro = { warnduration = function() return 180 end, numspawns = function() return 1 end },    light = { warnduration = function() return 150 end, numspawns = function() return 1 + math.random(1) end },    med = { warnduration = function() return 120 end, numspawns = function() return 1 + math.random(2) end },    heavy = { warnduration = function() return 90 end, numspawns = function() return 1 + math.random(3) end },    crazy = { warnduration = function() return 90 end, numspawns = function() return 1 + math.random(4) end },  },  attack_delays =  {    rare = function() return TUNING.TOTAL_DAY_TIME * 10, math.random() * TUNING.TOTAL_DAY_TIME * 8 end,    occasional = function() return TUNING.TOTAL_DAY_TIME * 9, math.random() * TUNING.TOTAL_DAY_TIME * 6 end,    frequent = function() return TUNING.TOTAL_DAY_TIME * 8, math.random() * TUNING.TOTAL_DAY_TIME * 4 end,  },  warning_speech = "ANNOUNCE_HOUND",  warning_sound_thresholds =  { --Key = time, Value = sound prefab    {time = 30, sound = "houndwarning_lvl4"},    {time = 60, sound = "houndwarning_lvl3"},    {time = 90, sound = "houndwarning_lvl2"},    {time = 500, sound = "houndwarning_lvl1"},  },}GLOBAL.require("components/hounded")local hounded = GLOBAL.TheWorld.components.houndedhounded:SetSpawnData(mySpawn)


I've tried many different things, but since I don't quite understand, it was just copying things that seemed similar from other mods and adjusting them for hounded.lua. Nothing worked, so I'm missing a fundamental point.
 
Here is the error in the log.txt:

[00:00:17]: Loading mod: workshop-574086943 (Less Hounded) Version:0.1a
[00:00:17]: Mod: workshop-574086943 (Less Hounded) Loading modworldgenmain.lua
[00:00:17]: Mod: workshop-574086943 (Less Hounded) Mod had no modworldgenmain.lua. Skipping.
[00:00:17]: Mod: workshop-574086943 (Less Hounded) Loading modmain.lua
[00:00:17]: Mod: workshop-574086943 (Less Hounded) Error loading mod!
[string "../mods/workshop-574086943/modmain.lua"]:77: variable 'components' is not declared
LUA ERROR stack traceback:
=[C] in function 'error'
scripts/strict.lua(23,1)
../mods/workshop-574086943/modmain.lua(77,1) in main chunk
=[C] in function 'xpcall'
scripts/util.lua(597,1) in function 'RunInEnvironment'
scripts/mods.lua(408,1) in function 'InitializeModMain'
scripts/mods.lua(389,1) in function 'LoadMods'
scripts/main.lua(243,1) in function 'ModSafeStartup'
scripts/main.lua(298,1)
=[C] in function 'SetPersistentString'
scripts/mainfunctions.lua(22,1) in function 'SavePersistentString'
scripts/modindex.lua(79,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(66,1) in function 'BeginStartupSequence'
scripts/main.lua(297,1) in function 'callback'
scripts/modindex.lua(520,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(500,1) in function 'Load'
scripts/main.lua(296,1) in main chunk

[00:00:17]: [string "../mods/workshop-574086943/modmain.lua"]:77: variable 'components' is not declared
LUA ERROR stack traceback:
=[C] in function 'error'
scripts/strict.lua(23,1)
../mods/workshop-574086943/modmain.lua(77,1) in main chunk
=[C] in function 'xpcall'
scripts/util.lua(597,1) in function 'RunInEnvironment'
scripts/mods.lua(408,1) in function 'InitializeModMain'
scripts/mods.lua(389,1) in function 'LoadMods'
scripts/main.lua(243,1) in function 'ModSafeStartup'
scripts/main.lua(298,1)
=[C] in function 'SetPersistentString'
scripts/mainfunctions.lua(22,1) in function 'SavePersistentString'
scripts/modindex.lua(79,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(66,1) in function 'BeginStartupSequence'
scripts/main.lua(297,1) in function 'callback'
scripts/modindex.lua(520,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(500,1) in function 'Load'
scripts/main.lua(296,1) in main chunk
[00:00:17]: [string "scripts/mainfunctions.lua"]:969: variable 'global_error_widget' is not declared
LUA ERROR stack traceback:
=[C] in function 'error'
scripts/strict.lua(23,1)
scripts/mainfunctions.lua(969,1)
=[C] in function 'SetPersistentString'
scripts/mainfunctions.lua(22,1) in function 'SavePersistentString'
scripts/modindex.lua(79,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(66,1) in function 'BeginStartupSequence'
scripts/main.lua(297,1) in function 'callback'
scripts/modindex.lua(520,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(500,1) in function 'Load'
scripts/main.lua(296,1) in main chunk
[00:00:17]: DoLuaFile Error: (null)
[00:00:17]: LuaError but no error string
[00:00:17]: Error loading main.lua
[00:00:17]: Failed mSimulation->Reset()
[00:00:17]: Error during game restart!
[00:00:19]: Collecting garbage...
[00:00:19]: lua_gc took 0.01 seconds
[00:00:19]: ~ShardLuaProxy()
[00:00:19]: ~InventoryLuaProxy()
[00:00:19]: ~NetworkLuaProxy()
[00:00:19]: ~SimLuaProxy()
[00:00:19]: SteamWorkshop::CancelDownloads clearing all unfinished downloads
[00:00:19]: lua_close took 0.01 seconds
[00:00:20]: [steam] Auth ticket cancelled
[00:00:20]: CurlClientThread::Main() complete
[00:00:20]: HttpClient2 discarded 0 callbacks.
[00:00:20]: Shutting down

(the mod is hidden on steam since it's just some practice)
 
How you can help me:
  • What is the fundamental point that I'm missing to make this work?
  • Why is the above not working?
Edited by Nakiamiir
Link to comment
Share on other sites

@Nakiamiir, the above isn't working because you're trying to modify it in the wrong manner.

 

It would be wiser to use AddComponentPostInit in order to change the hounded, instead of trying to import the file then modify it that way.

AddComponentPostInit("hounded", function(hounded)    if not TheWorld.ismastersim then        return    end    hounded:SetSpawnData(mySpawn)end)
Link to comment
Share on other sites

It would be easier if you could post you files directly to link the crash log you posted to the files themselves. I have to admit I'd rather look at the code myself directly than reading pieces of it, unformated, in a forum post :-)

 

Thank you very much for your reply ZupaleX!

 

I'm sorry.

I tried to put in code like Kzisor did, but it didn't work. :hopelessness:

 

I've attached the files as requested.

There are two of them because I tried something else yesterday with AddSimPostInit

 

 

@Nakiamiir, the above isn't working because you're trying to modify it in the wrong manner.

 

It would be wiser to use AddComponentPostInit in order to change the hounded, instead of trying to import the file then modify it that way.

AddComponentPostInit("hounded", function(hounded)    if not TheWorld.ismastersim then        return    end    hounded:SetSpawnData(mySpawn)end)

 

Thank you very much Kzisor!

 

As the name implies (AddComponentPostInit) adds a component after something has been initialized, right?

Is that something the game world (TheWorld?) or the player (ThePlayer?) or else?

 

From my understanding, the first "hounded" part in AddComponentPostInit above adds the component hounded.lua from the regular game files if I haven't supplied a new one in scripts/components/hounded.lua in my mod folder, right?

 

One last thing, the function that is added as second argument is mandatory or optional?

I mean, is there another use of AddComponentPostInit in which supplying a function as second argument wouldn't be necessary?

 

Once again, thank you very much for helping me understanding!

 

P.S.: I've been trying to find a place where all these AddComponentPostInit, AddSimPostInit, etc. are explained. I've looked at modutil.lua but they are not commented, so I'm not really confident.

 

:wilsontricycle: 

modmain_test1.lua

modmain_test2.lua

Link to comment
Share on other sites

@Nakiamiir, AddSimPostInit is depreciated in Don't Starve Together. PostInit functions will run on the Prefab/Component after it's been initialized (the game loads). Add*PostInit require both parameters to be set at all times, there is no case where you can leave either of them unset unless you want to encounter errors.

 

All you have to do is copy the code I post and apply it over top the AddSimPostInit code in modmain_test2.lua. Delete the mySpawnFn because it's not needed.

 

Link to comment
Share on other sites

Sorry to highjack your thread. I promise it won't be long.

 

@Nakiamiir, AddSimPostInit is depreciated in Don't Starve Together. PostInit functions will run on the Prefab/Component after it's been initialized (the game loads).

 

But then what if you want to execute a function after everything was loaded without especially attaching it to a prefab of a component? AddSimPostInit was made for that no? And what is replacing it then now? Attaching it to forest.lua or cave.lua? Modifying the MakeWorld function?

Link to comment
Share on other sites

Sorry to highjack your thread. I promise it won't be long.

 

 

But then what if you want to execute a function after everything was loaded without especially attaching it to a prefab of a component? AddSimPostInit was made for that no? And what is replacing it then now? Attaching it to forest.lua or cave.lua? Modifying the MakeWorld function?

 

Without given a proper example of what you're wanting to do I cannot answer your question because there is no reason that I can think of for you to need to do that.

 

Link to comment
Share on other sites

Well I was just talking generally. Like the other day I modified an existing shared loot table in the AddSimPostInit for example because this is not the kind of stuff that needs to be attached to a component or prefab. And it needs to be done after the shared loot tables have been initialized.

Link to comment
Share on other sites

@Nakiamiir, AddSimPostInit is depreciated in Don't Starve Together. PostInit functions will run on the Prefab/Component after it's been initialized (the game loads). Add*PostInit require both parameters to be set at all times, there is no case where you can leave either of them unset unless you want to encounter errors.

 

All you have to do is copy the code I post and apply it over top the AddSimPostInit code in modmain_test2.lua. Delete the mySpawnFn because it's not needed.

 

Thank you very much for taking the time to reply once more and explain.

It makes more sense in my head now, haha. :wilson_flower:

 

I'll keep learning and exploring the modding universe of DST.

Thanks again!

Link to comment
Share on other sites

Who knew that copy-pasting was so hard. :wilson_cry:
 
I'm getting the following error when loading up any world with only this mod activated:

35: attempt to index global 'TheWorld' (a nil value)

 

I've included the full log.txt and the modmain.lua
 
I'm trying to figure out what I did wrong this time... I'll be back.

modmain.lua

log.txt

Link to comment
Share on other sites

TheWorld is, as the error suggest, a global variable, so in your modmain, you have to call it GLOBAL.TheWorld

 

You can also do at the beginning

 

local TheWorld = GLOBAL.TheWorld

 

so each call of TheWorld later will refer to GLOBAL.TheWorld

 

A really basic explanation : If you put a .lua in your "scripts" folder or a subfolder of "scripts" you do not have to put GLOBAL in front of global variable. If your .lua is not in this "scripts" folder, you have to make a "link" to these global variables.

 

So the issue with TheWorld will repeat with all the other global variables you called in your modmain.lua. To fix it, simply put GLOBAL.[the_global_variable]

 

Cheers

Link to comment
Share on other sites

TheWorld is, as the error suggest, a global variable, so in your modmain, you have to call it GLOBAL.TheWorld

 

You can also do at the beginning

 

local TheWorld = GLOBAL.TheWorld

 

so each call of TheWorld later will refer to GLOBAL.TheWorld

 

A really basic explanation : If you put a .lua in your "scripts" folder or a subfolder of "scripts" you do not have to put GLOBAL in front of global variable. If your .lua is not in this "scripts" folder, you have to make a "link" to these global variables.

 

So the issue with TheWorld will repeat with all the other global variables you called in your modmain.lua. To fix it, simply put GLOBAL.[the_global_variable]

 

Cheers

I didn't know that about the "scripts", I'll keep that in mind.

I've also added the line "local TheWorld = GLOBAL.TheWorld" at the top of my modmain.lua, but it's still not working.

 

This time, it says: "attempt to index upvalue 'TheWorld' (a nil value)"

 

My understanding of upvalue is limited to what is explained here, so I'm not too sure how to handle this error. I'll look around, hopefully I can find some explanation. :indecisiveness:

 

I've attached the files again.

log.txt

modmain.lua

Link to comment
Share on other sites

honnestly I just make "references" like that when it's something i will use a lot in the modmain.lua

If you just have some call to TheWorld, just call it GLOBAL.TheWorld each time. But that's just a matter of tastes :-)

 

EDIT : I did not see the post before. The issue is that i did not explain really clearly this local TheWorld = GLOBAL.TheWorld

 

In a lot of cases you can put it at the complete beginning of the modmain.lua

This is true for tables or variables which are already set or initialized when the modmain.lua is executed, such as the TUNING table or all the subtables in TUNING. Or what you can find in constants.lua

Putting at the beginning of the modmain.lua something like local TheWorld = GLOBAL.TheWorld cannot work because, in a nutshell, when executing this part of the modmain.lua, TheWorld is not really initialised yet.

But when calling the PostInit functions, it is, so that's why it's working within the PostInit function.

Edited by ZupaleX
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...