Driklyn Posted September 8, 2013 Posted September 8, 2013 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? Quote
AggrorJorn Posted September 8, 2013 Posted September 8, 2013 Every C++ command has a Lua equivalent (or better said: the Lua function refers to the C++ command): http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/window/windowmousehit-r457 Every command is documented with C++ as well as Lua. Quote
Driklyn Posted September 8, 2013 Author Posted September 8, 2013 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. Quote
Josh Posted September 9, 2013 Posted September 9, 2013 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/ Quote My job is to make tools you love, with the features you want, and performance you can't live without.
Driklyn Posted September 9, 2013 Author Posted September 9, 2013 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 Quote
Josh Posted September 9, 2013 Posted September 9, 2013 Lua docs: http://www.lua.org/manual/5.1/ Like I said, most Lua commands have a method in the Interpreter class, but if not you can get the Lua virtual machine with Interpreter::L and start calling Lua yourself directly. Have fun! Quote My job is to make tools you love, with the features you want, and performance you can't live without.
shadmar Posted September 9, 2013 Posted September 9, 2013 Hey this is nice! Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB
Recommended Posts
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.