Slow AI

Hi! I’m still evaluating Defold for my projects - 2 of which are simple games (learning experiments really) that will probably be for the web and the last one being more elaborate and for Steam and possibly mobile (web is a possibility too) as I see things.

So far I have tried Flutter to run a 3D model using a component that renders the model in a tag inside a WebView widget. Results are somewhat ok but it’s a bit flaky at the same time - don’t worry, I know it’s technically not a great solution lol! I was drawn to Flutter for the multi-platforms aspect, and the fact that mixing UI and game was easy. Also I thought it was easy to integrate with all services offered by Firebase. Of course I could use Flame or something but I think using a real engine would be better if considering Flame at this point. Also I thought originally I would use some API s in Firebase that in the end won’t.

I saw Björn’s presentation on YouTube that he did 5 months ago and I found it made a good case for Defold. Since I am still experimenting with tools, I thought I would be more serious about Defold in my tests. I have a few questions though:

1- imagine a game such as Chess where your AI is slow. Or at least, an AI that spans multiple update cycles in the game loop. You still want to be responsive to user input as well as have the game display some animations or effects (say a clock, or a candle burning or whatever). I saw that there were no thread API in Lua. The only solution I saw was the use of the C API (dmThreads). My question is am I forced to use the C API to do threads? I think Löve2D has real threads. You can’t really do that with coroutines unless you yield after some time and do the AI computations in chunks but that’s hardly a nice solution.

2- is Lua performant enough to hold in memory a data structure comprising tables worth 5 to 15 MB of data for retrievals only? No insertions or removals here, just lookups. I would assume yes since it must be implemented as a simple hash map. I know this depends on the structure itself but generally, are tables a performance limitation when they are big?

I think Lua coroutines can be used to achieve what you are asking. If you really need to use proper threads then sure, dmThread could be used. There is an old example made by a forum member here: Experimental Threading extension - #12 by kiprono

I’m not sure if the extension is still working, but it could probably be made to work. Note that thread safety need to be adressed.

Yes, that is not a problem. Here’s an example where I load a 24MB JSON, parse it to a Lua table and print some of the values:

https://defold.com/codepad/#?c=#cp_sprite&s1=GYVwdgxgLglg9mABDMMoAoDOBTANsASgCgBIAczgDoBDVAW2qm3QCJKWAaRFgBzkzTwwlAJ6dEFSgAUAMgEEAmgCE5AYQDSAfRkB5HVM1SAkgDkA4lJ3muARgAMdrpICicgMqmz2064BKXACZiElw4CGpcRBAAJ0iAXm4ACygoHkwALgB6TOjqAHdKMjREkAAjEBxoiAQmMChKarpMgCtMBABaNGxcqDhozKZMKHaAE0ZqTIYh7szcamiybHbgGFxsSlaEFlIeaJQMFjNsFJQycRjcYOTUymjsAEcQbCH0C65D5wAVcVBIWAQsHhgFwYCMuHdMHwwDhgog4YhdvtWGY4FBEBCoThxBiEDgGnARthYfDQuFImMoNREAlNsJCdVCegcdD1syYaR4Qi9nVWAARbAM7AjFjEuHAPrIOI2Dj2OyIEZwDmcklhCKIABu1Pl4wA2jAALpK5WIcXRRAAaw4mpQCOoMGimHQ6oI8sVJGNxsRPMtGuIHuV2DAIyI/sDwZIYeIYaIRF+0CEJpQEUB+GIRGjsfA8YQUR4FOYOHwXBGUDTGbj/yQCE0dGemGoixTwMQtcw9cWmlBXFb7ewXBwQe6ZaDMYrCerKB4IAwheb1GzYE7YMQ88rw+Dmb+48Xd1C1BGTfXRCAA=

3 Likes

Hey thanks for the 24MB demo script! Doesn’t the json lib use C though? I might use JSON to store my data but it could be binary too to save space. In which case I would have to load and parse myself in Lua. I think it could work, performance wise.

For the threading question, what is the best way using coroutines? Separating the AI code in chunks and yielding frequently to let the game loop do its thing like I mentioned? The AI would essentially be tree or graph search, not so much numerical computations. I suppose yielding after we explore a certain number of nodes would work.

Yes, the JSON decoder is in C, but it returns a Lua table, which as you can see is not a problem for Lua to handle.

Yes, that would work.

Ok! Yes I see Lua tables can handle large amounts of data. Thanks again! I will redo my Flutter experiment in Defold. Will keep the board posted!

2 Likes