@jalih
can I have a look at your code, for that is basically the same that I am aiming at here.
Any way to have Function save its Args for later use?
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Any way to have Function save its Args for later use?
No, why should n[1] be garbage collected? It will never be garbage collected until you set it to Nil.
Re: Any way to have Function save its Args for later use?
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?
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?
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Any way to have Function save its Args for later use?
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.
Re: Any way to have Function save its Args for later use?
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:
results in
"world"
"world"
working:
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?
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]()
"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]()
"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?
Re: Any way to have Function save its Args for later use?
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.
Re: Any way to have Function save its Args for later use?
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.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?
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Any way to have Function save its Args for later use?
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.
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.