4
\$\begingroup\$

I am studying some chess and others AI algorithms and the majority of these implementations are made in C and C++, my question is, if I make a chess board and all the graphic thing in Unity, can I make it run with my algorithm written in C++? And how can I make this interface?

Sorry if is it a noob question, I'm just starting at this whole gamedev world.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Translating a pure algorithm from one programming language to another is usually not that difficult. The essential differences between programming language usually appear on a higher, more architectural layer. \$\endgroup\$
    – Philipp
    Jul 24, 2014 at 19:02
  • 1
    \$\begingroup\$ But, considering that I have a fully-functional chess board in Unity, with all the movements already implemented and a fully-functional chess algorithm with all the "in's and out's" working by the international chess official notation, can I easily merge them to work together, I mean, without the necessity of reading and rewriting the algorithm in a C# script. \$\endgroup\$
    – Mauk
    Jul 24, 2014 at 19:11
  • 2
    \$\begingroup\$ Yes you can: docs.unity3d.com/Manual/Plugins.html It's a Unity Pro only feature though. \$\endgroup\$
    – bummzack
    Jul 24, 2014 at 19:31
  • 1
    \$\begingroup\$ C++ and C# are very similar languages. You'll learn quite a bit about both languages and about the algorithm if you do the translation manually. \$\endgroup\$ Jul 24, 2014 at 20:16

2 Answers 2

6
\$\begingroup\$

It might be silly, but you could make a small C++ program around the chess library, that takes a simple text board state or movelist as input and returns the AI's selected move. I don't know .net's system libraries off hand, but you can probably start it as a subprocess, send to its stdin and then poll its stdout for a reply. Local sockets are also a possibility..

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Not silly at all. This is a very clean solution if you don't want or can touch the c++ code. You also don't have to debug your c# port of the algorithm. \$\endgroup\$ Jul 25, 2014 at 2:26
  • \$\begingroup\$ Yeah, that's what I was looking for, thank you for the answer, I will search for more informations about how to do this now that I know that it is possible. \$\endgroup\$
    – Mauk
    Jul 25, 2014 at 14:48
3
\$\begingroup\$

There are a variety of ways to run code written in other languages in Unity. Most of these are platform specific.

iOS: Since xCode will also compile C++ code, you could add the C++ code to the plugins directory and it will be built when you build the iOS app. You'd then define the function prototypes in C# so they are accessible from Unity. Documentation is here.

The official documentation for a variety of platforms is covered here.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .