unset image variable

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
cgruber
Posts: 36
Joined: Tue Sep 06, 2005 6:38 am

unset image variable

Post by cgruber »

How do you unload a loaded image in lua?

image = nil ?
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: unset image variable

Post by Shine »

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.
cgruber
Posts: 36
Joined: Tue Sep 06, 2005 6:38 am

Post by cgruber »

Ok, what about loading the image non globally in a function?
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

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: Select all

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".
cgruber
Posts: 36
Joined: Tue Sep 06, 2005 6:38 am

Post by cgruber »

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.
2Xtremes2004
Posts: 53
Joined: Wed Aug 31, 2005 1:43 am

Post by 2Xtremes2004 »

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: Select all

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!?
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

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.
Dr. Vegetable
Posts: 171
Joined: Mon Nov 14, 2005 1:32 am
Location: Boston, Massachusetts
Contact:

Post by Dr. Vegetable »

Just out of curiosity, does Lua Player use reference-counted "smart pointers", or is dead object cleanup done with garbage collection?
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

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
2Xtremes2004
Posts: 53
Joined: Wed Aug 31, 2005 1:43 am

Post by 2Xtremes2004 »

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: 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
...Instead of this:

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
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!?
2Xtremes2004
Posts: 53
Joined: Wed Aug 31, 2005 1:43 am

Post by 2Xtremes2004 »

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!?
Post Reply