PDA

View Full Version : call C++ functions/Variables by Script


sjoerd222
12-31-2006, 02:26 AM
Hi

Could anyone of you explain me how a script can call c++-functions or get acces to variables?
I tried to build something like a script language for a game that I am developing. But it's not so simple i saw. I have never done something like this befor. The only thing I can get working well is passing string, compare them, and call a related function, but thats ugly.

I thought about an build in Interpreter for my little game. Can anyone give me a good explanation how this works?
The Interpreterpart of my game can allready understand the syntax, but it lack calling c++ functions, storing Variables effectively (till now, they'r simply stored as strings in a selfbuild vector class). All function have one parameter of a self build type, which is a class containing various numbers of parameters (something like a vector).

Till now I have now idea how this is done by all the other languages. Does somebody now another way of calling C++ function by script?

thx!!!

dave_
12-31-2006, 04:30 AM
The only thing I can get working well is passing string, compare them, and call a related function, but thats ugly.


What you need is reflection (http://en.wikipedia.org/wiki/Reflection_(computer_science)), C++ does not have it built in to the language


Dont bother making your own interpreter for your game, you dont need to. Integrate an existing one, Try Lua or Tcl. There are loads of wrappers to help you integrate them.

Look at the articles on gamedev (http://www.gamedev.net/reference/list.asp?categoryid=76)

Reedbeta
12-31-2006, 10:27 AM
Basically, you need to provide a way to register C++ functions with the scripting engine. To do this you can use a function pointer, for instance store the function pointer with the name of the function (as a string) in your data structure someplace. You'll have to also communicate the number and types of parameters, or else write your script-accessible C++ functions so that they all take the same kind of datastructure (a list of the actual parameters). I'd advise you look at Lua, as even if you don't end up using the actual language, you can get some good ideas about how to proceed from studying how Lua provides the ability to call C functions.