Jump to content

PrefabPostInit


_Q_

Recommended Posts

So I want to change some little things in prefabs, what would be the best thing to do it.

Little change in spider den prefabs:

- so the spider dens on lvl 3 don't drop spider egg sack

- changing child spawner to only spider warriors

- editing lootdropper components in spider prefabs

Next thing are postinits to trees to change the growth times and lootdropper components.

I had some troubles to make that with post inits, the way I was doing it was first remove lootdropper component and then add it again with the new drop list.

What would be the best way of doing lot of small changes to prefab?

 

Link to comment
Share on other sites

Nothing has a 'standard' solution, you need to look into how each thing works and how you can change it.

Spider den only spawning spider warriors:

-- set the number of warriors to spawn equal to the max children of a spiderden-- note the number spawned each time is limited in spiderden.lua's SpawnDefenders functionTUNING.SPIDERDEN_WARRIORS = TUNING.SPIDERDEN_SPIDERS
Level 3 spiderden not dropping egg sack:

AddPrefabPostInit("spiderden", function(inst)	if inst.components.growable and inst.components.growable.stages then		-- find the "large" stage		local large_stage = nil		for i,stage in ipairs(inst.components.growable.stages do			if stage.name == "large" then				large_stage = stage			break			end		end		-- remove "spidereggsack" from the loot table after it becomes large		if large_stage then			local stagefn_base = large_stage.fn or function() end			large_stage.fn = function(inst)				stagefn_base(inst)				RemoveByValue(inst.components.lootdropper.loot, "spidereggsack")			end		end	endend)
Editing lootdropper for spiders:

AddPrefabPostInit("spider", function(inst)	local lootdropper = inst.components.lootdropper	if lootdropper and lootdropper.randomloot then		for i,loot in ipairs(lootdropper.randomloot) do			print("loot "..loot.prefab.." has a weight of "..loot.weight)			-- modify			loot.prefab = "minerhat"			local oldweight = loot.weight			loot.weight = 10			-- update totalrandomweight			lootdropper.totalrandomweight = lootdropper.totalrandomweight + weight - oldweight		end	endend)
Modifying evergreen growspeed:

-- see tuning.lua's EVERGREEN_GROW_TIME table and evergreens.lua's growth_stages tableTUNING.EVERGREEN_GROW_TIME[1].base = TUNING.EVERGREEN_GROW_TIME[1].base * 10TUNING.EVERGREEN_GROW_TIME[1].random = TUNING.EVERGREEN_GROW_TIME[1].random * 10-- the EVERGREEN_GROW_TIME table has indexes 1-4TUNING.EVERGREEN_GROW_TIME[4].base = TUNING.EVERGREEN_GROW_TIME[4].base * 10TUNING.EVERGREEN_GROW_TIME[4].random = TUNING.EVERGREEN_GROW_TIME[4].random * 10
Modifying evergreen lootdropper:

local tree_prefabs = { "evergreen", "evergreen_normal", "evergreen_tall", "evergreen_short", "evergreen_sparse", "evergreen_sparse_normal", "evergreen_sparse_tall", "evergreen_sparse_short" }for i,prefabname in ipairs(tree_prefabs) do	AddPrefabPostInit(prefabname, function(inst)		if inst.components.growable and inst.components.growable.stages then			for i,stage in ipairs(inst.components.growable.stages) do				local stagefn_base = stage.fn or function() end				stage.fn = function(inst)					stagefn_base(inst)					-- modify inst.components.lootdropper.loot here				end			end		end	end)end
All code is untested but should work (probably has some syntax errors). Also note that the PrefabPostInit for trees might take a while to execute when you load the game, as it is overwriting 4 functions per tree (one per growth stage). It's not ideal and there might be a less expensive method.

EDIT: This forum software is literal garbage, it thinks it knows better than me about how to format things even though I'm not using the WYSIWYG editor at all... Will try to fix the formatting.

EDIT#2: Fixed formatting.

Link to comment
Share on other sites

Thought of a better way to do the evergreen lootdropper modification:

 

-- if in modmain, need thislocal require = GLOBAL.requirelocal Growable = require "components/growable"-- add an event for when the growth stage changeslocal Growable_SetStage_base = Growable.SetStagefunction Growable:SetStage(stage)    local oldstage = self.stage    Growable_SetStage_base(self, stage)        if oldstage ~= self.stage then        self.inst:PushEvent("growthstagechanged", {oldstage=oldstage, stage=self.stage})    endend-- catch the growth stage changed eventlocal function OnTreeGrowthStageChanged(inst, data)    -- modify inst.components.lootdropper.loot here    print( tostring(inst).." grew to stage # "..tostring(data.stage).." from stage # "..tostring(data.oldstage) )endlocal tree_prefabs = { "evergreen", "evergreen_normal", "evergreen_tall", "evergreen_short", "evergreen_sparse", "evergreen_sparse_normal", "evergreen_sparse_tall", "evergreen_sparse_short" }for i,prefabname in ipairs(tree_prefabs) do    AddPrefabPostInit(prefabname, function(inst)        inst:ListenForEvent("growthstagechanged", OnTreeGrowthStageChanged)    end)end
The same thing could (and probably should) be done for the spiderden lootdropper modifications.

 

Thanks, many good things in that code examples.

I would never come up with some cool things like: RemoveByValue(inst.components.lootdropper.loot, "spidereggsack")

Take a look at util.lua, there are a lot of really useful helper functions in there.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...