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«First 1 2 3 4 5 6 Next»
#101
02/18/2007 (9:08 am)
If you like TGE and python check out:

www.mmoworkshop.com/trac/mom Lots of interesting stuff going on. Just reading the boards and working through a few tutorials I have learned a TON.
Page«First 1 2 3 4 5 6 Next»