Running a heavy lua script in Love

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.
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Running a heavy lua script in Love

Post by msilvestro »

Hi everybody!
I'm working on a very important university project but I'm a bit confused about a question.
Basically, I've already written an AI script that selects the best move to do. It can take up to 10 seconds or even more.
The fact is, now I'm writing the game interactive interface in Love. My concern is, in which function do I have to run the script? In love.update or where? The game needs to stop until the AI decided, then move on.
I've not yet managed a good way of doing this. I hope to have been clear about my concern.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Running a heavy lua script in Love

Post by s-ol »

It would be a good idea to have the AI running in a different thread so that the program doesn't appear to crash while the AI is loading.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Re: Running a heavy lua script in Love

Post by msilvestro »

S0lll0s wrote:It would be a good idea to have the AI running in a different thread so that the program doesn't appear to crash while the AI is loading.
Great! Is there any tutorial on how to use them, since I've never heard anything about them?
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Running a heavy lua script in Love

Post by Jasoco »

You can also use coroutines as well if threads are too difficult. I use it for a modified A* pathfinding algorithm and it works wonderfully. It's all about figuring out where to put the resumes and structuring it properly.
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Re: Running a heavy lua script in Love

Post by msilvestro »

Jasoco wrote:You can also use coroutines as well if threads are too difficult. I use it for a modified A* pathfinding algorithm and it works wonderfully. It's all about figuring out where to put the resumes and structuring it properly.
Cool, I need something simple, so I suppose I can go with coroutines. The problem is, I'm finding very hard to understand how they works. Do I have to start a coroutine in the love.update and, also in love.update, check every time if the coroutine has ended and, if so, continue? I'm confused...
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Re: Running a heavy lua script in Love

Post by msilvestro »

I managed to use threads, but they have a severe limitation - I can't pass objects made in lua! Just flat tables! Why? I really need to pass objects, since the AI algorithm only works with objects representing games. What can I do? I never thought it would have been so difficult to run a parallel thread...

I think that making a separate lua file with a unique global variable to be shared between main.lua and aithread.lua could do the job, but if it's really how it is supposed to be I find it quite quirky. And coroutines seems even worse ):
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Running a heavy lua script in Love

Post by T-Bone »

msilvestro wrote:I managed to use threads, but they have a severe limitation - I can't pass objects made in lua! Just flat tables! Why? I really need to pass objects, since the AI algorithm only works with objects representing games. What can I do? I never thought it would have been so difficult to run a parallel thread...

I think that making a separate lua file with a unique global variable to be shared between main.lua and aithread.lua could do the job, but if it's really how it is supposed to be I find it quite quirky. And coroutines seems even worse ):
If translating your objects to a flat table is difficult, you probably need to rethink your structuring.

There are serialization libraries out there that can translate most lua tables to strings and then back. You could use one of those to transfer your data. But if you're transferring large amounts of data, this approach could be slow.
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Re: Running a heavy lua script in Love

Post by msilvestro »

T-Bone wrote:If translating your objects to a flat table is difficult, you probably need to rethink your structuring.
Probably you're right, even if I find a bit difficult to understand what exactly means "rethink your structuring". I know I'm quite a noob, can you point me some examples, if any? I need a clue of what to do...
T-Bone wrote:There are serialization libraries out there that can translate most lua tables to strings and then back. You could use one of those to transfer your data. But if you're transferring large amounts of data, this approach could be slow.
Thank you for the advice, I might try this!
And thank for the kind answer!
User avatar
msilvestro
Prole
Posts: 29
Joined: Tue Feb 25, 2014 11:15 pm
Location: Italy
Contact:

Re: Running a heavy lua script in Love

Post by msilvestro »

Nevermind, I managed to work it out.
Following the hint from Jasoco:
Jasoco wrote:You can also use coroutines as well if threads are too difficult.
I read well the chapter in PiL about coroutines and search a bit on the Internet and I made it. May try to make a little tutorial since, as for me, it's not very clear. I found coroutines to be very powerful, since all global variables are shared between them and that is a deal maker for me. About Love2D threads, I found them a bit complicated and limited, but maybe it's just me being a noob.

Anyway, thanks for pointing me in the right direction!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Running a heavy lua script in Love

Post by kikito »

Just out of curiosity: what does your script do, and why do you think it takes 10 seconds to run it?
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bobble68 and 2 guests