Jump to content

Loading Lua script from C++


vblack
 Share

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 );
    list.push_back(entity);
 
}
  
// Load special script (But not working, object not callable).
Interpreter::NewTable();
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

end

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
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.

 Share

×
×
  • Create New...