Game Development Community

PyTGE (also PyTSE) - Python Bindings

by Prairie Games · in Torque Game Engine · 07/12/2006 (2:38 am) · 101 replies

I've had a number of requests for this so here it is sooner rather than later... Python bindings for TGE/TSE and probably TGB:

http://www.prairiegames.com/pytse10a.zip

It's called PyTSE, though there isn't any TSE specific source in it. So, it should build with no modifications with TGE 1.4

This hasn't been field tested, though it should be more or less bug free. Here's the text from the readme.txt, it's sparse I know. Time is a very limited commodity :|

Quote:This is a new TGE/TSE (and probably TGB) Python binding I have been working on. You can do with it what you like.

I don't have any time to document it. Though, the source file is only 16k, so it should be pretty clear.

I've also included a diff of source changes. They should be pretty close to the current HEAD revision.

You need to change your build target to a shared library (a dll on windows, also change to multithreaded dll code generation for this platform)

Here are some minimal docs in the forum of example usage, replace TSE with TGE if that is your desire:

[b]#--- TSE Python Module Example ---[/b]

[b]#TSE as a standard Python extension (no longer a executable)[/b]
import pytse

[b]#initialize pytse, this also executes main.cs and the .cs packages[/b]
pytse.initialize()

[b]#example of executing a script file[/b]
f = file("myscript.cs","rb")
script = f.read()
f.close()
pytse.evaluate(script)

[b]#or, just generate the cs code right inside Python![/b] 
pytse.evaluate("""
new GuiBitmapButtonCtrl(MyButton) {
 profile = "GuiButtonProfile";
 horizSizing = "right";
 vertSizing = "bottom";
 position = "404 361";
 extent = "285 85";
 minExtent = "8 2";
 visible = "1";
 text = "Button";
 groupNum = "-1";
 buttonType = "PushButton";
 bitmap = "./button";
 helpTag = "0";
};""")

[b]#it's easy to grab a reference to the button we created[/b]
button = TSEObject("MyButton")

[b]#buttons are kind of worthless without commands.  Let's make one:[/b]
def OnMyButton(value):
    print "Button pushed with value",value
    
[b]#export the function to the console system in much the same way the C++ system does...
#we also support optional namespaces, usage documentation, and min/max args[/b]
pytse.export(OnMyButton,"MyButton","OnButton","Example button command",1,1)

[b]#we can get and set fields (including dynamic fields).  We'll set our button's command:[/b]
button.command = "MyButton::OnButton(42);"

[b]#we can call console methods on our TSEObjects... So, let's simulate a button click.  
#the OnMyButton function will be called with the value 42 :)[/b]
button.performClick()

[i]#note that getting an object reference to the button and setting the command like this is 
purely for illustration. You can also: command = "MyButton::OnButton(42);" in the evaluated code.[/i]

[b]#moving on, we can get and set global variables[/b]
pytse.setglobal("$MyVariable",42)
print pytse.getglobal("$MyVariable")
pytse.evaluate('echo ("*** Here is your variable:" @ $MyVariable);')

[b]#the main loop is broken out and can be combined with other frameworks rather easily[/b]
while pytse.tick():
    pass

[b]#cleanup pytse.. goodbye![/b]
pytse.shutdown()


-Josh Ritter
Prairie Games, Inc
Page «Previous 1 2 3 4 5 6 Last »
#1
07/12/2006 (2:57 am)
Josh Ritter, You are an awesome torque Programmer.
Thanks alot for this, I'll definatly put it to use!
#2
07/12/2006 (4:29 am)
Indeed, thank you very much Josh. Amazing work!
#3
07/12/2006 (4:37 am)
Josh: Now I'm in love :-)

Got to try this out with Stackless!!!

Thanks for releaseing this.
#4
07/12/2006 (5:30 am)
Things to note so far:

Building on OSX, we want to change our platform:

//#include "platformWin32/platformWin32.h"
#include "platformMacCarb/platformMacCarb.h"

If you are building with the Python Framework linked into the project, change the Python.h headers to Python/Python.h

This might only be specific to the stock 1.4 distribution, but:

// #include "core/tDictionary.h"
#include "core/tagDictionary.h"

I wish I could debug this further, but I'm getting booted from the library here at Hokkaido University. Good luck to everyone!
#5
07/12/2006 (5:48 am)
Josh, thank you *so* much for releasing this. You've just saved me an awful lot of time and effort. :)
#6
07/12/2006 (12:01 pm)
@Everyone: You're welcome...

@Brian: There shouldn't be too much work to get this running on OSX. I recall doing some platform work last year when I got MoM running on OSX. Though, the specifics are foggy.

@Johan: I have my eye on Stackless (as used in EVE Online) for future development and MoM. I want to see what kind turn around they have on Python 2.5, which has some stuff I am pretty keen on.

