Jump to content

How do I replace a prefab with another prefab during worldgen?


Recommended Posts

I think the easiest way is to change the appearance. 
SetBuild() on the prefab will change how it looks. 

AddPrefabPostInit("prefabname", function(inst)
	inst:SetBuild("buildname")
end)

According to deciduoustrees.lua, there're 5 types of trees. "deciduoustree_normal", "deciduoustree_tall", "deciduoustree_short", "deciduoustree_burnt, "deciduoustree_stump". So each one you override the build will change their appearance.

If changing their appearance is not what you want, you can do something like this.

AddPrefabPostInit("prefabname", function(inst)
    if not GLOBAL.TheWorld.ismastersim then return end
    GLOBAL.SpawnPrefab("saptree").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst:Remove()
end)

Spawn an alternative prefab and move to where the original prefab is and remove the original. 
But if you do this in deciduoustrees, world's regrowth system will consider the original trees have been removed and should be planted.
Which may cause the endless spawn of deciduoustrees. I'm not sure though. Maybe you need to tweak the world regrowth system as well in this case.

Maybe better ways exist, so don't give up finding a better way.

Link to comment
Share on other sites

On 5/25/2019 at 6:32 AM, YakumoYukari said:

I think the easiest way is to change the appearance. 
SetBuild() on the prefab will change how it looks. 


AddPrefabPostInit("prefabname", function(inst)
	inst:SetBuild("buildname")
end)

According to deciduoustrees.lua, there're 5 types of trees. "deciduoustree_normal", "deciduoustree_tall", "deciduoustree_short", "deciduoustree_burnt, "deciduoustree_stump". So each one you override the build will change their appearance.

If changing their appearance is not what you want, you can do something like this.


AddPrefabPostInit("prefabname", function(inst)
    if not GLOBAL.TheWorld.ismastersim then return end
    GLOBAL.SpawnPrefab("saptree").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst:Remove()
end)

Spawn an alternative prefab and move to where the original prefab is and remove the original. 
But if you do this in deciduoustrees, world's regrowth system will consider the original trees have been removed and should be planted.
Which may cause the endless spawn of deciduoustrees. I'm not sure though. Maybe you need to tweak the world regrowth system as well in this case.

Maybe better ways exist, so don't give up finding a better way.

when i just did the second one in my modmain, this happened. no deciduous trees (the prefab i was replacing) spawned, nor did saptrees spawn. Am I missing something in the execution of the second on other than just  

 

AddPrefabPostInit("deciduoustree", function(inst)
    if not GLOBAL.TheWorld.ismastersim then return end
    GLOBAL.SpawnPrefab("saptree").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst:Remove()
end)

 

?c25143b028a20d3df9bde629fdaec9de.jpg

Link to comment
Share on other sites

Try

AddPrefabPostInit("prefabname", function(inst)
	if not GLOBAL.TheWorld.ismastersim then return end
	inst:DoTaskInTime(0, function()
		GLOBAL.SpawnPrefab("saptree").Transform:SetPosition(inst.Transform:GetWorldPosition())
		inst:Remove()
	end)
end)

 

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