forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

unset image variable

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
cgruber



Joined: 06 Sep 2005
Posts: 36

PostPosted: Tue Sep 06, 2005 9:31 pm    Post subject: unset image variable Reply with quote

How do you unload a loaded image in lua?

image = nil ?
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Tue Sep 06, 2005 11:35 pm    Post subject: Re: unset image variable Reply with quote

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
View user's profile Send private message
cgruber



Joined: 06 Sep 2005
Posts: 36

PostPosted: Tue Sep 06, 2005 11:43 pm    Post subject: Reply with quote

Ok, what about loading the image non globally in a function?
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Wed Sep 07, 2005 12:10 am    Post subject: Reply with quote

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
View user's profile Send private message
cgruber



Joined: 06 Sep 2005
Posts: 36

PostPosted: Wed Sep 07, 2005 12:31 am    Post subject: Reply with quote

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
View user's profile Send private message
2Xtremes2004



Joined: 31 Aug 2005
Posts: 53

PostPosted: Tue Dec 13, 2005 4:17 am    Post subject: Reply with quote

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
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Tue Dec 13, 2005 4:23 am    Post subject: Reply with quote

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
View user's profile Send private message
Dr. Vegetable



Joined: 14 Nov 2005
Posts: 171
Location: Boston, Massachusetts

PostPosted: Tue Dec 13, 2005 5:05 am    Post subject: Reply with quote

Just out of curiosity, does Lua Player use reference-counted "smart pointers", or is dead object cleanup done with garbage collection?
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Tue Dec 13, 2005 5:09 am    Post subject: Reply with quote

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
View user's profile Send private message
2Xtremes2004



Joined: 31 Aug 2005
Posts: 53

PostPosted: Tue Dec 13, 2005 5:30 am    Post subject: Reply with quote

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
View user's profile Send private message
2Xtremes2004



Joined: 31 Aug 2005
Posts: 53

PostPosted: Tue Dec 13, 2005 11:17 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group