Any way to have Function save its Args for later use?

Discuss any general programming issues here
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

@jalih

can I have a look at your code, for that is basically the same that I am aiming at here.
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Any way to have Function save its Args for later use?

Post by airsoftsoftwair »

Bugala wrote: Sat Dec 26, 2020 11:42 am Can you confirm if this is actually a safe way of doing something like this, or if program is long is it possible that after for example couple of minutes the garbage collection comes and removes n[1] prt state away?
No, why should n[1] be garbage collected? It will never be garbage collected until you set it to Nil.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

Yes, n[1] won't be garbage collected. But what about the PRT variable I am using inside it?

at the time I am declaring n[1], PRT is a local variable with "hello" inside it, but will it still be hello if I use it somewhere else in the program a thousand lines later?
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Any way to have Function save its Args for later use?

Post by airsoftsoftwair »

Well, the "Local" declaration doesn't really make sense to me here because it's not inside a function. Why would you want to use "Local" outside a function in the first place? Doesn't make sense to me at all.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to have Function save its Args for later use?

Post by Bugala »

Only Reason why I am using Local in that is since it seems to be working.

Point is that I want to have function where I can decide their ARGs already in advance.

That there is:
MyFunc(Arg1, Arg2)

And then I want to save it in to a table funcstable so that those Arg1 and Arg2 have already been decided before, in way of:

funcstable[1] = MyFunc(1, 2)
funcstable[2] = MyFunc(3, 4)
funcstable[3] = MyFunc(8, 3)

So when I later then call for example funcstable[2](), what I want to happen is that code executes MyFunc(3,4).

And as I was looking at how to get this done, it seems using Local was doing exactly that.

See here non-working and working example:

Not working:

Code: Select all

n = {}

prt = "hello"
n[1] = Function() DebugPrint(prt) EndFunction

prt = "world"
n[2] = Function() DebugPrint(prt) EndFunction
	

n[1]()
n[2]()
results in
"world"
"world"


working:

Code: Select all

n = {}

Local prt = "hello"
n[1] = Function() DebugPrint(prt) EndFunction

Local prt = "world"
n[2] = Function() DebugPrint(prt) EndFunction
	

n[1]()
n[2]()
results in :
"hello"
"world"

so that is why I am using local, not otherwise interested in using local. And this is only way I have so far figured out how to achieve this.

But question is, will n[1]() and n[2]() keep printing "hello" and "world" or might garbage collection or something affect them after thousand other lines maybe making it starting to debugprint NIL?
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Any way to have Function save its Args for later use?

Post by jalih »

Bugala wrote: Sun Dec 27, 2020 9:39 pm @jalih

can I have a look at your code, for that is basically the same that I am aiming at here.
Sorry for a late reply... My code is written in 8th, Forth like programming language. It can be a bit hard to follow, If you don't have experience with stack based concanative programming languages.

Here is a nice tutorial demonstrating some tile based game concepts.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Any way to have Function save its Args for later use?

Post by jalih »

Bugala wrote: Wed Jan 06, 2021 4:04 pm so that is why I am using local, not otherwise interested in using local. And this is only way I have so far figured out how to achieve this.

But question is, will n[1]() and n[2]() keep printing "hello" and "world" or might garbage collection or something affect them after thousand other lines maybe making it starting to debugprint NIL?
If you don't change the contents for a table index or set it to Nil, I see no reason why it would get garbage collected.
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Any way to have Function save its Args for later use?

Post by airsoftsoftwair »

Bugala wrote: Wed Jan 06, 2021 4:04 pm And as I was looking at how to get this done, it seems using Local was doing exactly that.
Ok, true. By doing it this way you'll get actually two different local variables named "prt". This is possible but of course it's a rather awkwar construction.
Bugala wrote: Wed Jan 06, 2021 4:04 pm But question is, will n[1]() and n[2]() keep printing "hello" and "world" or might garbage collection or something affect them after thousand other lines maybe making it starting to debugprint NIL?
No, they won't be garbage collected while there are still references. n[1] will reference the first "prt" local and n[2] will reference the second "prt" local so both won't be garbage collected. You can easily try this by manually forcing a garbage collecting cycle using CollectGarbage().

Btw, you should never worry about the garbage collector. It's totally impossible for it to collect anything that is still referenced.... unless there is a bug of course but that's rather unlikely because Hollywood uses the Lua logic here.
Post Reply