Jump to content

Recommended Posts

This is a very interesting issue I'm having with changing the Childspawner component.

 

Child Spawner Code

--[[  CHILDSPAWNER.LUA CHANGES  ]]local function ChildSpawnerPostInit(self)	self.children = {}	function self:AddChild(prefab)		self.children[prefab] = prefab	end	function self:SpawnChild(target, prefab, radius)		if not self:CanSpawn() then        	return    	end	local pos = GLOBAL.Vector3(self.inst.Transform:GetWorldPosition())	local start_angle = math.random()*GLOBAL.PI*2	local rad = radius or 0.5	if self.inst.Physics then		rad = rad + self.inst.Physics:GetRadius()	end	local offset = GLOBAL.FindWalkableOffset(pos, start_angle, rad, 8, false)	if offset == nil then		return	end	pos = pos + offset	-- Only line changed in this functionality.	--	local childtospawn = prefab or self.childname	local childtospawn	if self.children ~= nil then		childtospawn = prefab or GLOBAL.GetRandomKey(self.children)	end	if self.rarechild and math.random() < self.rarechildchance then		childtospawn = self.rarechild -- or GLOBAL.GetRandomKey(self.children)	end	    local child = GLOBAL.SpawnPrefab(childtospawn)        if child ~= nil then                child.Transform:SetPosition(pos:Get())        self:TakeOwnership(child)        if target and child.components.combat then            child.components.combat:SetTarget(target)        end                if self.onspawned then            self.onspawned(self.inst, child)        end				if self.childreninside == 1 and self.onvacate then            self:onvacate(self.inst)	    end	    self.childreninside = self.childreninside - 1	    			end	return child	endendAddComponentPostInit("childspawner", ChildSpawnerPostInit)

Add Child Code

		inst.components.childspawner:AddChild("mosquito")		inst.components.childspawner:AddChild("frog")

Now my problem seems to be when I pass 'self.children' which is a table to GLOBAL.GetRandomKey

 

Error

 

 

...m/steamapps/common/dont_starve/data/scripts/util.lua:230: bad argument #1 to 'random' (interval is empty)

 

Anyone have half a clue why it's not working, any help would be greatly appreciated.

 

Update: This is what happens when I have ROG enabled. In the base game it works flawlessly.

Edited by Kzisor

@Kzisor

This error happens when the table self.children is empty (which is pretty much always, except for your custom prefab or the prefab you're altering). Then the call done internally by GetRandomKey is math.random(0), which raises an error because the argument to math.random must be a positive integer.

I also would suggest that you don't overwrite game functions like that, to avoid having your mod incompatible with other mods. So my suggestion is that you remove that part changing the childspawner component in modmain altogether, and instead put this in your custom prefab's fn (if you're modifying a standard prefab instead, put this inside the prefab postinit, excluding the AddComponent call):

inst:AddComponent("childspawner")local children = {"mosquito", "frog"}local oldSpawnChild = inst.components.childspawner.SpawnChildfunction inst.components.childspawner:SpawnChild(target, prefab, radius)    if not prefab then        prefab = children[math.random(#children)]    end    return oldSpawnChild(self, target, prefab, radius)end
Edited by simplex

@Kzisor

This error happens when the table self.children is empty (which is pretty much always, except for your custom prefab or the prefab you're altering). Then the call done internally by GetRandomKey is math.random(0), which raises an error because the argument to math.random must be a positive integer.

I also would suggest that you don't overwrite game functions like that, to avoid having your mod incompatible with other mods. So my suggestion is that you remove that part changing the childspawner component in modmain altogether, and instead put this in your custom prefab's fn (if you're modifying a standard prefab instead, put this inside the prefab postinit, excluding the AddComponent call):

inst:AddComponent("childspawner")local children = {"mosquito", "frog"}local oldSpawnChild = inst.components.childspawner.SpawnChildfunction inst.components.childspawner:SpawnChild(target, prefab, radius)    if not prefab then        prefab = children[math.random(#children)]    end    return oldSpawnChild(self, target, prefab, radius)end

 

This is exactly what I was trying to accomplish, but couldn't figure out how to overwrite specific functions without overwriting the entire component function. Cheers!

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