Jump to content

Overwriting a component for a specific prefab.


Recommended Posts

Is this possible?

I've written a (basically copied and slightly modified) version of the follower component and I'm trying to use it to change the behavior of specific prefabs. I can't just directly modify the behavior of the component with AddComponentPostInit("follower", (changes)) because that would change it for everyone. Is what I'm trying to do possible? Everything refers to the "follower" component so I'm not sure what to do. I'm assuming I can't just pass my new component into the "follower" field in the prefab's table, right?

 

Link to comment
Share on other sites

Depending on what you want to change, you can just add a check at the beginning of the functions that are called so that they are only run if they are a certain prefab. For example:

local function FollowerTest(self)
	local old_StartLeashing = self.StartLeashing
	function self:StartLeashing(...)
		if self.inst and self.inst.prefab == "your_prefab" then
			--your code
		else
			return GLOBAL.unpack{old_StartLeashing(self,...)}
		end
	end
end

AddComponentPostInit("follower", FollowerTest)

But this will only work if you have functions that are not local, as otherwise they can't be accessed.

If you would need to change local functions for only a few prefabs, you can try this:

AddPrefabPostInit("your_prefab",function(inst)
	if inst.components.follower ~= nil then
		inst.components.follower = nil
		local new_cmp = require "components/new_follower"
		local loaded_cmp = new_cmp(inst)
		inst.components.follower = loaded_cmp
	end
end)

You will need to have the component you want to change as a new file with the changes you want. This will load the changed component instead of the normal component. But this is also more prone to errors as I'm not sure where exactly problems could arise. This has also not included the componentpostinits of the changed component so you would also need to add them if you want them.

So as long as you don't need to change local functions, I would use the first option where you check for the prefab name :)

Link to comment
Share on other sites

3 hours ago, Monti18 said:

Depending on what you want to change, you can just add a check at the beginning of the functions that are called so that they are only run if they are a certain prefab. For example:


local function FollowerTest(self)
	local old_StartLeashing = self.StartLeashing
	function self:StartLeashing(...)
		if self.inst and self.inst.prefab == "your_prefab" then
			--your code
		else
			return GLOBAL.unpack{old_StartLeashing(self,...)}
		end
	end
end

AddComponentPostInit("follower", FollowerTest)

But this will only work if you have functions that are not local, as otherwise they can't be accessed.

If you would need to change local functions for only a few prefabs, you can try this:


AddPrefabPostInit("your_prefab",function(inst)
	if inst.components.follower ~= nil then
		inst.components.follower = nil
		local new_cmp = require "components/new_follower"
		local loaded_cmp = new_cmp(inst)
		inst.components.follower = loaded_cmp
	end
end)

You will need to have the component you want to change as a new file with the changes you want. This will load the changed component instead of the normal component. But this is also more prone to errors as I'm not sure where exactly problems could arise. This has also not included the componentpostinits of the changed component so you would also need to add them if you want them.

So as long as you don't need to change local functions, I would use the first option where you check for the prefab name :)

Thank you! Both of these are really good. I'm actually inclined towards the second option because I did write an entire modified follower class (oops). I'll try both and see what sticks.

Link to comment
Share on other sites

For the record the part:

return GLOBAL.unpack{old_StartLeashing(self,...)}

Can be reduced to:

return old_StartLeashing(self,...)

Since it's not modifying anything.  Saves memory and computation for moving the returns into a table to then unpack them back into returns.

  • Like 2
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...