| View previous topic :: View next topic |
| Author |
Message |
my psprecious
Joined: 17 Feb 2006 Posts: 24
|
Posted: Fri Feb 17, 2006 5:34 am Post subject: how does memory work with lua |
|
|
If I have a script that calls another script, does the first script get erase from the memory or is it still running, if it's still running can I stop it from running and keep some variables in memory (do I need to write them to a file before)
Hope it's clear, I'm new at lua since last friday, and I'm not a coder, I'm a gfx artist, but lua is really fun to learned.
If it's not clear just let me know I'll try to explain differently and the best I can
thanx all for your help |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Sat Feb 18, 2006 7:09 am Post subject: |
|
|
| LUA is call based. If you run another script.. the previous script is still loaded in active memory. |
|
| Back to top |
|
 |
fullerlee
Joined: 03 Nov 2005 Posts: 54
|
Posted: Sat Feb 18, 2006 7:28 am Post subject: |
|
|
This seems like a good place to ask about Garbage Collection in luaplayer.
- Are locals marked for GC as soon as they go out of scope?
- Will globals be marked for GC if we nil all references?
- When does GC run (every n secs, when mem usage reaches a threshold etc) ?
Lee |
|
| Back to top |
|
 |
my psprecious
Joined: 17 Feb 2006 Posts: 24
|
Posted: Sat Feb 18, 2006 8:30 am Post subject: |
|
|
| romero126 wrote: | | LUA is call based. If you run another script.. the previous script is still loaded in active memory. |
is there away to clear them from the memory, here's an example
I call a level for a game it call images and other stuff that are only used for that level, so when I get to another level is there a way to clear the previous one from memory? |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Sat Feb 18, 2006 9:27 am Post subject: |
|
|
Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format. |
|
| Back to top |
|
 |
my psprecious
Joined: 17 Feb 2006 Posts: 24
|
Posted: Sat Feb 18, 2006 11:52 am Post subject: |
|
|
| romero126 wrote: | Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format. |
Big thanx romero126
so if I understand this right if I called a png that takes lots of memory and then make it = nil it wil be out of the memory
thanx again, sorry if I sound a bit ignorant, just started lua last week and considering my knoledge of programming = nil, well Lua is super easy especially with help from people like you
Just tought of something, since the lowser is a script that start a script everytime we come out of a lua application or game and go back to the lowser the image of previous script stay in the memory (corect me if I'm wrong I'm just trying to understand) so is there a way to clear all on exit, or am I wrong and it does it. I ask this cause I was fooling around with the controls script tutorial from wiki.ps2dev.org and it happen so I wanted to be sure. |
|
| Back to top |
|
 |
indianajonesilm
Joined: 08 Feb 2005 Posts: 15
|
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sat Feb 18, 2006 7:17 pm Post subject: |
|
|
| romero126 wrote: | Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format. |
That's wrong. Take a look at this code:
| Code: |
foo=Image.load("test.png")
bar=foo
foo=nil
|
In bar is the same image object reference stored as in foo. When setting foo to nil, there is still a reference to this object in bar, so the image will not garbage collected. But when you set bar to nil, too, then the image will be collected.
Note that "foo" and "bar" in this example are global variables. When using local variables, it will be garbage collected when out of scope, but not in this example:
| Code: |
function test()
local foo = Image.load("test.png")
bar = foo
end
|
because bar is a global variable and the object reference for the image is stored to it, so after leaving the function scope, the image is still accessible with bar.
To answer the original question: If you call a script for every level, it is a good idea to use the same global variables for each level, e.g. "images={start=Image.load("start.png"), foo=Image.load("foo.png")}". Then the next level creates a new table for the images variable and the table for the first level and all images are not accessible any more (if not stored in another global variable) and they will be garbage collected.
And: If you call another script, the caller continues after the call instruction after the called scripted ends. You can call a script with dofile. But it is a good idea just to define some level variables and perhaps some special level functions and implement the main game loop in only one file. |
|
| Back to top |
|
 |
my psprecious
Joined: 17 Feb 2006 Posts: 24
|
Posted: Sat Feb 18, 2006 9:48 pm Post subject: |
|
|
Wow that definetly answers my question, thanx shine.
So you propose I do everything in one file instead of different files, now this brings another question , is there a file size limit |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sat Feb 18, 2006 11:07 pm Post subject: |
|
|
| my psprecious wrote: | | So you propose I do everything in one file instead of different files, now this brings another question , is there a file size limit |
There is no limit for the script file, only available memory. But you can use different files, e.g. one for each level, but I meant that you should not duplicate code, e.g. the function for checking the controls, but write these functions all in one file (or in more files, one file for each functionality, if it is large, because then it is easier for you to reuse it for other games). For example:
your-game-library.lua:
- general purpose side-scroller functions
- general purpose level handling functions
index.lua:
- main game loop with controller functions
- level loading algorithm
level1.lua:
- defines a level variable, e.g. with callback to level specific functions
level2.lua:
...
But I would start with one file and if you think it is getting to complex, then split it into multiple files. |
|
| Back to top |
|
 |
my psprecious
Joined: 17 Feb 2006 Posts: 24
|
Posted: Sun Feb 19, 2006 4:04 am Post subject: |
|
|
Shine I love your explanation, very clear and simple
you just made me understand exactly what function where for (sorry for my ignorance, started lua a week ago and for only background in programation basic from 20 years ago) |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Mon Feb 20, 2006 4:31 pm Post subject: |
|
|
| Shine wrote: | | romero126 wrote: | Variable = nil.
If a variable is nil then it is immidiatly called to the garbage collector and removed since varibles that dont exist give you a nil/null format. |
That's wrong. Take a look at this code:
| Code: |
foo=Image.load("test.png")
bar=foo
foo=nil
|
In bar is the same image object reference stored as in foo. When setting foo to nil, there is still a reference to this object in bar, so the image will not garbage collected. But when you set bar to nil, too, then the image will be collected.
Note that "foo" and "bar" in this example are global variables. When using local variables, it will be garbage collected when out of scope, but not in this example:
| Code: |
function test()
local foo = Image.load("test.png")
bar = foo
end
|
because bar is a global variable and the object reference for the image is stored to it, so after leaving the function scope, the image is still accessible with bar.
To answer the original question: If you call a script for every level, it is a good idea to use the same global variables for each level, e.g. "images={start=Image.load("start.png"), foo=Image.load("foo.png")}". Then the next level creates a new table for the images variable and the table for the first level and all images are not accessible any more (if not stored in another global variable) and they will be garbage collected.
And: If you call another script, the caller continues after the call instruction after the called scripted ends. You can call a script with dofile. But it is a good idea just to define some level variables and perhaps some special level functions and implement the main game loop in only one file. |
Please make code hideously more complicated in explination as it needs to be. Understanding that all variables are just pointers to a set of information makes what you said make absolute sense but at the same time. To a person new to the LUA codeing which possibly means new to programming in general makes what you said hideously complex.
Removing complexitys is needed when talking to people curious to know about data collection and variable pointer paths. |
|
| Back to top |
|
 |
my psprecious
Joined: 17 Feb 2006 Posts: 24
|
Posted: Wed Feb 22, 2006 3:37 pm Post subject: |
|
|
actually I am new to programming, but 22 years ago I use to program basic on ZX-81 and commodore 64
but I'm starting to understand again, still a few things I need to understand, but thanx to shine earlier I understand function |
|
| Back to top |
|
 |
|