Sign in to follow this  
lbphero

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

Recommended Posts

lbphero    167

Say i wanted to replace all deciduous trees with, another modded in tree, by the name of "saptree". Would there be an easy way to do this? 

Share this post


Link to post
Share on other sites
YakumoYukari    144

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.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
lbphero    167
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

Share this post


Link to post
Share on other sites
Warbucks    2,388

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)

 

Share this post


Link to post
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

Sign in to follow this