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.
Running a heavy lua script in Love
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- msilvestro
- Prole
- Posts: 29
- Joined: Tue Feb 25, 2014 11:15 pm
- Location: Italy
- Contact:
Re: Running a heavy lua script in Love
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.
- msilvestro
- Prole
- Posts: 29
- Joined: Tue Feb 25, 2014 11:15 pm
- Location: Italy
- Contact:
Re: Running a heavy lua script in Love
Great! Is there any tutorial on how to use them, since I've never heard anything about them?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.
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Running a heavy lua script in Love
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.
- msilvestro
- Prole
- Posts: 29
- Joined: Tue Feb 25, 2014 11:15 pm
- Location: Italy
- Contact:
Re: Running a heavy lua script in Love
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...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.
- msilvestro
- Prole
- Posts: 29
- Joined: Tue Feb 25, 2014 11:15 pm
- Location: Italy
- Contact:
Re: Running a heavy lua script in Love
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 ):
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
If translating your objects to a flat table is difficult, you probably need to rethink your structuring.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 ):
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.
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
- msilvestro
- Prole
- Posts: 29
- Joined: Tue Feb 25, 2014 11:15 pm
- Location: Italy
- Contact:
Re: Running a heavy lua script in Love
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:If translating your objects to a flat table is difficult, you probably need to rethink your structuring.
Thank you for the advice, I might try this!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.
And thank for the kind answer!
- msilvestro
- Prole
- Posts: 29
- Joined: Tue Feb 25, 2014 11:15 pm
- Location: Italy
- Contact:
Re: Running a heavy lua script in Love
Nevermind, I managed to work it out.
Following the hint from Jasoco:
Anyway, thanks for pointing me in the right direction!
Following the hint from Jasoco:
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.Jasoco wrote:You can also use coroutines as well if threads are too difficult.
Anyway, thanks for pointing me in the right direction!
- 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
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Semrush [Bot] and 6 guests