Python and T2D
by Jason McIntosh · in Torque Game Builder · 09/07/2005 (9:25 pm) · 16 replies
Hello Python fans!
Just wanted to say that the PyT2D binding is working, and I didn't do jack to get it into that state (to my pleasant surprise). It was already very complete. Humongo-normous thanks to Josh Ritter and Tomas Dahle. The only code I did was to create a function in TorqueScript which will play a sound with optional volume:
The other thing that was a show-stopper for me was the inability to schedule events attached to an object, like this:
So I'll regard this thread as a Q&A for PyT2D if you have questions about how to use it. I'm still learning, too, as I just now got to a point where I can start to create the technology for my next game. The docs that Tomas wrote are very slim, so maybe this thread will help others struggling to get the syntax right for what you want to accomplish. At least I can contribute that way. :)
Again, thanks and bravo to Ritter and Dahle for this great resource.
Just wanted to say that the PyT2D binding is working, and I didn't do jack to get it into that state (to my pleasant surprise). It was already very complete. Humongo-normous thanks to Josh Ritter and Tomas Dahle. The only code I did was to create a function in TorqueScript which will play a sound with optional volume:
function playSound( %datablock )
{
%handle = alxPlay( %datablock );
if ( %datablock.volume !$= "" )
alxSourcef( %handle, "AL_GAIN", %datablock.volume );
else
alxSourcef( %handle, "AL_GAIN", 1.0 );
return %handle;
}So to use sounds with PyT2D, you just need to define your audio datablocks in TorqueScript (I do this anyway as I don't see any benefit of doing it in Python and since I am a "get straight to the game" kind of person, I didn't bother looking into it more than that). Then from Python you just call:TAP.playSound( "mySound" )...and the sound plays with the optional volume field from the datablock.
The other thing that was a show-stopper for me was the inability to schedule events attached to an object, like this:
%myobj.schedule( 1000, sayHello, "yo!" );The cool thing about events scheduled this way is that they automatically cancel if the object is deleted. I use this a whole lot. Turns out it was only a syntax issue and once trial and error prevailed, I found out that:
myPyObj.schedule( 1000, "sayHello", "yo!" )...was all I needed!
So I'll regard this thread as a Q&A for PyT2D if you have questions about how to use it. I'm still learning, too, as I just now got to a point where I can start to create the technology for my next game. The docs that Tomas wrote are very slim, so maybe this thread will help others struggling to get the syntax right for what you want to accomplish. At least I can contribute that way. :)
Again, thanks and bravo to Ritter and Dahle for this great resource.
About the author
#2
This does prove annoying, though, because I can't submit the resource with any code examples in it.
09/09/2005 (9:11 am)
I think if you only distribute the .pyc files (compiled bytecode) you'll be okay. Josh/GG, could we get a confirmation on that, please?This does prove annoying, though, because I can't submit the resource with any code examples in it.
#3
And yes, you can distribute compiled files only, regardless of the language used.
Nice work Jason! This is cool.
09/12/2005 (1:23 am)
Jason, you can submit a T2D specific release and we can make it so that only T2D licensees can have access to it. That's how it's possible to have any T2D resources out there that cover scripting, etc. And yes, you can distribute compiled files only, regardless of the language used.
Nice work Jason! This is cool.
#4
Another question, if I may. Did anybody do performance comparison between TS and Python? I'm sure TS is faster than Python, but by how much? Would it be worthwhile to develop a fast-paced game with Python, or should it be kept for puzzles/card/etc games?
(Yes, I could do this myself, but I'm busy... :()
09/12/2005 (3:49 am)
Thanks for the clarification Josh.Another question, if I may. Did anybody do performance comparison between TS and Python? I'm sure TS is faster than Python, but by how much? Would it be worthwhile to develop a fast-paced game with Python, or should it be kept for puzzles/card/etc games?
(Yes, I could do this myself, but I'm busy... :()
#5
@Jean-Francois: TS is probably faster, yes, but the speed hit depends on how you structure your code. It's going to be negligible 90% of the time since the code will only run after an event like a keypress. I will know more once I finish up some prototype ideas I'm working on, but I don't expect the difference to be tremendous.
In other news, I did fix a bug so there's my little contribution to this package. :)
09/12/2005 (4:25 am)
@Josh: Ah, didn't realize that. I'll post the resource soon, then. Thanks for the info.@Jean-Francois: TS is probably faster, yes, but the speed hit depends on how you structure your code. It's going to be negligible 90% of the time since the code will only run after an event like a keypress. I will know more once I finish up some prototype ideas I'm working on, but I don't expect the difference to be tremendous.
In other news, I did fix a bug so there's my little contribution to this package. :)
#6
Jason i tried to email you but your email isn't posted - but im subscribed to this thread here :).
im lookin to start up a project in T2D with Python - any updates regarding T2D and python would help!
01/26/2006 (9:04 pm)
Is there any resource for the PyT2D binding? or does the TGEPython binding work? i couldn't find the resource for PyT2D. i actually msged Tomas Dahle and he mentioned he wasn't working on it anymore.Jason i tried to email you but your email isn't posted - but im subscribed to this thread here :).
im lookin to start up a project in T2D with Python - any updates regarding T2D and python would help!
#7
I wrote something which may be similar, but using Lua and probably less sophisticated :p
01/26/2006 (11:22 pm)
What are these bindings like? Are all functions exposed to Python? Can Torquescript and Python share data? I wrote something which may be similar, but using Lua and probably less sophisticated :p
#8
I'd like to say that the bindings are great, but I continued Tomas' work and ran into a few issues that made things complicated. Maybe it's because of the kind of systems I create, but eventually I decided that pure torquescript was the best option for this engine (and Tomas agreed). But it's very capable so maybe you guys will get more mileage than me. All script functions are exposed in Python, with caveats here and there.
Python and torquescript can kind of share data, yes. This is one of the problems I mentioned. Python instantiated stuff can't be seen from the torquescript side, which is ok if you don't plan to use any torquescript. The bad news is that you will inevitably have to in order to make certain things work (event callbacks are especially tricky). Maybe more work can be done with the C source to fix these problems, but I got to the point where it wasn't going to be worth the effort when I could just use torquescript and get a game done. :)
Like I said, I was doing some crazy stuff to have my way (I'm a perfectionist), so maybe you won't have the same issues. If you could get T2D to work as an extension rather than embedding Python, you might have better results. If I was going to continue work on this, I'd probably use SWIG to wrap T2D and try it that way. So if anyone wants to take the torch, I'll give you the source. I'd love to see T2D fully integrated with the power of Python.
01/27/2006 (1:17 am)
@lineage: There is no resource, but I have the code if you want me to email it to you. jason -at- griminventions.com if you want to email me.I'd like to say that the bindings are great, but I continued Tomas' work and ran into a few issues that made things complicated. Maybe it's because of the kind of systems I create, but eventually I decided that pure torquescript was the best option for this engine (and Tomas agreed). But it's very capable so maybe you guys will get more mileage than me. All script functions are exposed in Python, with caveats here and there.
Python and torquescript can kind of share data, yes. This is one of the problems I mentioned. Python instantiated stuff can't be seen from the torquescript side, which is ok if you don't plan to use any torquescript. The bad news is that you will inevitably have to in order to make certain things work (event callbacks are especially tricky). Maybe more work can be done with the C source to fix these problems, but I got to the point where it wasn't going to be worth the effort when I could just use torquescript and get a game done. :)
Like I said, I was doing some crazy stuff to have my way (I'm a perfectionist), so maybe you won't have the same issues. If you could get T2D to work as an extension rather than embedding Python, you might have better results. If I was going to continue work on this, I'd probably use SWIG to wrap T2D and try it that way. So if anyone wants to take the torch, I'll give you the source. I'd love to see T2D fully integrated with the power of Python.
#9
he actually recommended PyGame. of course this is totally subjective, but for your 2D project you still decided to choose T2D and use torquescript, over PyGame using Python?
im just trying to figure out the best approach for a 2D game. i already have T2D but at the same time, id like to learn / exercise alot more python on the way, but ultimately my goal is a 2D game - so Python can wait if need be.
k thanks for the update - i think ill pass on the bindings and stick to T2D and torque script, or if you really recommend checking out PyGame let me know :)
01/27/2006 (9:49 am)
Thanks for the big update jason. i happen to receive an email from tomas saying the same thing - it wasn't worth it to continue integrating python into T2Dhe actually recommended PyGame. of course this is totally subjective, but for your 2D project you still decided to choose T2D and use torquescript, over PyGame using Python?
im just trying to figure out the best approach for a 2D game. i already have T2D but at the same time, id like to learn / exercise alot more python on the way, but ultimately my goal is a 2D game - so Python can wait if need be.
k thanks for the update - i think ill pass on the bindings and stick to T2D and torque script, or if you really recommend checking out PyGame let me know :)
#10
I really don't like TorqueScript's syntax at all, but it does its job well, and it is definitely better than C++ in terms of time to completion. Just go for it!! :)
01/27/2006 (11:15 am)
As much as I love Python, if your goal is to create a 2d game, then T2D kicks PyGame's ass up and down town. T2D gives you such a huge head start. I explored making a 2d engine with PyGame, but it's as much work as starting from scratch. PyGame is cool, but it's not going to save you much time other than binding Python to SDL. So I can't recommend PyGame. Some kind of Python/T2D hybrid doesn't look likely, without heavy constraints and big caveats (which defeats the purpose IMHO).I really don't like TorqueScript's syntax at all, but it does its job well, and it is definitely better than C++ in terms of time to completion. Just go for it!! :)
#11
01/27/2006 (12:38 pm)
@Jason: When I put up my Lua resource it may help you see how to allow Torquescript and Python to "talk". For example it should be possible to assign the value of a TS variable to a Python variable or to call each others functions. It can probably be done using a scriptobject similar to the one I have. I don't know Python though, so I couldn't help with it much..
#12
01/27/2006 (1:41 pm)
Looks like its time to start the project in T2D!
#13
01/27/2006 (2:22 pm)
Cool, it looks like t2d.net has some healthy compitition :)
#14
@Jason: Not unless someone else wants to work on it. :) But I'm interested in how Iron Python would fit into the equation with T2D.net.
01/27/2006 (4:11 pm)
@Joe: Python can see everything that's created in TS, but TS can't see things created in Python (ie, a Python native class). Maybe someone else will want to put the time into it, but I'm pretty much finished with that project. :)@Jason: Not unless someone else wants to work on it. :) But I'm interested in how Iron Python would fit into the equation with T2D.net.
#15
That being said, I personally dont like t2d.net in it's current incarnation. It is basically the same syntax as torquescript but using C# instead of... well torquescript.
I did a proof-of-concept cleanup (making t2d.net into a 'real' .NET SDK) which turned out really well, but I wont have time to work on it until I quit my day job (which will be end of feb, in preperation to moving to thailand)
I'm going to actually move in the May-June timeframe, but without a day job eating up 50hrs a week i should have more time to work on t2d.net before then.
01/27/2006 (4:25 pm)
@Jason: Have you used .NET before? Basically, any .NET compliant language can work with any/all .NET sdk's. Meaning that there is nothing magical standing in the way of t2d.net working with any/all other .net languages, including Iron Python.That being said, I personally dont like t2d.net in it's current incarnation. It is basically the same syntax as torquescript but using C# instead of... well torquescript.
I did a proof-of-concept cleanup (making t2d.net into a 'real' .NET SDK) which turned out really well, but I wont have time to work on it until I quit my day job (which will be end of feb, in preperation to moving to thailand)
I'm going to actually move in the May-June timeframe, but without a day job eating up 50hrs a week i should have more time to work on t2d.net before then.
#16
Good luck with your new venture and with t2d.net!
01/28/2006 (9:36 am)
Well I wouldn't consider using .net until 2.0 is really common (ie, already on 90% of computers) and Mac support via Mono is solid/common. I was studying C# heavily when I ran into Python, and the rest is history. :) So it may never come to pass for my games (although tools is a different story), but I do like the .net design.Good luck with your new venture and with t2d.net!
Torque Owner Jean-Fran
If I understand the EULA correctly, I can't open my TorqueScript source. What about the Python source? Am I bound by the same restrictions?