My ongoing newbie questions

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.
essell
Prole
Posts: 15
Joined: Tue Jun 09, 2009 6:26 pm
Location: Newcastle, UK
Contact:

My ongoing newbie questions

Post by essell » Wed Jun 10, 2009 8:49 pm

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)?

User avatar
Jake
Prole
Posts: 27
Joined: Sun Jul 20, 2008 2:01 pm

Re: My ongoing newbie questions

Post by Jake » Wed Jun 10, 2009 9:23 pm

love.filesystem.include or love.filesystem.require will do the trick.

essell
Prole
Posts: 15
Joined: Tue Jun 09, 2009 6:26 pm
Location: Newcastle, UK
Contact:

Re: My ongoing newbie questions

Post by essell » Wed Jun 10, 2009 9:30 pm

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)

User avatar
ljdp
Party member
Posts: 209
Joined: Sat Jan 03, 2009 1:04 pm
Contact:

Re: My ongoing newbie questions

Post by ljdp » Wed Jun 10, 2009 10:36 pm

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

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: My ongoing newbie questions

Post by bartbes » Thu Jun 11, 2009 5:32 am

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

essell
Prole
Posts: 15
Joined: Tue Jun 09, 2009 6:26 pm
Location: Newcastle, UK
Contact:

Re: My ongoing newbie questions

Post by essell » Thu Jun 11, 2009 9:33 am

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...

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: My ongoing newbie questions

Post by Robin » Thu Jun 11, 2009 10:46 am

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.
Help us help you: attach a .love.

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: My ongoing newbie questions

Post by bartbes » Thu Jun 11, 2009 2:04 pm

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)

essell
Prole
Posts: 15
Joined: Tue Jun 09, 2009 6:26 pm
Location: Newcastle, UK
Contact:

Re: My ongoing newbie questions

Post by essell » Thu Jun 11, 2009 2:08 pm

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.?

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: My ongoing newbie questions

Post by bartbes » Thu Jun 11, 2009 3:55 pm

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...

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests