Jump to content
  • Advertisement
  • entries
  • comments
  • views

The Beginnings of Scripted Objects

Sign in to follow this  
Jason Z


Scripting Objects

Neither of my Lua books have shown up yet, which may have turned out to be a good thing. I've spent more time on the documentation and the online Programming in Lua book than I would have if I already had the books and it appears to have given me a better grasp on things.

With a little better understanding of how Lua works, especially with respect to passing data back and forth to C/C++, I have the beginnings of a functional system mapped out. Initially I had considered using toLua++ or Luna or Lunar (all of which are described on the Lua-users wiki here) but after reading a little about them they actually seemed either too hackish or too complex for what I was looking for.

Really, what I want to be able to do is to create C++ engine objects via a script. The perfect example of this (and consequently the first object I have scripted) is my CEntity3D class. It is the basic entity class that has a location and orientation, and can be rendered. If I can simply run a script to create a number of entities in a scene, and then animate them through an updating script, then that serves the purposes that I am looking for. If possible, I want to be able to reference these script created objects through the script and query and set some of it's state - such as the position so that I can make it move.

Handles And What You Put Into Them

Where I landed in the design was to provide a function per class that will create an instance of the class for the script to use. The problem is, you need to pass something back to the script to be able to reference the object later. The naive approach (which I was guilty of initially...) is to return the pointer to the object as a number in Lua. Since Lua uses doubles as its number type, there is no loss of precision with a 32-bit integer. However, this opens up all types of security and stability issues - a user or hacker could manipulate the pointer in the script world and the sky is the limit.

Then I recalled an article that I read in Game Developer magazine by Noel Llopis - I don't remember the title, but it was all about using handles to refer to a list of objects. The basic idea of a handle is to split the bits of the handle up for different purposes. For example, the upper 16 bits can be used to identify the object's class type, and the lower 16 bits can be used to identify the instance of that class type. That's exactly what I have used them for...

Script Manager

So, the script manager is modified to allow a class to register it's type. This amounts to giving each class an ID equivalent to it's order of registration. Then, when an instance of the class is created, it also get's registered to the script manager, also getting an ID equivalent to it's order of registration. Then the function call to create the object from the script returns a handle with the upper half as the class ID and the lower half as the instance ID. The script simply stores this handle as a number, and then passes that handle to a function to perform an operation on the C++ object.

The beauty of it is that if I want to make the system more secure I can change the handle layout to include some CRC bits or choose a random integer for the object instance ID. Then any bogus handles can easily be identified and logged. The system is in it's infancy, but the initial results look pretty good.

Next up is creating Lua classes that encapsulate these handles and use them to implement member functions with them. This will be close to the holy grail with respect to my scripting desires, so I'm really looking forward to hitting this thing and seeing how it turns out...
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!