Basics: Translation
Automatic Lua to C++ converter
Table of Contents
Lua to C++
ShiVa can automatically translate your Lua scripts into C++ code. Due to the nature of C++ being a compiled language and Lua being interpreted at runtime, there are considerable speed and performance advantages (up to 50%) to code translation. But most importantly, it means that you can benefit from both fast development using Lua scripting and fast execution from native code compilation.C++ translation can be chosen at export time in the Authoring Tool (Button "Generate C++ Files"). You also need to select "Project" as Output Type in order to generate compilable solutions. Depending on the platform you are on and the one(s) you are targeting, projects will be generated for:
- Microsoft Visual Studio on and for Windows
- Xcode for OS X and iOS
- makefiles for gcc Linux
Projects are exported for Xcode (*.xcodeproj) and Visual Studio (*.sln) where the platform permits it.
Every Lua script will be converted into its own .cpp file, named appropriately for easy navigation:
SimpleProjector_Projector_Handler_onEnterFrame.lua becomes
SimpleProjector_Projector_Handler_onEnterFrame.cpp
Limitations
General
C++ exports will not work for:- games that load additional STKs containing Lua code
- Lua code that is not in the ShiVa API
- platforms that do not offer native C++ SDKs, like Flash and HTML5
Functions
Functions can have only 13 parameters, less is always better.Do not write functions that can have a variable amount of return values:
function MyAI.myFunction ( ) if ( ... ) then return 1, 2, 3 --> 3 returns end return 1 --> 1 return end
Do not declare variables multiple times in the same scope:
function MyAI.myFunction ( ) local i = 0 ... local i = 1 --> second declaration, won't compile in C++ ... end
Strings
For performance reasons, the memory pool for strings is only 1MB by default. If you have a lot of strings or want to import big text files, you need to increase it by adding this line of Lua code to your main AI:application.setOption ( application.kOptionNativeStringPoolSize, nSizeInBytes )
This is a C++ export only setting, it has no effect for regular script exports.
Example
A ShiVa handler function like this:-------------------------------------------------------------------------------- function SimpleProjector_Projector.onEnterFrame ( ) -------------------------------------------------------------------------------- local lx = this.nX ( ) + math.cos ( ( application.getTotalFrameTime ( ) + this.nDelta ( ) ) * this.nSpeed ( ) ) * 30 local lz = this.nZ ( ) + math.sin ( ( application.getTotalFrameTime ( ) + this.nDelta ( ) ) * this.nSpeed ( ) ) * 30 object.lookAt ( this.getObject ( ), lx, this.nY ( ) - 5 , lz, object.kGlobalSpace, 1 ) -------------------------------------------------------------------------------- end --------------------------------------------------------------------------------
... will be transformed into this:
//------------------------------------------------------------------------------ #include "SimpleProjector_Projector.h" //------------------------------------------------------------------------------ using namespace S3DX; //------------------------------------------------------------------------------ S3DX_BEGIN_HANDLER_00( SimpleProjector_Projector, onEnterFrame ) { AIVariable lx = this->nX() + math.cos((application.getTotalFrameTime() + this->nDelta()) * this->nSpeed()) * 30.0f; AIVariable lz = this->nZ() + math.sin((application.getTotalFrameTime() + this->nDelta()) * this->nSpeed()) * 30.0f; object.lookAt(this->getObject(), lx, this->nY() - 5.0f, lz, object.kGlobalSpace, 1.0f); } S3DX_END_HANDLER( ) //------------------------------------------------------------------------------