Notifications
Clear all

[Closed] pass variables between functions & rollouts while avoiding globals

So i have variables I want to pass between functions and rollouts
but I have read many, many times that globals should be avoided…

a drastically simplified example:


rollout doThings "doThings" (

	fn Something =(

		foo="how do I pass variables "
		bar=" without declaring "

		)

	fn SomethingElse =(

		foo= foo+"between functions and rollouts, "
		bar= bar+"them as global variables?"

		)

	button btn_Something "do something"
	on btn_Something pressed do Something()

	button btn_SomethignElse "do something else"
	on btn_SomethignElse pressed do SomethingElse()
)

rollout doOtherThings "doOtherThings"(

	fn OtherThing=(

		print foo+bar

	)

	button btn_OtherThing "do other thing"
	on btn_OtherThing pressed do OtherThing()
)

--add both rollouts to a floater
floater =newRolloutFloater " Do All Things " 200 150
addRollout doThings floater
addRollout doOtherThings floater



this doesn’t work unless I declare foo and bar as globals.

the question is:
avoiding globals, what is the best way to pass foo and bar between these functions and rollouts?

5 Replies

Are those rollouts in different files? I guess so, otherwise you could just make them local in the scope enclosing both the rollouts, just reassuring myself it’s not something trivial. In that case, especially with many variables you need to access from vrious parts of your scripts, having one global struct instance with all those variables would be the most painless way.

global myUglyVariableDump
(
	struct dump (foo, bar)
	myUglyVariableDump = dump()
)

...

	fn Something =
	(
		myUglyVariableDump.foo = "how do I pass variables "
		myUglyVariableDump.bar=" without declaring "
	)

...

You get the idea.

thanks for your response.

Yes, the example was drastically simplified. I have 20+ global variables being manipulated and passed between various functions, rollouts and scripts. I knew it wasn’t how I want to proceed,m but

I was just hunting the pages on “struct / rollout pairs” in the TechArt.org wiki,
the same idea I suppose.

but most of those wiki pages are 404…

structs are new to me
could you comment he first chunk -the struct declaration a bit to describe what the lines do?

actually let me guess:



--what type of scripty thing  is this first line called? 
--there's no "=" ...so can't be  a variable or function declaration
global myUglyVariableDump 

(
	
--constructor for the struct "dump"
	struct dump (foo, bar) 
	
--instantiate dump() as the variable myUglyVariableDump?
--how does this relate to the first line above?
	myUglyVariableDump = dump() 
	
)


it almost looks like you declare a global myUglyVariableDump variable,
but the decalration itself includes a the struct constructor function.

1 Reply
(@swordslayer)
Joined: 11 months ago

Posts: 0

Exactly, sorry for the confusion, the extra pair of brackets is there just to open a local scope – if the global declaration itself was in a local scope somewhere, it’s not needed at all. Or, as there’s just a single instance of the struct anyway, you could reuse the variable:

struct myUglyVariableDump (foo, bar)
myUglyVariableDump = myUglyVariableDump()

Sometimes you will witness code like this although it feels somewhat hideous to me.

Structs only need to be instantiated when you need to access not only the functions they contain but also their variables. So for functions only this is okay:

struct usefulFunctions
(
	fn add a b = a + b,
	fn subtract a b = a - b
)

usefulFunctions.add 10 5 -- works without instantiating

However when using variables, an instance is needed.

struct vars (varStr = "Sample", varInt)

vars.varStr --> won't work (not at max now, try it to get the corresponding error message)
varsInstDefault = vars()
varsInstDefault.varStr --> "Sample"
varsInstDefault.varInt --> undefined
varsInstCustom = vars varInt:10
varsInstCustom.varInt --> 10

thanks again.

the usefulness of structs is becoming much clearer.

which is making me want re-factor everything

which is good. it will make you finally free and happy