Making Python a scripting language for C functions

Started by
5 comments, last by Kylotan 13 years, 1 month ago
I want to make Python a scripting language for regular C/C++ functions. For example, I have a particle emitter that will accept a starting position, but instead of using a custom scripting language I would like to use Python to initialize it. How in the world can I do that?
Advertisement
That's a really vague question. You could use the raw Python extending and embedding interface or a binding library like boost::python or SWIG.
I get how to run Python script from C. How do I run C functions in Python?
You don't run YOUR C functions directly. You write wrapper functions in C that take python object parameters and perform the necessary translation to then call your C functions. Google.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


I get how to run Python script from C. How do I run C functions in Python?


That's a really vague question. You could use the raw Python extending and embedding interface or a binding library like boost::python or SWIG.
Thanks for telling me about SWIG. I looked at the implications of it, but I believe for my purposes it may be too much control over my C program. Not too sure yet.

Anyway, I created a script reader using C/C++. I'm certain that Python would make the script reading easier because of its string parsing capabilities. I was mostly trying to get the Python script to run in place of the current script reader. I'm hoping it would give me more options than just writing variables to a file, like loops, if statements, and random numbers [not too sure if it can do that]. I have a huge enum showing all values available for commands, and I have a couple functions that convert strings in the file to their number counterpart.

Efficiency questions:
Should I remake the script reader into Python?
Does this seem like a sensible thing to do for a game engine?

If I do that, then I would essentially be creating multiple functions to incorporate the base commands, and then I would have to convert that command into a number for the C program to interpret. I would be running the base script and then loading multiple files while assuming the functions will always be there until Python exits. A problem I may run into is that I may want to stop the scripting for a frame, and start running another one. I'm assuming this may cause multiple problems with overwriting variables previously declared.

Can I run Python during run-time of a game without having the window for it open so I won't have lag time for it loading OR is incorporating Python in the program using #include <Python.h> [and of course Py_Initialize()] already do this?

Thanks for your help so far.
Python itself is just a language - if you embed the Python DLL in a C++ program there is no window at all. If you are seeing a window when using Python this must be because you are running some sort of program - eg. IDLE, the simple Python development environment.

I don't know what you mean by "script reader" - if you are reading and executing the script then that is exactly what a scripting language is for. You wouldn't use the Python string handling to understand the script - you write the scripts themselves in Python and the Python language executes it directly.

As for stopping scripting for a frame and resuming it later, Python does not allow that. You can't stop a Python function half way through - Python just continues executing the code until it reaches the end, at which point you end up back in your C code. To some degree you can fake this sort of functionality using Stackless Python but that is a very complex system.

If all you want to do is read in some data to initialise variables then a language like Lua is a better choice - it's smaller, quicker, and was designed for this sort of task.

This topic is closed to new replies.

Advertisement