Notifications
Clear all

[Closed] recursive call by reference

I’m trying to create a recursive function to copy all nested position_xyz controllers
from one object to another, assuming they have the same controller structure

this is my code:


(
	fn copyPosXYZ src tar =
	(
		if isKindOf (*src) position_xyz then
			*tar = *src
		else if isKindOf (*src) position_list then (
			local a = *src
			for i = 1 to a.count do (
				local s = &((*src)[1].controller)
				local t = &((*tar)[1].controller)
				--*t = *s	-- test
				copyPosXYZ s t 	-- error
			)
		)
		OK
	)
	
	copyPosXYZ &$[1].pos.controller &$[2].pos.controller
) 

and this is the error I get:

-- Runtime error: * dereferences only allowed & reference values, got: 1

to test the code simply create an object, add position list to its position controller.
duplicate the object and animate the position of one of the objects.
now select first the animated object and then the other object and evaluate the code.

the expected result will be the position_xyz controller nested in the position_list controller on the animated object should be instanced onto the other object and their animation should be aligned.

can anyone help me? :shrug:

2 Replies

does this work for you?


(
	fn copyPosXYZ src tar =
	(
		if isKindOf (src) position_xyz then
		(
			replaceInstances (tar) (src)
			--tar = src
		)
		else if isKindOf (src) position_list then (
			local a = src
			for i = 1 to a.count do (
				
				tar[i].controller
				copyPosXYZ src[i].controller tar[i].controller	
			)
		)
		OK
	)
	
	copyPosXYZ $[1].pos.controller $[2].pos.controller
) 

why are you passing references? what is the need for them in this situation?

yep, thanks!

I needed to copy the src controller into the actual address of the tar controller
and not just into the variable ‘tar’

here is a cleaned up code of your solution:


(
	fn copyPosXYZ src tar =
	(
		if isKindOf src position_xyz then
			replaceInstances tar src
		else if isKindOf src position_list then
			for i = 1 to src.count do
				copyPosXYZ src[i].controller tar[i].controller	
		OK
	)
	
	copyPosXYZ $[1].pos.controller $[2].pos.controller
)