Page 1 of 5

My ongoing newbie questions

Posted: Wed Jun 10, 2009 8:49 pm
by essell
Hello all,

I'm new to Love, and indeed programming in general. Just got into things last weekend as I'm wanting to learn Lua to help with future job applications etc (I'm an industry-experienced level designer).

I'm right at the start of making a basic side-scrolling shooter as my first project. It's coming along alright, but now and again I come up with these little questions that I need answers to, and I figured instead of starting threads for each of them, I'd post them all here. Here's my first question, and thanks in advance:

Q. Instead of keeping all my code in my main.lua file, what code do I use to refer to another file that I want to include (PHP style)?

Re: My ongoing newbie questions

Posted: Wed Jun 10, 2009 9:23 pm
by Jake
love.filesystem.include or love.filesystem.require will do the trick.

Re: My ongoing newbie questions

Posted: Wed Jun 10, 2009 9:30 pm
by essell
Yep it did - project is now neatly arranged into folders, and the code nicely categorised. Cheers :)

Q. How do I keep track of time in Lua?
(e.g. When player presses this button, do this, then after 2 seconds, stop)

Re: My ongoing newbie questions

Posted: Wed Jun 10, 2009 10:36 pm
by ljdp
love.timer.getTime()
will return the number of seconds your computer has been on since startup.
so if wan't something to happen in two seconds your looking for
love.timer.getTime() + 2
simply store that in a variable
nextaction = love.timer.getTime() + 2
then have a if statement such as
if nextaction and love.timer.getTime() > nextaction then
do this
end

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 5:32 am
by bartbes
Or something that a lot of us do, create a timer variable, and add dt to it every time you are in update, example:

Code: Select all

function load()
    --something
    timer = 0
    --some more
end

function update(dt)
    timer = timer + dt
    if timer >= 2 then
        --do stuff
    end
end

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 9:33 am
by essell
That second approach looks simpler and neater to me - will try it tonight. Cheers :)

Q. How would I make a some title screens to click through before the game starts?
I don't currently understand how to delay all the gamey stuff from happening whilst say, displaying a picture for a title screen...

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 10:46 am
by Robin
You can make a global variable that keeps track of the "state" of the game:

Code: Select all

function load()
    state = 'title'
    --load stuff here
end
-- rest of the game here

function draw()
    if state=='title' then
        -- display title
    elseif state=='game' then
        -- display game stuff
    end
end
Whenever you want to start with the game, just do state='game'. This can easily be extended to include, for example in-game menu's.

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 2:04 pm
by bartbes
The specific question was stopping game logic, which is just the same, but for the update function as well, another (similar) solution is:

Code: Select all

menu = {}
function menu:update(dt)
--my update stuff for the menu
end

function menu:draw()
--my draw stuff for the menu
end

game = {}
function game:update(dt)
--I guess, you should know the rest...

function load()
    curstate = menu
end

function update(dt)
    curstate:update(dt)
end

function draw()
    curstate:draw()
end
(possible for other callbacks as well, of course)

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 2:08 pm
by essell
I've just implemented a title screen according to Robin's suggestion - cheers Robin :)

Bartbes - can you briefly describe what your solution is, does, and how it works, etc.?

Re: My ongoing newbie questions

Posted: Thu Jun 11, 2009 3:55 pm
by bartbes
There is a table for every part of the game, in my example they are game and menu, they have their callbacks stashed in that table.
If you want to switch (or init) you assign one of these tables (AKA states) to curstate and call the corresponding callback in the global callbacks.
If this isn't vague...