I developed a two-player iPhone board game. Computer players (AI) can either be local (in the game code) or remote running on a server. In the 2nd case, both client and server code are coded in Lua. On the server the actual AI code is separate from the TCP socket code and coroutine code (which spawns a separate instance of AI for each connecting client).

I want to be able to further isolate the AI code so that that part can be a module coded by anyone in their language of choice. How can I do this? What tecniques/technology would enable communication between the Lua TCP socket/coroutine code and the AI module?

  • \$\begingroup\$ swig.org comes to mind.. \$\endgroup\$ – Jari Komppa Mar 15 '11 at 10:33
  • \$\begingroup\$ swig is c/c++ to other languages. Lua to other languages needs a different route. \$\endgroup\$ – David Young Mar 15 '11 at 17:17

A socket based messaging scheme can be used to allow any arbitrary language to interact with the AI. You could use binary based messages, xml, text, etc. Just clearly define what the message format is and what messages you will send and receive.

This technique works relatively well for light to modest communication handling. There is a latency penalty associated with it of course, but since your game is a board game style it doesn't have to run in real-time.

  • \$\begingroup\$ I'm already using sockets to get the AI's move from the AI server to the iOS client. Everything is in Lua. I want to know how an AI routine, coded in Python, for example (on the server) could communicate with the Lua socket routine (on the server). For example, the Python AI sends its move to the Lua socket routine which then uses the TCP socket to relay the move to the iOS client. \$\endgroup\$ – TokyoDan Mar 16 '11 at 0:38
  • \$\begingroup\$ Answered below but socket to socket communication can be used to communicate between two processes or programs on the same computer. In the same manner as two programs communicating over the internet. Except you don't need all that TCP/IP handling. \$\endgroup\$ – David Young Mar 16 '11 at 1:13

For any language X, you can't know what interop feature it does or does not have- no language is expected to provide any interoperation features. The most reliable is C-style, as that is (almost certainly) what your OS uses, although this doesn't mean that the language actually exposes it.

Perhaps the most independent would be using a named pipe provided by the OS and sending through say, XML messages. However, this is a tad extreme- most encapsulation schemes stop short of encapsulating the authoring language.

  • \$\begingroup\$ Pipes are a useful approach especially if you want something to test out quick and dirty. It isn't very scalable though and much slower than direct socket to socket communication. \$\endgroup\$ – David Young Mar 15 '11 at 22:54
  • \$\begingroup\$ Maybe I'm missing something. I'm new to this. I thought sockets were used to get info from one computer to another computer somewhere on the net. Can sockets also be used for communication between two programs/processes/coroutines running on the same computer? \$\endgroup\$ – TokyoDan Mar 16 '11 at 0:41
  • \$\begingroup\$ Yep! you are basically communicating over a localhost socket which won't actually route outside the ethernet card. With socket communication you could be talking to another program on the same computer or another program on a different computer. It's handled exactly the same. \$\endgroup\$ – David Young Mar 16 '11 at 1:10
  • \$\begingroup\$ Thanks. Even if this is possible the non-Lua AI routine would have to code socket communication routines. I want to get away from this. I just want the non-Lua AI to send a string like "Move P1 to X Y" to my Lua socket code on the server. The Lua socket code will relay the string to the iOS client. \$\endgroup\$ – TokyoDan Mar 16 '11 at 1:17
  • \$\begingroup\$ You might try using Pipes directly then. Let your server side Lua spawn a child process which is your non-Lua AI program. You can communicate with the child process through it's In and Out pipes. lua-users.org/wiki/ExtensionProposal \$\endgroup\$ – David Young Mar 16 '11 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.