# How to get scripted programs governing game entities run in parallel with a game loop?

I recently discovered Crobot which is (briefly) a game where each player codes a virtual robot in a pseudo-C language. Each robot is then put in an arena where it fights against other robots.

A robots' source code has this shape :

/* Beginning file robot.r */
main()
{
while (1)
{
/* Do whatever you want */
...
move();
...
fire();
}
}
/* End file robot.r */


You can see that :

1. The code is totally independent from any library/include
2. Some predefined functions are available (move, fire, etc…)
3. The program has its own game loop, and consequently is not called every frame

My question is: How to achieve a similar result using scripted languages in collaboration with a C/C++ main program ?

I found a possible approach using Python, multi-threading and shared memory, although I am not sure yet that it is possible this way. TCP/IP seems a bit too complicated for this kind of application.

• Google for scripted languages that can be run from an existing application - Python, Lua and PascalScript come to mind. Jun 2, 2014 at 13:15
• @KromStern I thought the same, however, it appears Crobot is a compiler and virtual machine application. So, it appears it's compiling its own executables and then running them on a virtual machine? I'm not sure of the details. However Crobot did it, it's likely far more advanced than is necessary today, and a scripting language would be superior way to implement this now.
– House
Jun 2, 2014 at 13:19
• @Jim, do you actually care how Crobots did it? Or do you just want to know how you can implement something similar? Typically questions that are "how X did Y" are off topic here because only X can answer that. In this case, the source code is GPL, so you can find out exactly how Crobots did it on your own, but I suspect you'd like to know how you can do it on your own instead.
– House
Jun 2, 2014 at 13:22
• @Byte56, you're right, I was planning to try something similar. And as you say (and Krom Stern says), it might be easier to use scripting languages. I am still curious about Crobot's case, maybe I will investigate further but it does not seem to be the best way of doing things today.
– Jim
Jun 2, 2014 at 14:27
• @Jim Maybe you can modify the question to ask what you're actually interested in, or remove the question?
– House
Jun 2, 2014 at 15:50

A way to implement it is to use Python, threads and TCP/IP. What you need is :

1. A main application managing the game itself
2. A python thread for each bot
3. A python script to connect a bot to the main application (sending/receiving messages)

What you do not need (in this particular case) is :

1. Embed Python in the main application or vice versa
2. Mutex, semaphore or any other stuff preventing the memory to get corrupted (thanks to TCP/IP)

I can suggest the following design which worked for me :

Using this design, I came up with a very simple interface for the bots and a fairly playable game. A bot looks like :

# Beginning of file name_of_bot.py
def main(bot):
while True:
bot.move()
...
scanResult = bot.scan()
...
bot.rotate(15)
...
# End of file name_of_bot.py


...where you can do anything you want since it is a Python script (provided that you keep the main function's signature as it is) !

Update: For those interested, here is the link to the actual game on GitHub.

• As this answer is very specific, I am still open to accept a more generic (constructive) answer.
– Jim
Aug 11, 2015 at 15:32