Page 1 of 2

Running a heavy lua script in Love

Posted: Fri Sep 11, 2015 5:35 pm
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.

Re: Running a heavy lua script in Love

Posted: Fri Sep 11, 2015 6:02 pm
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.

Re: Running a heavy lua script in Love

Posted: Fri Sep 11, 2015 6:07 pm
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?

Re: Running a heavy lua script in Love

Posted: Fri Sep 11, 2015 6:30 pm
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.

Re: Running a heavy lua script in Love

Posted: Sat Sep 12, 2015 8:38 pm
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...

Re: Running a heavy lua script in Love

Posted: Thu Sep 17, 2015 1:58 pm
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 ):

Re: Running a heavy lua script in Love

Posted: Thu Sep 17, 2015 6:30 pm
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.

Re: Running a heavy lua script in Love

Posted: Fri Sep 18, 2015 9:55 am
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!

Re: Running a heavy lua script in Love

Posted: Fri Sep 18, 2015 12:16 pm
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!

Re: Running a heavy lua script in Love

Posted: Fri Sep 18, 2015 12:55 pm
by kikito
Just out of curiosity: what does your script do, and why do you think it takes 10 seconds to run it?