Jump to content

Loading Lua script from C++

Recommended Posts

'm using the C++ SDK and lua for small objects code. I need to create the entities from C++ and later use the bool Entity::CallFunction(const std::string& functionName, Object* extra)

How to 'bind' on script lua to an entity ? it looks that Interpreter is using tolua++, but I can't figure how to load the .lua properly to the object 

// Create 100 special entity

std::vector<Entity*> list;
for (int i = 0; i < 100; i ++) {
	Entity* entity = new MyEntity();
	entity->AddToWorldTable(true );
// Load special script (But not working, object not callable).
Interpreter::SetGlobal("Script"); // Must Script. Don't change that, because the script are prefixed with Script:myFunction
if (Interpreter::ExecuteFile("MyEntity.lua")) {
	System::Print("Error: Failed to execute script \"" + it + "\".");
 for (auto&it : list) it->CallFunction("OnSpecialFunction")



Script Lua


function Script:OnSpecialFunction()
   print self



Share this post

Link to post

Yes I've read that, but the class doesn't do the loading of LUA file and instancing of for object

I'm using Interpreter::Push (which is tolua_pushusertype(L, o, o->GetClassNameStr().c_str())

But my class is for example Class A : public Entity, but my script using Script:<NameSpace> (same format as Leadwerks).

I need a function from an allocated Entity 

1) Load 1 Lua file  (Interpreter::ExecuteFile) (Which is using Script::<NameSpace> function).
2) 'Connect' for each entity  to the loaded lua (I don't want to load 1000 times the same lua code).
3)  Ability that each entity have its own variable.



Share this post

Link to post

Doesn't this just give "Error failed to..." every time? you're printing the error when the Interpreter::ExecuteFile method returns true..

But yeah Josh is right, Entity::SetScript(const std::string& path); is what you're looking for.

Share this post

Link to post

Ah ok, so SetScript is not just setting the file name, it loads and attach it, correct ?

Share this post

Link to post

Yep.  There is no script loading function, because there is no script object.

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...