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:

Re: Running a heavy lua script in Love

Post by msilvestro »

kikito wrote:Just out of curiosity: what does your script do, and why do you think it takes 10 seconds to run it?
The "heavy script" is an artificial intelligence based on MCTS (http://mcts.ai/). I give, as input, the number of iterations it must do (the more iteration the more accurate are the game theoretic values, so it's more similar to a minimax tree). If the iterations reach 10000, the script takes some seconds to return what move to do. So, I needed to display the move selection screen and, while the human player chooses what to do, in the background the AI algorithm should run without freezing everything. It works quite well putting the

Code: Select all

coroutine.yield()
after some iterations of the MCTS.
User avatar
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

Post by Jasoco »

Just make sure to not make the coroutine yield run too often because it can actually slow things down.

Think of it this way. Yielding once might actually take slightly more time than it would to do a few hundred or thousand loops of your for loop. So play around with the timer that counts down to the next yield. You might even get away with setting that loop counter pretty high. Or depending on your code, it might require a much lower number. Play around!
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:Just make sure to not make the coroutine yield run too often because it can actually slow things down.

Think of it this way. Yielding once might actually take slightly more time than it would to do a few hundred or thousand loops of your for loop. So play around with the timer that counts down to the next yield. You might even get away with setting that loop counter pretty high. Or depending on your code, it might require a much lower number. Play around!
Yes, I already played around a bit, since I noticed that it would become really slow if yielding too much.
Thanks a lot! You pointed me to the right direction!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 7 guests