unset image variable
Moderators: Shine, Insert_witty_name
unset image variable
How do you unload a loaded image in lua?
image = nil ?
image = nil ?
Re: unset image variable
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:cgruber wrote:How do you unload a loaded image in lua?
image = nil ?
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.
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:
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".
Code: Select all
function foo()
local img = Image.load("image.png")
-- do something with img
end
-
- Posts: 53
- Joined: Wed Aug 31, 2005 1:43 am
When I use the above code as a ref. and add it to my code, the ms light constantly flashes.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:
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".Code: Select all
function foo() local img = Image.load("image.png") -- do something with img end
I want my money back!?
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.2Xtremes2004 wrote:When I use the above code as a ref. and add it to my code, the ms light constantly flashes.
-
- Posts: 171
- Joined: Mon Nov 14, 2005 1:32 am
- Location: Boston, Massachusetts
- Contact:
see http://lua-users.org/wiki/GarbageCollectionTutorialDr. Vegetable wrote:Just out of curiosity, does Lua Player use reference-counted "smart pointers", or is dead object cleanup done with garbage collection?
-
- Posts: 53
- Joined: Wed Aug 31, 2005 1:43 am
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:
...Instead of this:
Go ahead, laugh it up! ;-) (I did)
One more question, where exactly would be the best place to insert BG=nil and MM=nill?
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: Select all
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
Code: Select all
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
One more question, where exactly would be the best place to insert BG=nil and MM=nill?
I want my money back!?
-
- Posts: 53
- Joined: Wed Aug 31, 2005 1:43 am