how does memory work with lua
Moderators: Shine, Insert_witty_name
-
- Posts: 24
- Joined: Fri Feb 17, 2006 5:08 am
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
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
-
- Posts: 24
- Joined: Fri Feb 17, 2006 5:08 am
is there away to clear them from the memory, here's an exampleromero126 wrote:LUA is call based. If you run another script.. the previous script is still loaded in active memory.
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?
-
- Posts: 24
- Joined: Fri Feb 17, 2006 5:08 am
Big thanx romero126romero126 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.
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.
-
- Posts: 15
- Joined: Tue Feb 08, 2005 6:36 pm
Check out this old post by Shine where he explains about how he fixed the automatic Garbage Collection in luaplayer:
http://forums.ps2dev.org/viewtopic.php? ... 7341#24006
Also a Garbage Collection Tutorial can be found here:
http://lua-users.org/wiki/GarbageCollectionTutorial
Which explains the collectgarbage() function.
http://forums.ps2dev.org/viewtopic.php? ... 7341#24006
Also a Garbage Collection Tutorial can be found here:
http://lua-users.org/wiki/GarbageCollectionTutorial
Which explains the collectgarbage() function.
That's wrong. Take a look at this code: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.
Code: Select all
foo=Image.load("test.png")
bar=foo
foo=nil
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: Select all
function test()
local foo = Image.load("test.png")
bar = foo
end
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.
-
- Posts: 24
- Joined: Fri Feb 17, 2006 5:08 am
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: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
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.
-
- Posts: 24
- Joined: Fri Feb 17, 2006 5:08 am
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.Shine wrote:That's wrong. Take a look at this code: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.
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.Code: Select all
foo=Image.load("test.png") bar=foo foo=nil
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:
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.Code: Select all
function test() local foo = Image.load("test.png") bar = foo end
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.
Removing complexitys is needed when talking to people curious to know about data collection and variable pointer paths.
-
- Posts: 24
- Joined: Fri Feb 17, 2006 5:08 am