3

I am a novice programmer in (Java/C++/C#) and I also know Python. I am trying to create a GameEngine in Java that can call Jython scripts, that have access to methods in the Java engine.

I am clueless as to how to approach this. I have already done weeks of research and nothing has answered my question ; that is:

How can I call methods in my Parent class, from my JythonScript, which is executed by my Parent class?

-----------------------------------UPDATE---------------------------------------------------

Okay, The answer here helped me understand some things, but it didn't solve my problem. What I was wondering if something such as this would work:

class MyJavaClass
{
 Public MyJavaClass()
 {
 PythonInterpreter interp = new PythonInterpreter;
 interp.execfile("MyJythonScript.py");
 interp.exec("InGameCommand");
 }
 public void SpawnPlayer()
 {}
 public void KillPlayer()
 {}
}

MyJythonScript.py

Def InGameCommand():
 SpawnPlayer()
 KillPlayer()

Is this even possible? There a way to do this?

-----------------------------------UPDATE---------------------------------------------------

Location to Jython: "C:\jython2.7a2\jython.jar" Location to my work: "C:\Documents and Settings\PC\Desktop\Jython*.java" Location to my local JtyhonJar: "C:\Documents and Settings\PC\Desktop\Jython\jython.jar"

my compiler I wrote: "@echo off" "javac -classpath C:\jython2.7a2\jython.jar *.java" "echo done" "pause>nul"

now it doesn't even compile... (I've changed little things in my code to see if it changed and it hasn't!)

tshepang
12.5k25 gold badges98 silver badges140 bronze badges
asked Jun 25, 2012 at 1:33
1
  • OH and if it helps clarify: Java class contains SpawnEntity() class Java class executs JythonScript Jython script Calls SpawnEntity() That's basically what I really want to accomplish ^ Commented Jun 25, 2012 at 5:41

2 Answers 2

1

Yes, this way is fine, but you can not run python script in constructor method, if so, it will be dead recursive at your code. please see the following code. you run PythonScriptTest class, it will run python script first, then python script will invoke PythonScriptTest.SpawnPlayer() method.

java code:

package com.xxx.jython;
import org.python.core.PyFunction;
import org.python.util.PythonInterpreter;
public class PythonScriptTest {
 public static void main(String[] args) {
 PythonScriptTest f = new PythonScriptTest();
 f.executePythonScript();
 }
 public PythonScriptTest(){
 }
 public void executePythonScript() {
 PythonInterpreter interpreter = new PythonInterpreter();
 interpreter.execfile("/home/XXX/XXX/util.py");
 PyFunction pyFuntion = (PyFunction) interpreter.get("InGameCommand", PyFunction.class);
 pyFuntion.__call__();
 }
 public void SpawnPlayer() {
 System.out.println("Run SpawnPlayer method ##################");
 }
}

Python scripts, named util.py:

import sys.path as path
# the following path is eclipse output class dir
# does not contain java class package path.
path.append("/home/XXX/XXX/Test/bin")
from com.xxx.jython import PythonScriptTest
def add(a, b): 
 return a + b 
def InGameCommand():
 myJava = PythonScriptTest()
 myJava.SpawnPlayer()
answered Jun 28, 2012 at 4:03
Sign up to request clarification or add additional context in comments.

3 Comments

oh okay :D thank you so much!I just need to get my paths to work and it is "AufWiedersehen!" to these problems! You rock ,sir.
My script is just: import engine def InGameCommand(): myJava = engine() myJava.SpawnPlayer() All the Java is doing is: PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("MyScript.py"); PyFunction pyFuntion = (PyFunction) interpreter.get("InGameCommand", PyFunction.class); pyFuntion.__call__(); and I get a TON of python errors when executed (compiles fine)
Oh and (I'm sorry, these are way to many questions) Is this supposed to work like instructions? So far all of this has worked for me. Except if I try to change variables defined in my Java class, It looks as though the jython is running a duplicate of the java instead of talking back...I may be doing this wrong but It is not 100% for me. X' to many questions I know
1

need to jython.jar

  1. execute python code in java.

    import org.python.util.PythonInterpreter; 
    public class PythonScript{ 
     public static void main(String args[]){ 
     PythonInterpreter interpreter = new PythonInterpreter(); 
     interpreter.exec("days=('One','Two','Three','Four'); "); 
     interpreter.exec("print days[1];"); 
     }
    } 
    
  2. invoke python script method in java.

    python script file, named test.py

    def add(a, b): 
     return a + b 
    

    java code:

    import org.python.core.PyFunction; 
    import org.python.core.PyInteger; 
    import org.python.core.PyObject; 
    import org.python.util.PythonInterpreter; 
    public class PythonScript { 
     public static void main(String args[]) { 
     PythonInterpreter interpreter = new PythonInterpreter(); 
     interpreter.execfile("/home/XXX/XXX/test.py"); 
     PyFunction pyFuntion = (PyFunction)interpreter.get("add",PyFunction.class); 
     int a = 10, b = 20 ; 
     PyObject pyobj = pyFuntion.__call__(new PyInteger(a), new PyInteger(b)); 
     System.out.println("result = " + pyobj.toString()); 
     }
    } 
    
  3. run python script in java

    python script file, named test.py:

    number=[1,10,4,30,7,8,40] 
    print number 
    number.sort() 
    print number 
    

    java code:

    import org.python.util.PythonInterpreter;
    public class FirstJavaScript {
     public static void main(String args[]) {
     PythonInterpreter interpreter = new PythonInterpreter();
     interpreter.execfile("/home/XXX/XXX/test.py");
     }
    }
    
answered Jun 25, 2012 at 2:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.