Memory Control?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
CryoNox
Prole
Posts: 18
Joined: Sun Jun 28, 2009 2:25 pm
Location: Singapore

Memory Control?

Post by CryoNox »

Hi, I'm new to LÖVE and I have some questions:

1) Is it possible to control when to load/unload resources in my game?
e.g. load game sprites when player enters the game state from the menu screen.

2) Is there an event callback when all resources have loaded?
e.g. Lets say I have a variable called "img" in my Actor class, and I want to set that value to a loaded image after all resources are loaded.
"img" is then used in the draw function.

Code: Select all

-- Actor Class --

-- Call Initialize when resources have loaded (Is this possible???)
function Actor:initialize()
	img = love_ball -- set the "img" variable to the loaded image
end

-- Draw Function
function Actor:draw()
	love.graphics.draw(img,self.x,self.y)
end
Thy father beckons.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Memory Control?

Post by Robin »

Remember, img is here a global variable. If you want some sort of object oriented approach, you will want to use self.img. (The self is implicit, for example: actor1:draw() is really Actor.draw(actor1))

EDIT: as for your questions, for that you place them in the function load():

Code: Select all

function load()
    actor1 = Actor.new() --or whatever function you use
    love_ball = love.graphics.newImage("ball.png")
    actor1:initialize()
end
So, you don't need callbacks for them.
Help us help you: attach a .love.
User avatar
CryoNox
Prole
Posts: 18
Joined: Sun Jun 28, 2009 2:25 pm
Location: Singapore

Re: Memory Control?

Post by CryoNox »

oh no wonder it couldn't work, I forgot to add the "self". Thanks Robin for your help :)

What about dynamic loading and unloading of resources then? (My 1st question)
Thy father beckons.
User avatar
Tenoch
Citizen
Posts: 76
Joined: Mon Jul 21, 2008 7:49 am

Re: Memory Control?

Post by Tenoch »

You can load images whenever you want, just be careful with the fact that for big images it can take some time (and pause the game loop for a while).

To "unload", simple remove all references to the image, and Lua's garbage collection will take care of it :

Code: Select all

self.img = nil
"When in doubt, use brute force." Ken Thompson
User avatar
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

Re: Memory Control?

Post by Zorbatron »

Unless you have a ridiculously large amount of images, I doubt you'd need to worry about unloading them. However, I guess it's good to have load screens when you switch between levels for a good base framework.
User avatar
CryoNox
Prole
Posts: 18
Joined: Sun Jun 28, 2009 2:25 pm
Location: Singapore

Re: Memory Control?

Post by CryoNox »

Zorbatron: Yup, I was thinking along the lines of loading screens between levels :)

Tenoch: Thanks so much for your reply :D
Thy father beckons.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 2 guests