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 »

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 »

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 »

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 »

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 »

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 »

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 »

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 »

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 »

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 »

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: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 4 guests