| View previous topic :: View next topic |
| Author |
Message |
cgruber
Joined: 06 Sep 2005 Posts: 36
|
Posted: Tue Sep 06, 2005 9:31 pm Post subject: unset image variable |
|
|
How do you unload a loaded image in lua?
image = nil ? |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Tue Sep 06, 2005 11:35 pm Post subject: Re: unset image variable |
|
|
| cgruber wrote: | How do you unload a loaded image in lua?
image = nil ? |
You can't unload an image, Lua will free the memory, if you no longer reference it. "image=nil" may work, if you have no other references to it, but in most cases you don't need to care about, for example:
background=Image.load("level1.png")
....
some time later:
background=Image.load("level2.png")
As long as you don't copy a reference to the first image to some other variable, the leve1.png image object will be freed automaticly by Lua, when you load the "level2.png" and store the reference to this object to the background variable. |
|
| Back to top |
|
 |
cgruber
Joined: 06 Sep 2005 Posts: 36
|
Posted: Tue Sep 06, 2005 11:43 pm Post subject: |
|
|
| Ok, what about loading the image non globally in a function? |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Wed Sep 07, 2005 12:10 am Post subject: |
|
|
If you are using local variables, I think it will be freed, when the function ends and if you don't save the reference to another global variable:
| Code: |
function foo()
local img = Image.load("image.png")
-- do something with img
end
|
Without "local" the variable "img" will be accessable from the whole script and the image won't be freed, until you assign another value to "img", like "img=nil". |
|
| Back to top |
|
 |
cgruber
Joined: 06 Sep 2005 Posts: 36
|
Posted: Wed Sep 07, 2005 12:31 am Post subject: |
|
|
I'm doing both. I'm just anal I guess :)
Are you going to be continuning work on the Windows Lua Player? I think it's great and very well could be a nice gaming enviroment for making simple cross platform games as well. |
|
| Back to top |
|
 |
2Xtremes2004
Joined: 31 Aug 2005 Posts: 53
|
Posted: Tue Dec 13, 2005 4:17 am Post subject: |
|
|
| Shine wrote: | If you are using local variables, I think it will be freed, when the function ends and if you don't save the reference to another global variable:
| Code: |
function foo()
local img = Image.load("image.png")
-- do something with img
end
|
Without "local" the variable "img" will be accessable from the whole script and the image won't be freed, until you assign another value to "img", like "img=nil". |
When I use the above code as a ref. and add it to my code, the ms light constantly flashes. _________________ I want my money back!? |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Tue Dec 13, 2005 4:23 am Post subject: |
|
|
| 2Xtremes2004 wrote: | | When I use the above code as a ref. and add it to my code, the ms light constantly flashes. |
Yes, that's right, if you are calling the function multiple times, because the image is loaded all over again with every call. If you don't want this, you could use a global reference. If you don't understand what I mean, please read http://www.lua.org/pil/ because this is not Lua Player specific, but very basic Lua knowledge. |
|
| Back to top |
|
 |
Dr. Vegetable
Joined: 14 Nov 2005 Posts: 171 Location: Boston, Massachusetts
|
Posted: Tue Dec 13, 2005 5:05 am Post subject: |
|
|
| Just out of curiosity, does Lua Player use reference-counted "smart pointers", or is dead object cleanup done with garbage collection? |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Tue Dec 13, 2005 5:09 am Post subject: |
|
|
| Dr. Vegetable wrote: | | Just out of curiosity, does Lua Player use reference-counted "smart pointers", or is dead object cleanup done with garbage collection? |
see http://lua-users.org/wiki/GarbageCollectionTutorial |
|
| Back to top |
|
 |
2Xtremes2004
Joined: 31 Aug 2005 Posts: 53
|
Posted: Tue Dec 13, 2005 5:30 am Post subject: |
|
|
Thanks for your quick reply! As soon as I read your reply, I figured out my problem. I fell extremely stupid now. :-P
However I had only called the function once, and the light was constantly flashing. This was due to the most stupid error I had ever made while programing in Lua. :-)
I was doing this:
| Code: |
function MainTitle()
while true do
pad = Controls.read()
local BG = Image.load("Data/MBG.png")
local MM = Image.load("Data/MM.png")
screen:blit(0,0, BG)
screen:blit(0,0, MM)
if pad:start() then
break
end
screen.waitVblankStart()
screen.flip()
end
end
|
...Instead of this:
| Code: |
function MainTitle()
local BG = Image.load("Data/MBG.png")
local MM = Image.load("Data/MM.png")
while true do
pad = Controls.read()
screen:blit(0,0, BG)
screen:blit(0,0, MM)
if pad:start() then
break
end
screen.waitVblankStart()
screen.flip()
end
end
|
Go ahead, laugh it up! ;-) (I did)
One more question, where exactly would be the best place to insert BG=nil and MM=nill? _________________ I want my money back!? |
|
| Back to top |
|
 |
2Xtremes2004
Joined: 31 Aug 2005 Posts: 53
|
Posted: Tue Dec 13, 2005 11:17 am Post subject: |
|
|
I obviously dont have BG=nil and MM=nil in the right place, cause after the function is loaded about 7 times it gives an error. So I know that it is not collecting the garbage like I wanted it to. Any ideas? _________________ I want my money back!? |
|
| Back to top |
|
 |
|