2
\$\begingroup\$

At the moment, I'm working on a small game project using LUA and the love2d framework. Using this framework, I've made my own assets (i.e. button images, form images, etc), and using these assets I've been able to make a functional application, but not yet a game. In my project, I plan on having several scenes, and I'm not quite sure whether my method for scene management/switching is efficient/safe to use (safe, as in minimal bugs). So the process I'm using is as follows:

if scene_selector.active_scene == "splash" then
    if sceneSplash.loaded == false then
        sceneSplash.link.load()
        sceneSplash.link.update(dt)
        sceneSplash.link.draw()
        sceneSplash.loaded = true
    else
        sceneSplash.link.update(dt)
    end
elseif scene_selector.active_scene == "menu" then
    if sceneMainmenu.loaded == false then
        sceneMainmenu.link.load()
        sceneMainmenu.link.update(dt)
        sceneMainmenu.link.draw()
        sceneMainmenu.loaded = true
    else
        sceneMainmenu.link.update(dt)
    end
end

where scene_selector is a link to the follow file:

    -- Misc Vars
    active_scene = "splash"
    version = nil

    -- Resources
    cre = nil
    xp = nil

    -- End Vars

    -- Global Script Indicator
    calls = {}
    calls.active_scene = active_scene
    calls.version = version
    return calls

and sceneSplash/sceneMainMenu are links to lua files which act as the scenes. Each file manages its own updates, which are passed through by the main.lua - found by the love.update and love.draw functions which are passed through to the forms relevant load/update/draw functions. I'm aiming at using about 10 forms, 3 variable files and 2 misc function files (file management/encryption/IO operations/logging/etc).

So the TL;DR version is - is it efficient/safe (safe in terms of minimal bugs) to pass through draw and update functions from main.lua to other lua script files?

So far, the process works without any bugs, even when I go back to a previously accessed 'scene', so I assume the process will work 'safely' when I'm using more scenes.

\$\endgroup\$
0
\$\begingroup\$

To answer your question, it's perfectly safe and common to let other functions (in potentially other files) update or draw parts their responsible for (or everything if you'd like that). The important thing to keep in mind is that drawing things to the screen outside of the love.draw() function, or functions called from that one, will have no effect, because the screen is cleared before love.draw() is invoked in your main.lua script.

Now, as to your question about efficiency; all function calls have a minimal overhead. Calling functions from love.update() or love.draw() does not produce any extra overhead when compared to any other function call in Lua.

\$\endgroup\$
0
\$\begingroup\$

I'm also "playing" with love2d.

You can spare a bounch of if .. elseif if you use a generic scene like this :

if currentScene.loaded == false then
    currentScene.link.load()
currentScene.link.update(dt)
    currentScene.link.draw()
    currentScene.loaded = true
else
    currentScene.link.update(dt)
end

changing scene :

currentScene.unload() --if you need to releas resurces.. invoce gc .. ecc
currentScene= sceneMainmenu
currentScene.loaded = false
\$\endgroup\$
  • \$\begingroup\$ that's a good suggestion, but it doesn't really answer my question. \$\endgroup\$ – Bilfred Nov 24 '15 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.