juan104 Posted June 13, 2021 Share Posted June 13, 2021 (edited) Edit: Special thanks to krylincy and Monti18 for their help. I've been trying to mod the guts the black swordsman mod (https://steamcommunity.com/sharedfiles/filedetails/?id=2285521612) and I wanted to make it so that when it's nighttime he gets attacked by monsters. I've been trying different pieces of code but none have worked, here's my latest try. What am I doing wrong? GLOBAL.require master_postinit = function(inst:WatchWorldState("isnight", SetInducedInsanity)) local function brandattraction(inst) if Theworld.state.isnight then inst.components.sanity:SetInducedInsanity(inst, true) end end end Edited June 15, 2021 by juan104 Link to comment Share on other sites More sharing options...
juan104 Posted June 13, 2021 Author Share Posted June 13, 2021 I've also tried with this piece of code AddPrefabPostInit("guts", function tryspawnthing (inst) if TheWorld ~= nil then if TheWorld.state.isnight then inst:AddComponent("childspawner") inst.components.childspawner:SetRegenPeriod(5) inst.components.childspawner:SetSpawnPeriod(5) inst.components.childspawner:SetMaxChildren(2) inst.components.childspawner:StartRegen() inst.components.childspawner.spawnradius = {min = 10, max = 30} inst.components.childspawner.childname = "nightmarebeak" inst.components.childspawner:StartSpawning() end end end ) but it doesn't work. I think TheWorld returns nil when entering the world and then it just doesn't run. What do I do? Link to comment Share on other sites More sharing options...
krylincy Posted June 13, 2021 Share Posted June 13, 2021 (edited) you can watch the world state in your prefab like AddPrefabPostInit("guts", function tryspawnthing (inst) if TheWorld ~= nil then -- not sure if you need this check inst:WatchWorldState("isnight", function() if inst.components ~= nil then -- this check my be enough inst:AddComponent("childspawner") inst.components.childspawner:SetRegenPeriod(5) ... end end) end end and then add a watch for day to remove the component again Edited June 13, 2021 by krylincy Link to comment Share on other sites More sharing options...
Monti18 Posted June 14, 2021 Share Posted June 14, 2021 Do you have a line in your modmain where you wrote local TheWorld = GLOBAL.TheWorld If not, this is one of your problems, as TheWorld without GLOBAL is always nil in the modmain. In your character prefab you won't need a GLOBAL. Use what krylincy wrote and add a GLOBAL depending on if you need it. I think the TheWorld check and inst.components check may not be needed, if there is an error there the game has bigger problems. 1 Link to comment Share on other sites More sharing options...
krylincy Posted June 14, 2021 Share Posted June 14, 2021 9 hours ago, Monti18 said: I think the TheWorld check and inst.components check may not be needed, if there is an error there the game has bigger problems. i thought i always need the component check in a prefab postint to skip the clients, like the default prefab would do with if not TheWorld.ismastersim then return inst end Link to comment Share on other sites More sharing options...
Monti18 Posted June 14, 2021 Share Posted June 14, 2021 inst.components exists on client, so this check doesn't help to check if it's a client. But you are right, there should be a check for clients. For this, you add the same thing as you wrote. AddPrefabPostInit("guts", function(inst) if not TheWorld.ismastersim then return inst end inst:WatchWorldState("isnight", function() inst:AddComponent("childspawner") inst.components.childspawner:SetRegenPeriod(5) ... end) end I also just saw that the function shouldn't have a name as you define it directly afterwards. You can also just add to the master postinit this code: inst:WatchWorldState("isnight", function() inst:AddComponent("childspawner") inst.components.childspawner:SetRegenPeriod(5) ... end) or this: inst:WatchWorldState("isnight", function() inst.components.sanity:SetInducedInsanity(inst, true) end) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now