-Josh Ritter
Prairie Games, Inc
#7
07/12/2006 (1:56 pm)
By the way, I ported this to TGB and it works great.
#8
07/12/2006 (2:35 pm)
I'm having problems compiling the pytse.ccp. (VS 7.1)

It doesn't seem to recognize this syntax:

static HashTable gCallableLookup;

Compiling...
pytse.cc
d:\Torque\PyTSE\engine\pytse\pytse.cc(30) : error C2143: syntax error : missing ';' before '<'
d:\Torque\PyTSE\engine\pytse\pytse.cc(225) : error C2275: 'StringTableEntry' : illegal use of this type as an expression
d:\Torque\PyTSE\engine\platform\types.h(32) : see declaration of 'StringTableEntry'
d:\Torque\PyTSE\engine\pytse\pytse.cc(225) : error C2059: syntax error : '>'
d:\Torque\PyTSE\engine\pytse\pytse.cc(225) : error C2039: 'Iterator' : is not a member of 'operator''global namespace'''
d:\Torque\PyTSE\engine\pytse\pytse.cc(226) : error C2275: 'StringTableEntry' : illegal use of this type as an expression
d:\Torque\PyTSE\engine\platform\types.h(32) : see declaration of 'StringTableEntry'
...
#9
07/12/2006 (4:35 pm)
@Johan - Yeah I was running into the same error last night under OSX with Xcode 2.1, haven't tracked down the problem yet. Maybe Josh can enlighten us?

I also noticed that linking in the MacCarb stuff directly wasn't a good idea, since it seems to re-override the 'new' operator (again) which causes problems.
#10
07/13/2006 (9:30 pm)
@Johan & @Brian

You need the TSE files for tagDictionary, the tDictionary code isn't what's needed. tagDictionary defines additional classes that are needed for it to work. I just got this working in TGB and needed those files. Fortunately, I'm a dev for all three.
#11
07/13/2006 (10:10 pm)
Oops. I thought everything needed would have been in TGE 1.4

Maybe someone can come up with an alternative mapping class.

-JR
#12
07/14/2006 (1:52 am)
J.E. & Josh:
Thanks guys.
Good for me I am a TSE owner :-)
#13
07/14/2006 (3:58 am)
Works like a charm.
I've run this with Python 2.4.2 Stackless 3.1b3 and it starts and runs the demo without any apparent problems.

I haven't actually tried to run any tasklets and channels yet, that have to way until the evening :-)
#14
07/17/2006 (9:10 am)
*screaming*

*still screaming*

*panting*

That's my way of saying "thank you." :) I bought TGE just to get this! But now I guess I have to justify the purchase and experiment with them both. Now it's time to make some more games....

@Jeremy: did you build TGB as a dll? If so, could you share the process with us? I'm not great with win32/OSX stuff; I am a "high level" kind of guy.

Oh bugger, do I need to buy TSE, too? Hrmmmm. There is a tagDictionary.h in the TGE distribution, but I guess it's different from the TSE version. Could that single file be shared with TGE owners without violating the EULA? Obviously, I'd be sh*tting my pants with glee to get this working. :)
#15
07/17/2006 (11:58 am)
Jason McIntosh:

Yes, the tagDictionary.h differs from the tDictionary.h. I don't think it's ok with GG to share it between TGE and TSE users. But we sure could ask them to include it in TGE.

Third option (what was the first ;-) would be what Josh mentioned above, to use another mapping implementation. I'm not a senior C++ programmer in any way, but it can't be that hard can it?
#16
07/17/2006 (12:55 pm)
Thanks for the reply, Johan. I have tagDictionary.h and tagDictionary.cc in my TGE 1.4 source. There is no tDictionary.h file. That's why I assumed the differences must be minimal.

If it's just a hash map implementation then it would be pretty easy to replace. But I'm not familiar with TGE's guts at all or whether or not it involves anything TGE-specific. So I'm not sure what would be required.
#17
07/17/2006 (3:00 pm)
The different is major, as far as I understand.

I think any map implementation would do, correct me if I'm wrong (josh) , but the mapping is just a keyword/value mapping (with a interator) as far as I can see!?
#18
07/17/2006 (3:24 pm)
Yeah, I'll either add a hashmap implementation (why oh why can't we have good STL support in TGE?) or just buy TSE. It's only another $150. Thanks for suggestions!
#19
07/19/2006 (1:46 pm)
I bought TSE and got everything working. I'm kind of numb from the shock of it, though. (It's a good kind of shock. Not that I doubted it would work, just that Python and a great game engine together is [was!] something of a dream to me.)

Just wanted to say thanks again, Mr. Josh Ritter, sir, for so generously sharing this with everyone! If I can return the favor some day, I intend to. *big hug* As a small payment, I'll give you all the games I make with pytse. :)
#20
07/19/2006 (7:32 pm)
Ok, sorry, nevermind, nothing to see here. Move along. *blush* Figured it out.
Page «Previous 1 2 3 4 5 6 Last »