Jump to content

Arena

  • entries
    5
  • comments
    21
  • views
    9,266

Pushing Lua


xtreampb

1,353 views

 Share

Lua Interpreter Tutorial  

8 members have voted

  1. 1. Should I write a tutorial on how to use the Lua Interpreter?

    • Yes
      8
    • No
      0

So after a quick, prompt to mentoring session from Josh about how to use Lua, I learned a lot. Hear are just a few things.

 

Each Lua project has its own virtual stack. Here is what this means. Each Leadwerks project, when running, creates it own virtual, global, stack. You can use this just like a normal stack in C/C++ except is done in a more explicit manner. In C, you call functions and the function along with the parameters are pushed onto the stack. When you exit the function, the function and it's parameters are popped off the stack. This is all handled mostly behind the scenes and new programmers are typically not aware of the stack or the heap. Now if your just using Lua, this tid bit of information may not hear you. But if, your using C with your Lua game (or Lua with your C game) then this may help you. Josh has kindly taken a good bit of work out of interacting with the global Lua stack with his Interpreter class. Here pushing and popping items onto and off the lua stack is more manageable. Lets look at one of my uses of the interpreter class to send my world object from my C code, to my lua script.

 

//done in C++
world = World::Create();
Interpreter::PushObject(world);
Interpreter::SetGlobal("World");

 

the above code creates a world in my C++ project, pushes this object onto the global lua stack, which is a virtual stack, and sets the key for this object to "World". Now to access my world object in my lua script is this:

--done in my App.lua start() script
self.world=World

 

thats it. i can now use my world object inside my script. As easy as this is, it isn't complete. As with any stack programming, it has to be balanced and just calling it in the script doesn't make it balanced. In the C code, before you do anything with the interpreter, it was reccommended to store the size of the unmodified lua stack with:

int stacksize=Interpreter::GetStackSize();

 

Once you are done pushing things onto the stack, set the size of the stack to it's previous, unmodified size with:

Interpreter::SetStackSize(stacksize);

 

This is just one of the many things I learned about Lua from Josh. I'm thinking that once I get comfortable enough with it, that I'll write a tutorial about this. Let me know what you think.

 

Remember you can watch the development of this project at www.twitch.tv/xtreampb. Stream is most active between 8:00 PM and 11:00 PM. Stop by and say hi, ask questions and give suggestions.

  • Upvote 1
 Share

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

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