1

I'm trying to create the nim game in unity with some nice 3d graphics. For those who don't know, nim is a solved game which means that, assuming both players play perfectly, we can know who will win at the first turn.

I want to make 4 game modes:

  1. Two players who play against each other.
  2. The player plays against the computer who plays perfectly.
  3. The computer plays against a bot the user wrote in python.
  4. The player plays against a bot he wrote in python.

I learnt about this game in computer science class in high school, and we were given an exercise to write a program that will win a simpler version of this game.

That's why I want to include the last two modes: To let students write their bots in python and test it in my game. I think it can turn out very cool.

However, that means I need to figure out how to run a python script from c# script in unity. I know I can use Process in c# to run the external python script, but that requires me to know the location of the python executable.

I could make a settings menu, so the user can set the path to python there, but I don't like the fact that the user will have to deal with path settings.

I learnt that, on windows, I can download an embeddable zip which contains the python interpreter (python.exe) and simply ship this with the game so the user doesn't even need to have python installed on their machine. However, I couldn't find any similar zip for other platforms, specifically linux and mac.

So, my question is how should I run python scripts in unity? Is there a way to embed the python interpreter for both windows, mac and linux, or should I make a settings menu for configuring the python path and use processes? If somebody can give me an idea for how to use python in unity, I'd really appreciate this.

Note: I want to use python 3 for the scripting, so solutions that work for python 2 only can't help me at all.

Thanks in advance.

1
  • Hey shai avr did you find a solution to this? One problem is that if unity already has an interpreter that it includes in a build, it will be subject to unit'ys license agreement so can't use it for player editable scripts Commented Apr 27, 2021 at 8:03

2 Answers 2

1

You can use System.Diagnostics.Process.Start and start cmd with it

string strCmdText;
strCmdText= "python script.py";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);```
Sign up to request clarification or add additional context in comments.

2 Comments

That only works for windows. Also, what would happen if the user doesn't have python in his PATH system variable
Didn't even come to think about that. Maybe a check could be implemented for windows, mac and linux to compare the OS. Now there's just needed a solution for mac and linux
0

Only python 2.7.5, python 3 is not functional

Python and Unity

but if you just want to use a python as Bot, i suggest you to communicate via TCP (socket) or others ways...

5 Comments

Not good. I can't use python 2. Also I don't need to access unity classes in python, so maybe there is another solution
just fyi, C# could only execute Python version 2.7... So if C# cant do in Python 3, unity cant...
yes you could launch process, its not the same thing than an embedded python (IronPython for example) inside c#.. but for me its better to communicate via socket for example.. or use MemoryShared if python does this functionality
Can you give an example or reference to how it can be done? I don't want to set a server for that. I'd prefer the game to not require internet connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.