Notifications
Clear all

[Closed] Problem With Scope of variables

Hi Masters
I’ve got a silly problem with scope of variables during creating my own plugin which seems to have a simple answer, anyway here we go.
my problem is I can’t edit a value of a variable with a custom function and call it outside that Custom function. lets see the example: here is a test I’ve prepared to better showing my problem


plugin modifier Test --blah blah
name:"test" --blah blah
classID:#(0x57c4464a, 0x1ba5a130) --blah blah

(
	
	local fn_create,fn_delete --function names as local of this block
	parameters param
	(
		nodes	type:#nodetab tabsizevariable:true --the parameter which I want edit its value ---by a custom function
	)
	
	rollout createObjs "create objects" width:162 height:70
	(
		button btn_create "create" pos:[46,10] width:85 height:25 
		button btn_del "Delete" pos:[46,38] width:85 height:25
		
		on btn_create pressed  do
		(
			fn_create()
		)
		on btn_del pressed  do
		(
		fn_delete()
		)
	)
	
	fn fn_create = --*** here is where the magic does NOT happen.my function to create some --objects and put them into "node" array
	(
		for i =1 to 5 do
		(
			a = sphere pos:[10*i , 0, 0]
			append nodes a
		)
	)
	
	fn fn_delete =  --- my function to delete the value of nodes
	(
		for i = 1 to nodes.count do
			delete nodes[i]
		nodes=#()
	)
)

as it shows (hopefully) I have function to create objects and another one to delete objects but I can’t represent a common variable for both functions

Anyone help ? thank you very very much

2 Replies

that’s a known issue of definition order. you define your functions after use in the rollout. when plugin creates the rollout it uses pointers to undefined at this moment functions (pressed handle is same as function)…
so there to several ways to fix it. i show two:
#one
force the plugin to use function definition on the fly. it will search for a pointer of the function every time when rollout opens.
you code need this change

		on btn_create pressed do if fn_create != undefined do
		(
			fn_create()
		)
		on btn_del pressed do if fn_delete != undefined do
		(
			fn_delete()
		)

#two (which i prefer)
move you functions above the rollout definition. in this case we have to modify your functions a little because they use parameter block params before the param block was defined.

plugin modifier Test --blah blah
name:"test" --blah blah
classID:#(0x57c4464a, 0x1ba5a130) --blah blah
(
	fn fn_create =
	(
		for i=1 to 5 do append this.nodes (sphere pos:[10*i,0,0])
	)
	fn fn_delete =
	(
		for i=1 to this.nodes.count do delete this.nodes[i]
		this.nodes = #()
	)
	parameters param
	(
		nodes type:#nodetab tabsizevariable:true --the parameter which I want edit its value ---by a custom function
	)
	rollout createObjs "create objects" width:162 height:70
	(
		button btn_create "create" pos:[46,10] width:85 height:25 
		button btn_del "Delete" pos:[46,38] width:85 height:25
		
		on btn_create pressed do fn_create()
		on btn_del pressed do fn_delete()
	)
)

i prefer #2 because when start designing a plugin i don’t know for sure what functions i will add.

#three.
define all functions inside the rollout. this method i don’t like. inside the rollout should be only functions that attend to the this rollout only or expected to be used internally.

Thank you Very very much DenisT , you helped me all over again. You’re my man bro… I will go on and if something new did happen I will come back again