Doing stuff on exit

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Doing stuff on exit

Post by kalle2990 »

Okay, I did a quick test for everyone who wants to save stuff on exit. It calls a function (love.quit) when the game shuts down, where the user can call their save function. To improve this I will create a function for saving variables easily using a technology I found in the Android syntax. It creates a bundle with all values using keys, for example bundle.putNumber("A number value", 42). And for reading, bundle.getNumber("A number value"). I might make it auto detect the type, this was just an example. The script will be used together with this to make it easier for other developers to save game states.

Here's the code, one line in the love.run is changed, at the event iterator:

Code: Select all

function love.quit()
end

function love.run()
 
    if love.load then love.load(arg) end
 
    local dt = 0
 
    -- Main loop time.
    while true do
        if love.timer then
            love.timer.step()
            dt = love.timer.getDelta()
        end
        if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
        if love.graphics then
            love.graphics.clear()
            if love.draw then love.draw() end
        end
 
        -- Process events.
        if love.event then
            for e,a,b,c in love.event.poll() do
                if e == "q" then
                    if love.audio then
                        love.audio.stop()
                    end
					     love.quit() --Changed line!!
                    return
                end
                love.handlers[e](a,b,c)
            end
        end
 
        if love.timer then love.timer.sleep(1) end
        if love.graphics then love.graphics.present() end
 
    end
 
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Doing stuff on exit

Post by Robin »

Seems interesting, although you could remove the definition of love.quit and change one line to:

Code: Select all

                    if love.quit then love.quit() end --Changed line!!
Also, such a "bundle" can be implemented using tables and metatables:

Code: Select all

bundle["A number value"] = 42
print(bundle["A number value"])
For that to work, you implement the bundle metatable's __index and __newindex fields as getValue and putValue functions.
Help us help you: attach a .love.
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Doing stuff on exit

Post by kalle2990 »

1. Yes, I just used is a reference.
2. That saves it to a table, but I was thinking to add something to save the values to a file after. When the game is started again the values can be restored using the values from the read file ^^
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Doing stuff on exit

Post by Robin »

kalle2990 wrote:2. That saves it to a table, but I was thinking to add something to save the values to a file after. When the game is started again the values can be restored using the values from the read file ^^
I wasn't clear.

Do you know how to use metatables?

You can use the metamethods __index and __newindex to implement such behaviour.
Help us help you: attach a .love.
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: Doing stuff on exit

Post by kalle2990 »

I know how to use metatables, they have different stuff for doing things with them. I've been using them on my GUI which I stopped working on atm, it would need some code optimizations... :roll:
Post Reply

Who is online

Users browsing this forum: No registered users and 49 guests