Page 1 of 1

is there any way for Function to hold variables?

Posted: Fri Nov 18, 2022 9:24 am
by Bugala
I know I can do this:

Code: Select all

myvar = {}
myvar.var = 10

function myvar.func()
debugprint(myvar.var)
endfunction
But just from cleaner code point of view, I would rather have something like:

Code: Select all

Function Func()
func.var=10
debugprint(func.var)
Endfunction
Is something like this doable any way?

Re: is there any way for Function to hold variables?

Posted: Fri Nov 18, 2022 11:50 am
by Flinx
Hi Bugala,

I know that you ask Andreas, but I'm trying to understand what you're hoping to achieve.
In your first snippet you have the global table myvar containing the variable var and the function func. func prints the variable var from the table.

But what should be func.var in the second snippet? Something like a local variable of the function Func()? In Hollywoods syntax I think this would be an element of a table func, not of a function. And what are the connections to the first code snippet?
I the subject you write "hold variables". Do you mean local variables that hold their value over several function calls but are not visible from outside the function, as is done in C with the static keyword?

Ralf

Re: is there any way for Function to hold variables?

Posted: Fri Nov 18, 2022 5:37 pm
by Bugala
Lets put it like this.

What I am after is basically that if I decide to either reuse the code, or for example delete that function, I would in that case just want to delete stuff between FUNCTION and ENDFUNCTION.
I don't want to need to check if that code continues beyond those two, like in the first examples case I would need to.

As in:

Code: Select all

var = {}
var.x = 1

FUNCTION var.func()
ENDFUNCTION
If I decide I don't need this Function anymore, I would then need to notice that I also need to delete those two lines before to really delete that function completely.


Real case that just came to me in my code is that I am going to be having a function, which needs to hold sort of timer variable.

Code: Select all

FUNCTION animation()
Timer = Timer + Timepassed

If Timer = 1 then...
If Timer = 2 then...
If Timer > 3 then Timer = 0

ENDFUNCTION

Right now I either need to make that timer variable outside of that function, or to use the

Code: Select all

myvar = {}
myvar.timer = 0

FUNCTION myvar.func()
myvar.timer = myvar.timer + timepassed
ENDFUNCTION


So I would prefer having a system where that whole thing would be contained between FUNCTION and ENDFUNCTION, instead of it spreading outside of it.

Also, since if I don't use myvar option, then that timer would need to be global and could accidentally collide with something else at later point.

Re: is there any way for Function to hold variables?

Posted: Fri Nov 18, 2022 6:00 pm
by Flinx
Thank you, I see. It seems like you want what I mentioned in my last sentence.

Code: Select all

Static Timer = Timer + Timepassed

Re: is there any way for Function to hold variables?

Posted: Fri Nov 18, 2022 8:42 pm
by jPV
Lua doesn't have static variables, so I guess it's out of question for Hollywood too and you'll have to use global variables.

Maybe you should just decide a pattern to name these kinds of variables so that you don't use them elsewhere accidently. For example a global variable used in the animation function could be something like g_func_animation_timer and so on.

Re: is there any way for Function to hold variables?

Posted: Sat Nov 19, 2022 7:53 am
by Bugala
Yeah, I been using G_var naming convention in these kind of cases, but especially in this case when this G_Var is used only in this single function, it is somewhat inconvenient to resort to global function from coding style point of view.

Re: is there any way for Function to hold variables?

Posted: Sat Nov 19, 2022 9:27 am
by emeck
What about assigning the function to a variable? Something like this maybe (haven't tested the code, just an idea):

Code: Select all

p_myvar = Function(...) Local pars = arg.n Return({pars[0],pars[...],pars[n]}) EndFunction

result1 = p_myvar(5, 2)
result2 = p_myvar(8)
result3 = p_myvar(5, 2, 4)
You can also write functions to with fixed values only or use fixed values in case no parameters are given.

Edit:
Maybe this is better for different number of arguments:

Code: Select all

p_myvar = Function(...) Return(Unpack(arg)) EndFunction

Re: is there any way for Function to hold variables?

Posted: Sat Nov 19, 2022 12:57 pm
by Flinx
jPV wrote: Fri Nov 18, 2022 8:42 pm Lua doesn't have static variables, so I guess it's out of question for Hollywood too
Of course I don't know much about the way Hollywood generates Lua code, but that shouldn't be a problem. Such a "Local Static varname" only needed to be translated into a global variable that is only used in this function. For me the question is more whether this would be an improvement for Hollywood.

Re: is there any way for Function to hold variables?

Posted: Sun Nov 20, 2022 11:26 am
by Flinx
Hi Bugala,

If it's just a question of putting everything into the function, then perhaps the following structure might help:

Code: Select all

Function myfunc()
	If IsNil(myvar) Then myvar=5
	myvar=myvar+1
	DebugPrint(myvar)
EndFunction

For i=1 To 3
	myfunc()
Next
You then just have to use a safe name for myvar that can't collide with the rest of the program.

Re: is there any way for Function to hold variables?

Posted: Sun Nov 20, 2022 12:37 pm
by Bugala
@emeck, that could work, going to consider this one, although basically in this case I guess I will revert to @Flink idea.

Thanks @Flink, that seems like a very easy solution to this problem, although it still does leave that variable Global, which @Emecks solution would avoid.