Python 2.2.1 Support for TGE
by Prairie Games · in General Discussion · 07/30/2002 (8:58 am) · 59 replies
Python 2.2.1 support is underway...
I have a Python interpreter embedded with output going to the Torque console. It is currently possible to create Torque objects from Python, and call member functions on them with arguments. Return values to Python also work. It is also possible to set datablocks from Python script :)
This is pretty much the basic functionality. What is left is all the stuff I am going to run into implementing beyond the basics :)
I am leveraging the Torque Scripting support. This means that you will expose to Python via the already existing methods. If I get tricky, I may add some new macros that wrap the Torque stuff. Regardless, this isn't a replacement for the current scripting stuff: TorqueScript<->C++<->Python
www.python.org
Vibes,
-J
I have a Python interpreter embedded with output going to the Torque console. It is currently possible to create Torque objects from Python, and call member functions on them with arguments. Return values to Python also work. It is also possible to set datablocks from Python script :)
This is pretty much the basic functionality. What is left is all the stuff I am going to run into implementing beyond the basics :)
I am leveraging the Torque Scripting support. This means that you will expose to Python via the already existing methods. If I get tricky, I may add some new macros that wrap the Torque stuff. Regardless, this isn't a replacement for the current scripting stuff: TorqueScript<->C++<->Python
www.python.org
Vibes,
-J
#2
07/30/2002 (9:50 am)
This isn't meant to be as offensive as it sounds, but why do you want to enable python in Torque, you can do everything you want in the code and existing scripting language.
#3
Python is a powerful and mature language. It is cross platfrom with some amazing libraries. It is free and open source. It is also highly popular.
On security, it has a restricted operation mode which provides security against harmful scripts.
You can learn about Python programming at your local bookstore. There is also a site dedicated to letting you know why Python is desireable. www.python.org
Mostly, I want to use it.
-J
07/30/2002 (10:05 am)
I am not here to evangelize Python. But, I'll take a moment:Python is a powerful and mature language. It is cross platfrom with some amazing libraries. It is free and open source. It is also highly popular.
On security, it has a restricted operation mode which provides security against harmful scripts.
You can learn about Python programming at your local bookstore. There is also a site dedicated to letting you know why Python is desireable. www.python.org
Mostly, I want to use it.
-J
#4
That a language is Turing-complete is not the same as that it is as useful in all areas as another language.
Torque script is great for doing game logic and getting data in and out of the engine. It's small, fast, and efficient.
Python is great for doing stuff that C++ can't readily do, and TorqueScript would choke on. Say I want to write some heavier duty AI code; Python would be better suited for that than C++ or TorqueScript. Python used as a sandbox would make it safe and robust to let clients download code from any server they come across... And since Python has more libraries than TorqueScript, it's trivial to add extensive functionality to your game without requiring people to get a new EXE.
But there will always be a place for C++ and TorqueScript; they both fill very important and crucial roles.
07/30/2002 (11:25 am)
Yup.That a language is Turing-complete is not the same as that it is as useful in all areas as another language.
Torque script is great for doing game logic and getting data in and out of the engine. It's small, fast, and efficient.
Python is great for doing stuff that C++ can't readily do, and TorqueScript would choke on. Say I want to write some heavier duty AI code; Python would be better suited for that than C++ or TorqueScript. Python used as a sandbox would make it safe and robust to let clients download code from any server they come across... And since Python has more libraries than TorqueScript, it's trivial to add extensive functionality to your game without requiring people to get a new EXE.
But there will always be a place for C++ and TorqueScript; they both fill very important and crucial roles.
#5
There's just something about the ability to pass code blocks as objects. :)
I really do need to become more familiar with python though. More and more people seam to be trying to use it.
08/01/2002 (7:28 am)
Personally, I like Ruby better.There's just something about the ability to pass code blocks as objects. :)
I really do need to become more familiar with python though. More and more people seam to be trying to use it.
#6
Coding this feels very natural. It's a good fit.
-J
08/01/2002 (9:27 am)
The Python support is going well. Having a system like the TGE console already in place helps the effort tremendously.Coding this feels very natural. It's a good fit.
-J
#7
08/01/2002 (2:48 pm)
Python support in torque would be really cool...great work!!
#8
(Ooh, I know, let's add brainfuck support! www.muppetlabs.com/~breadbox/bf/)
Edit: nm, just put this in somewhere. Too bad BF doesn't support calls... Won't really cause much game to happen ;)
(-- from www.catseye.mb.ca/esoteric/bf/)
08/01/2002 (2:52 pm)
Chances are that integrating Ruby would be dead easy, now that Torque already supports two scripting languages... Go to it!(Ooh, I know, let's add brainfuck support! www.muppetlabs.com/~breadbox/bf/)
Edit: nm, just put this in somewhere. Too bad BF doesn't support calls... Won't really cause much game to happen ;)
char m[9999],*n[99],*r=m,*p=m+5000,**s=n,d,c;main(){for(read(0,r,4000);c=*r;
r++)c-']'||(d>1||(r=*p?*s:(--s,r)),!d||d--),c-'['||d++||(*++s=r),d||(*p+=c==
'+',*p-=c=='-',p+=c=='>',p-=c=='<',c-'.'||write(2,p,1),c-','||read(2,p,1));}(-- from www.catseye.mb.ca/esoteric/bf/)
#9
The following Python code now works(including network play).. It is possible to lay out Python created game objects in the editor... pick them up... and use them... this Crossbow shoots 20 projectiles with a nice spread...
Of note... mixing of C++, TorqueScript, and Python! Classes, Functions, and Datablocks!!!!!
You can call .cs script from Python by calling evaluate("some script;") ... currently this is only necessary because the classes below use the .cs inventory system...
Python scripts are executable from .cs script by calling execPython("somepython.py")
Python can call into .cs and you can define .cs functions in Python... for instance... the onFire method below is called when you shoot the new PyCrossbow in game!!!! woot!!!!!
TGEExportFunction() in python acts a lot like the C++ addCommand() function... it exposes a function for .cs script use... again, most stuff will just be written in Python... and of course it isn't necessary to expose Python to itself :)
EDIT: Actually see my .plan for code snippet... it's a bit long for the thread.. it's a silly port of some crossbow stuff, just using it to get things working...
EDIT2: Automatic garbage collection from SimObject C++ destruction to Python (wrapper) class instance destruction is working... hooked it up via a DeleteNotify() function call... this is for anything (like a Datablock,Shapebase, etc) that exists in both languages... when the object is being deleted C++ side, Python DECREFs it's tied instance, if any... works very well...
Another note, I used the STL, this requires one change to the current Torque Source, which is to comment out the new declaration in the platform (for instance, in winMemory.cc)..
On Win32, Python support does require that Torque be compiled with Multithreaded DLL code generation... this is the way things should be for maximum compatibility anyway...
-=J=-
08/06/2002 (11:55 am)
Work continues...The following Python code now works(including network play).. It is possible to lay out Python created game objects in the editor... pick them up... and use them... this Crossbow shoots 20 projectiles with a nice spread...
Of note... mixing of C++, TorqueScript, and Python! Classes, Functions, and Datablocks!!!!!
You can call .cs script from Python by calling evaluate("some script;") ... currently this is only necessary because the classes below use the .cs inventory system...
Python scripts are executable from .cs script by calling execPython("somepython.py")
Python can call into .cs and you can define .cs functions in Python... for instance... the onFire method below is called when you shoot the new PyCrossbow in game!!!! woot!!!!!
TGEExportFunction() in python acts a lot like the C++ addCommand() function... it exposes a function for .cs script use... again, most stuff will just be written in Python... and of course it isn't necessary to expose Python to itself :)
EDIT: Actually see my .plan for code snippet... it's a bit long for the thread.. it's a silly port of some crossbow stuff, just using it to get things working...
EDIT2: Automatic garbage collection from SimObject C++ destruction to Python (wrapper) class instance destruction is working... hooked it up via a DeleteNotify() function call... this is for anything (like a Datablock,Shapebase, etc) that exists in both languages... when the object is being deleted C++ side, Python DECREFs it's tied instance, if any... works very well...
Another note, I used the STL, this requires one change to the current Torque Source, which is to comment out the new declaration in the platform (for instance, in winMemory.cc)..
On Win32, Python support does require that Torque be compiled with Multithreaded DLL code generation... this is the way things should be for maximum compatibility anyway...
-=J=-
#10
I've gone thru and cleaned up most quirks... a remaining one is having to explicitly call regsiterObject() on a Python created TGE instance... this is because the member variables have to be set before the object is registered... noodling on this...
It is very easy to expose C++, TorqueScript, and Python to one another... Python's balance between power and simplicity is really shining...
A fun thing is that most work now is centering around Python code... for instance, today I am making the GUI system scriptable with Python...which basically means writing (tiny) Python wrappers... I haven't had to touch C++ at all
The Torque console system has made it possible to add Python support with ZERO changes to the Torque source code... nice work on that stuff...
-J
08/08/2002 (11:09 am)
TGEPython is currently around 3000 lines of code... this is mixed between C++ and Python... I am applying an effort to make this a professional quality interface... I've gone thru and cleaned up most quirks... a remaining one is having to explicitly call regsiterObject() on a Python created TGE instance... this is because the member variables have to be set before the object is registered... noodling on this...
It is very easy to expose C++, TorqueScript, and Python to one another... Python's balance between power and simplicity is really shining...
A fun thing is that most work now is centering around Python code... for instance, today I am making the GUI system scriptable with Python...which basically means writing (tiny) Python wrappers... I haven't had to touch C++ at all
The Torque console system has made it possible to add Python support with ZERO changes to the Torque source code... nice work on that stuff...
-J
#11
08/08/2002 (11:45 am)
I am pretty psyched about this :)
#12
It's in the middle of a rewrite... learned a lot the first go around...
Hm... Quark and Blender both use Python... interesting :)
-J
10/01/2002 (10:14 pm)
Just letting interested people know I am still working on this... It's in the middle of a rewrite... learned a lot the first go around...
Hm... Quark and Blender both use Python... interesting :)
-J
#13
10/02/2002 (2:20 am)
You'll be my god if you get this working. The list datatype on it's own will make my life 100 times easier.
#14
The TestTrigger class launches the player straight up with some serious force and thus provides an example of messing with TGE from python...
Also notice that inheritance is completely supported in the derived TestTrigger2 class :)
Woot!
-J
10/02/2002 (4:50 pm)
Below is working Python code to create some TriggerData datablocks which can be applied to triggers in the mission editor... The TestTrigger class launches the player straight up with some serious force and thus provides an example of messing with TGE from python...
Also notice that inheritance is completely supported in the derived TestTrigger2 class :)
Woot!
-J
class TestTrigger(TriggerData):
def __init__(self,objectname="TestTrigger"):
TriggerData.__init__(self,objectname)
TGEExport(TestTrigger.onEnterTrigger,"TestTrigger","onEnterTrigger","poot",4,4)
TGEExport(TestTrigger.onLeaveTrigger,"TestTrigger","onLeaveTrigger","poot",4,4)
def onEnterTrigger(self,args):
obj=TGEObject(args[3])
obj.setVelocity("0 0 100")
print "entered TestTrigger muahhahaha"
def onLeaveTrigger(self,args):
print "leaving TestTrigger muahhahaha"
class TestTrigger2(TestTrigger):
def __init__(self,objectname="TestTrigger2"):
TestTrigger.__init__(self,objectname)
TGEExport(TestTrigger2.onEnterTrigger,"TestTrigger2","onEnterTrigger","poot",4,4)
TGEExport(TestTrigger2.onLeaveTrigger,"TestTrigger2","onLeaveTrigger","poot",4,4)
def onEnterTrigger(self,args):
TestTrigger.onEnterTrigger(self,args)
print "entered TestTrigger2 muahhahaha"
def onLeaveTrigger(self,args):
TestTrigger.onLeaveTrigger(self,args)
print "leaving TestTrigger2 muahhahaha"
#15
10/02/2002 (5:39 pm)
*Mouth Watering* :)
#16
The TGE <-> Python layer is small and efficient... it's a lot like making TGEScript functions in C++ ... as the latest test shows:
A trigger that applies damage to objects inside ...
This is representative of glue code (not bad looking for glue)... though, the idea is to code your game logic in Python, only calling into TGE when necessary...
A note: Anyone who is interested in simulation (should be anyone programming game logic?) check out Python 2.2 Generators... an interesting little code snippet is here simpy.sourceforge.net
-J
10/02/2002 (6:00 pm)
Heh...The TGE <-> Python layer is small and efficient... it's a lot like making TGEScript functions in C++ ... as the latest test shows:
A trigger that applies damage to objects inside ...
def onTickTrigger(self,args):
trigger=TGEObject(args[2])
numObjects=int(trigger.getNumObjects())
for x in range(0,numObjects):
obj=TGEObject(trig.getObject(x))
obj.applyDamage(1)This is representative of glue code (not bad looking for glue)... though, the idea is to code your game logic in Python, only calling into TGE when necessary...
A note: Anyone who is interested in simulation (should be anyone programming game logic?) check out Python 2.2 Generators... an interesting little code snippet is here simpy.sourceforge.net
-J
#17
#this sets velocity to "0 0 125" in a demonstrative manner
v=[0,0,100]
v[2]+=25
obj.setVelocity(v)
No more cheesy string representations of vectors/transforms .. Whee!
Any ListType derivative can be used... so about any vector/matrix library can be used...
-J
10/02/2002 (9:47 pm)
Check this subtle but powerful addition:#this sets velocity to "0 0 125" in a demonstrative manner
v=[0,0,100]
v[2]+=25
obj.setVelocity(v)
No more cheesy string representations of vectors/transforms .. Whee!
Any ListType derivative can be used... so about any vector/matrix library can be used...
-J
#18
10/02/2002 (10:06 pm)
Rock on! So when are the masses going to be able to play with this? :)
#19
I need to come up with a good Python framework yet... which partially means rectifying it with TGEScript/Native Code(and C++'s overlap with what could have been script)
I may rewrite the C++ one more time... but really it is pretty darned fast/minimal...
Probably will be awhile to a bit... how's that for noncommittal? :)
-J
10/02/2002 (10:25 pm)
Most of the core is done... The C++ code is like 20k ... the test Python framework I am using is less than 1kI need to come up with a good Python framework yet... which partially means rectifying it with TGEScript/Native Code(and C++'s overlap with what could have been script)
I may rewrite the C++ one more time... but really it is pretty darned fast/minimal...
Probably will be awhile to a bit... how's that for noncommittal? :)
-J
#20
10/02/2002 (10:55 pm)
No hurry really, just all these code snippets are getting me excited. Its worth taking the time to do it right.
Associate Ian Omroth Hardingham
Mode 7 Games