Main
I’m making a scripting language using C++. I plan to use it with a simple test game editor. But I have to make a support for bindings to call game engine’s nodes’ methods to update positions, rotations, etc.
What are the main approaches for this?
The game engine I plan to use also supports lua, so maybe I can also use this fact.
Details
The problem I have faced lies in the fact of how can I call library C++ object methods within a script. English is not my native language and the lack of knowledge in that sphere prevents me from making a good query on the topic. So I don’t know what to search. Google shows me information on how to make bindings to some scripting languages not the fundamentals by search phrases like “how to make bindings support to a scripting language”, “scripting bindings”.
At this time I came up to the following things. I have to inherit all objects that will be used in script from a service class, ex. CLangObject. With its help I can identify the object’s type within a C++ library objects’ methods. Then I have to define the most general pointer to method types. I have also define a callback service class to pass it to a byte machine when C++ library method returns value. The game engine I want to use has bindning to lua, so maybe I can use its LuaObject and LuaCallback classes. At this time I don’t know if this approach will lead to a normal result.
2
I think the better search term for this might be “bridge”, have a try with that.
The simplest approach is to use a C function to dispatch from the scripting language into your engine. Imagine a function msgSend(target, message, arg0, ...)
; if you can write the scripting language engine such that it can call msgSend
with arbitrary arguments, you can see how from that you can call target
‘s message
method with arg0, ...
.
Since you mention Lua, have a look at https://github.com/vinniefalco/LuaBridge . You can see in the sample how he passes in classes and function addresses — on the Lua end these will be wrapped as some kind of Lua object and calling that function will enter a message dispatch function that will unwrap the target and arguments (if necessary) and proceed to call the function from the address.
1
To restate your concern, you have a piece of software, the game engine, that supports a particular scripting language, Lua, but are unsure how to expose, access, and interject game objects and events to Lua, or to do the same with your own custom external driver (a C++ based editor).
The game engine itself probably has a set of methods or decorators for exposing objects and events that would allow for tracking through external sources or programs. It also probably has it’s own specific protocol for manipulation by those external sources. Without knowing which particular piece of software or engine you are looking at, it is impossible to give specific guidance.
What I can suggest, is review the documentation of that particular engine. Short of that, look at how known things are exposed to the script, and mimic the method by which those things are exposed and acted upon.
For example, Torque3D offers documentation that shows how to expose objects to script (or an external source like a compiled C++ based editor).
http://www.garagegames.com/products/torque-3d/documentation
http://www.garagegames.com/products/torque-3d/documentation/Scripting/Advanced/EngineInterface.html
The second document shows how to create the call-ins, call-outs, exposed types, and limitations to function definition shape within the game engine.
In order for the script or driver/editor to consume and craft those exposed data types, at some level you will need to be able to create types from within the scripting language (or editor) that can be marshaled, or transformed, onto structures that match the format and conventions of the engine, basically a map. Each language has it’s own way of implementing a structures layout, field order, and sizing. Some follow a particular convention very well, others use their own convention.
The map might be unwieldy or ugly to use in the script, so you may want to create an additional second ‘clean’ type within the scripting language or driver/editor for normal use, with a ‘transform’ method to shape the ‘easy to use type’ into the type that can be marshaled back to the engine.
Lua has their own way of working with other languages, and it’s documentation specifically talks about accessing and consuming C based [compiled] software:
http://www.lua.org/pil/26.html (look at section IV)
1
You could build the scripting language into the engine so that the engine can take some file formatted in your language and invent some grammars for your program to compile these script files during run-time and use a stack to push method calls, variable declarations etc. like a normal running program though I think it limits your scripting language to only the methods your engine is written with, unless you create good grammars for the language that also binds to the original language the engine is written in.
1