How to mix c++ & lua? And is there a component to update lua script & resource package?


#1

I’d like to know is there any tutorial about mixing c++ code with lua to implement some mission/stage scripts?

and is there any component to handle the update of lua script & resource packages?


#2

For use Lua and C++ together, here’s the documentation: http://www.cocos2d-x.org/wiki/Lua, especially you should pay attention to the section of Bind a c++ class to lua by bindings-generator automatically.

For update of lua script & resource packages, please refer to AssetsManagerEx:

BTW: I can’t remember if AssetsManagerEx has been binded to Lua. If not, you can follow the first documentation to bind it via bindings-generator.


#3

so i cannot simply mix two language together and must choose to create project with lua?

Can I call lua in c++ project just as what can do in JNI, in the way that create a vm in c++ and run a lua script?


#4
  1. I suggest you to create project with Lua. Otherwise, you had to add Lua source codes, add Lua library paths manually into your c++ project. It can work, but you need extra works.

  2. Of course you can call lua in c++ project, sample code

#include "CCLuaEngine.h"

void foo()
{
    LuaEngine::getInstance()->executeScriptFile("myluacode.lua");
}

You can also execute a string of lua code, a global function and many other types. Please read CCLuaeEngine.h for more details


#5

I get it.
That is to say call lua in c++ project is not a good choice for me because it is lack of support&practise because I’m totally a newbie in cocos2d-x.

Thanks for your reply.


#6

hi, We have a cocos2d-x c++ project. And we add the lua-support in Xcode, it work well.
but on the android platform, We get the error when linking:
“ Particle3D/PU/CCPUBoxCollider.cpp:216: error: undefined reference to ‘cocos2d::AABB::containPoint(cocos2d::Vec3 const&) const’
….
Particle3D/CCParticle3DRender.cpp:258: error: undefined reference to 'cocos2d::AABB::getCorners(cocos2d::Vec3*) const’
collect2: error: ld returned 1 exit status”

The “Android.mk”:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/…/…/…/…/…/cocos2d-x)
$(call import-add-path,$(LOCAL_PATH)/…/…/…/…/…/cocos2d-x/external)
$(call import-add-path,$(LOCAL_PATH)/…/…/…/…/…/cocos2d-x/cocos)

LOCAL_MODULE := showgame
….
LOCAL_STATIC_LIBRARIES := iconv_static cocos2d_lua_static
……
$(call import-module,scripting/lua-bindings/proj.android)

Can you tell me how to fix it?


#7

Hello
i have the same problem here. I want to add Lua to my cpp project. I ve done so many things my Mk looke like this :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/cocos)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/external/lua/luajit/include)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/external/lua/lua)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/external/lua/tolua)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/cocos/scripting/lua-bindings/)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/cocos/scripting/lua-bindings/manual)
$(call import-add-path,$(LOCAL_PATH)/…/…/cocos2d/cocos/scripting/lua-bindings/auto)

LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

LOCAL_SRC_FILES := hellocpp/main.cpp
…/…/Classes/AppDelegate.cpp
…/…/Classes/HelloWorldScene.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/Classes
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/cocos
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/cocos/scripting/lua-bindings/manual
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/cocos/scripting/lua-bindings/auto
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/external/lua/lua
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/external/lua/tolua
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/external/lua/luasocket
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/external/lua/luajit/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/…/…/cocos2d/external/xxtea

_COCOS_HEADER_ANDROID_BEGIN

_COCOS_HEADER_ANDROID_END

LOCAL_STATIC_LIBRARIES := cocos2dx_static cocos_lua_static luajit_static

_COCOS_LIB_ANDROID_BEGIN

_COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)
$(call import-module,scripting/lua-bindings/proj.android)
$(call import-module,.)

_COCOS_LIB_IMPORT_ANDROID_BEGIN

_COCOS_LIB_IMPORT_ANDROID_END

But Still I cant run my project when I want to build my Android Apk though the project works fine in visual studio. The error is undefined LuaEngine.
Please Please some body helps if she/he knows how I could fix this
Thanks alot