[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Loading commands from a file



Ian Mallett kirjoitti:
I'd like to know how to load commands from a file. In the file (in this case a .txt) there would be commands like "glBegin(GL_QUADS)". I would like to load these commands into a program so that the commands can be executed. Previously, I had been selecting all in the .txt file and pasting all in the .py file, but I think there must be a

one option is to make your .txt file a .py file, and say 'import filename' (without the .py) in your app - that results in executing the commands in that file, immediately.

if you need more control over when the commands are executed, you can make them functions in the .py file, and then call those (e.g. say def drawrect(): glBegin ... in your commands.py, and then do: import commands; commands.drawrect() in your app).

there are also tools like the eval() function for which you can give commands as strings for python to execute, but i think you are better off with by making .py files (i.e. python modules) that you can import and use.

if you have a running application, where want to load commands from a file on the fly, you can use the reload() function to re-import a module (the modified .py file). see the documentation for more info.

Ian

~Toni