Run function for _ seconds in love.update

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.
Post Reply
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Run function for _ seconds in love.update

Post by iPoisonxL »

Hello! I'm trying to make some cutscene tests and I've basically got a cutscene lua file that will run an action for X time. Except I can't make it run for a certain amount of time without having to put some timers in the main.lua. How could I keep all the timer code inside my cutscene.lua? Here's some of the cutscene code:

Code: Select all

function cs:doAction(action, actorIndex, duration, dt)
	self.actorTimers[actorIndex]=duration; --I have multiple "actors", that are objects with an X, Y and Speed proprety. They have an index in the actor table
	action(dt); --in this context it will call p.moveRight();
	if(self.actorTimers[actorIndex]>0)then
		self.actorTimers[actorIndex]=self.actorTimers[actorIndex]-dt;
		self:doAction(action, actorIndex, self.actorTimers[actorIndex], dt);
	end
end

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Run function for _ seconds in love.update

Post by vrld »

Does this code work? Because as far as I can tell you recurse by calling doAction until the timer reaches 0. All that happens in one frame. Without knowing your code, there should probably be two functions, one that adds an action and one that updates all actions, e.g.

Code: Select all

function cs:addAction(actorIndex, duration)
    self.actorTimers[actorIndex] = duration
end

function cs:doAction(action, actorIndex, dt)
    if self.actorTimers[actorIndex] and self.actorTimers[actorIndex] > 0 then
        self.actorTimers[actorIndex] = self.actorTimers[actorIndex] - dt
        action(dt)
    else
        self.actorTimers[actorIndex] = nil
    end
end
If you want to use third party libraries (and why wouldn't you?), hump.timer has a function for that: timer.do_for. In your case:

Code: Select all

Timer.do_for(duration, action)
Provided that you put this code into main.lua:

Code: Select all

Timer = require 'hump.timer'
function love.update(dt)
    Timer.update(dt)
    ...more update...
end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Run function for _ seconds in love.update

Post by iPoisonxL »

vrld wrote:Does this code work? Because as far as I can tell you recurse by calling doAction until the timer reaches 0. All that happens in one frame. Without knowing your code, there should probably be two functions, one that adds an action and one that updates all actions, e.g.

Code: Select all

function cs:addAction(actorIndex, duration)
    self.actorTimers[actorIndex] = duration
end

function cs:doAction(action, actorIndex, dt)
    if self.actorTimers[actorIndex] and self.actorTimers[actorIndex] > 0 then
        self.actorTimers[actorIndex] = self.actorTimers[actorIndex] - dt
        action(dt)
    else
        self.actorTimers[actorIndex] = nil
    end
end
If you want to use third party libraries (and why wouldn't you?), hump.timer has a function for that: timer.do_for. In your case:

Code: Select all

Timer.do_for(duration, action)
Provided that you put this code into main.lua:

Code: Select all

Timer = require 'hump.timer'
function love.update(dt)
    Timer.update(dt)
    ...more update...
end
No, the code doesn't work, I just get a stack overflow.

Thanks mate I will look into these. The reason I don't want to use third party libraries is I don't want to include any code that I haven't written myself. It's all about experience for me, if I do something by myself I know I can do it, you know? But then again, all of LOVE is kinda like a huge library then there's the lower level language all the way going back to 0's and 1's (or assembly).

Thanks!
Last edited by iPoisonxL on Tue Dec 16, 2014 8:45 pm, edited 1 time in total.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Run function for _ seconds in love.update

Post by davisdude »

I used to have that mentality. I don't want to derail this thread too much, but I will say a couple of things:
- If you want to make the libraries all yourself, go ahead. Nobody's stopping you. It's a good learning experience, plus you will know how things work, so it's easier to find internal errors (when you make them ;)).
- If you do use a library, make sure it's thoroughly tested and it fits your style. If it doesn't fit your style (i.e. under_scored vs. camelCased) then this will lead to lots of confusion. If you can't find one that fits your style, make your own.
- Making your own library isn't a bad thing. It just takes a lot of time to do all of them. Doing a couple of them can be really helpful, especially if the existing ones don't really make sense to you/work the way you want them to.

One example I should point to is my own default game template: I started working on it in the summer with almost no external libraries. Now, several months later, I've wiped most all of my custom files (save a few) and implemented libraries that are much faster and bug free than my own.

TLDR: If you want to make your own libraries, it can be very helpful, but not time-efficient or practical.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Run function for _ seconds in love.update

Post by iPoisonxL »

davisdude wrote:I used to have that mentality. I don't want to derail this thread too much, but I will say a couple of things:
- If you want to make the libraries all yourself, go ahead. Nobody's stopping you. It's a good learning experience, plus you will know how things work, so it's easier to find internal errors (when you make them ;)).
- If you do use a library, make sure it's thoroughly tested and it fits your style. If it doesn't fit your style (i.e. under_scored vs. camelCased) then this will lead to lots of confusion. If you can't find one that fits your style, make your own.
- Making your own library isn't a bad thing. It just takes a lot of time to do all of them. Doing a couple of them can be really helpful, especially if the existing ones don't really make sense to you/work the way you want them to.

One example I should point to is my own default game template: I started working on it in the summer with almost no external libraries. Now, several months later, I've wiped most all of my custom files (save a few) and implemented libraries that are much faster and bug free than my own.

TLDR: If you want to make your own libraries, it can be very helpful, but not time-efficient or practical.
Thanks mate. Yeah I'm still.. well I wouldn't say new to LOVE, I've been doing this for maybe a year, over a year? But I'm still learning. So yeah that's what I've been doing recently just mucking around with ideas not really making full games. But I have all these folders in my game programming folder that are just... tests, tests, tests, and unfinished projects. So I've got material to work with.

edit: nearly 2 years, actually

edit 2: woohoo! Success!
Image
Last edited by iPoisonxL on Tue Dec 16, 2014 10:01 pm, edited 1 time in total.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Run function for _ seconds in love.update

Post by iPoisonxL »

Sorry for double post but I've got a new issue, I don't think it fits as an edit. I don't think I should make a new thread either, it's very small and probably something so stupid. But here it goes basically it's saying that p is nil even though it's global, when I change any of the move functions to p.move*. Here's the .love (press space to see the error, space activates the cutscene)
cutscene test.love
(953 Bytes) Downloaded 58 times

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Run function for _ seconds in love.update

Post by Azhukar »

At line 3 you create your p table, then you add your functions to the table, then at line 23 you create ANOTHER p table that overwrites your old table.

Your naming scheme (g,p,cs) is also unmaintainable, it might look cool and short but after you come back to your project that has long ran out of letters you will be wondering what you were thinking.
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Run function for _ seconds in love.update

Post by iPoisonxL »

Azhukar wrote:At line 3 you create your p table, then you add your functions to the table, then at line 23 you create ANOTHER p table that overwrites your old table.

Your naming scheme (g,p,cs) is also unmaintainable, it might look cool and short but after you come back to your project that has long ran out of letters you will be wondering what you were thinking.
Thanks mate. This is just a test, that's why I don't bother with variable names. This isn't really going anywhere else than personal knowledge.

Also, how would I append a table with named variables? I can't use table.insert because it would place unnamed values into the table p.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Run function for _ seconds in love.update

Post by micha »

iPoisonxL wrote: Also, how would I append a table with named variables? I can't use table.insert because it would place unnamed values into the table p.
You don't need an append-functionality for that, simply do

Code: Select all

tablename[fiedname]=content
The only point of table.insert is to handle the numbering within the table for you. If you have named variables, then there are no numbers and you don't need append.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 53 guests