Jump to content

Access CPP global via Lua?


Driklyn
 Share

Recommended Posts

Started with a CPP project, but using Lua for scripted objects. Need to be able to access things like:

 

window->KeyDown
window->SetMousePosition
world->gravity

 

...from within the scripts themselves, even though they are defined in a CPP header file (App.h).

 

How exactly do I access them?

Link to comment
Share on other sites

Yes, I realize this, but I'm not creating the window/world in a script, as is occurring when you start from a Lua project. I need to be able to access the already created window/world (in CPP) via a script.

 

I was thinking I might need to use the `Interpreter` class, but I'm not entirely sure how to.

Link to comment
Share on other sites

Writing Lua interfaces is beyond the scope of what Leadwerks provides support for. However, the Interpreter class contains an analog for nearly every Lua C++ command and can be used to push and pop things from the Lua stack. I believe it would be something like this:

Interpreter::PushString("hello");
Interpreter::SetGlobal("myvar");

 

Whenever you are doing stack manipulation, it's a good idea to start a block of code with this:

int stacksize = Interpreter::GetStackSize();

 

And end it with this:

Interpreter::SetStackSize(stacksize);

 

This will ensure you don't mess up the stack inadvertently, and it's easier than popping a bunch of values yourself.

 

For full C++ binding with Lua, I recommend ToLua++:

http://www.leadwerks.com/werkspace/files/file/216-tolua/

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Thank you, Josh. Finally got it working! (I had nearly exactly what you wrote before creating this topic, but I placed SetGlobal before PushObject.)

 

bool App::Start() {
   // create window, context, world...

   int stacksize = Interpreter::GetStackSize();
   Interpreter::PushObject(window);
   Interpreter::SetGlobal("window");
   Interpreter::PushObject(context);
   Interpreter::SetGlobal("context");
   Interpreter::PushObject(world);
   Interpreter::SetGlobal("world");
   Interpreter::SetStackSize(stacksize);

   // load map, etc...
}

 

Then, from any script:

 

function Script:Start()
   System:Print(context:GetWidth())
   System:Print(context:GetHeight())
   System:Print(world.gravity) -- prints 0x00000000

   window:ShowMouse()
end

function Script:UpdateWorld()
   if window:KeyDown(Key.D) then
       System:Print("D down")
   end
end

 

This is awesome! And was much easier to do than expected... Only hiccup is that I couldn't seem to access any members of `world`, just its methods. I did, however, figure out you can do it this way instead:

 

Interpreter::PushObject(&world->gravity);
Interpreter::SetGlobal("worldGravity");

 

worldGravity.y = worldGravity.y * -1

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