Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
Are you designing your own language, and writing a compiler/interpreter for it in C++ ? Or are you trying to create bindings to a C++ application in an existing language ? I hope it's the second option. In this case, can you read about SWIG, especially this part: swig.org/Doc2.0/Lua.html, and tell us more precisely what you need to know ? –  Tibo May 22 '13 at 9:48
    
@Tibo I'm designing a simple language with a simple interpreter when I have free time and I want to use it with a 3rd party small game engine. I couldn't figure out the mechanism how the script works with an objects implemented in a C++ binary module: method calls, creating new instances, etc. Now, everything is clear. Thank you for your attention. –  Dmitry K. Jun 14 '13 at 22:13
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
It is really what I need. This way is clean for me. I was close a bit: I thought I could make a base class for all objects with onMethod(methodname, arglist) member function that could process calls from a script and a global factory function for creating new instances of an object from a script. But this method looks more clean and straight-forward. Thank you very much. –  Dmitry K. Jun 14 '13 at 22:05
add comment

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)

share|improve this answer
    
Though it looks somewhat complex for me to understand but I think that it is the right way. Thank you very much. –  Dmitry K. Jun 14 '13 at 21:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.