• entries
  • comments
  • views

More Scripting

Sign in to follow this  


So, after looking at Lua again with Phantom's post in mind, I found that you can place functions inside of variables in the global table; e.g.

function events.create()


this makes a key "create" with a value of the function pointer in the table "events" which is in the global environment.

So starting off with my existing good ju ju, that is, replacing the environment table with a custom one and having it forward to the 'real' environment table for things it can't find.

I then added another feature:

Whenever a script is run (file is loaded and function in that file is called), I first create a new table and set it into the global envronment with a key of "events"; then within this script any function that wants to be valid for an event call, needs to be designated as "events.eventName"

After loading the code into the new thread state, I lookup the events table, and grab the function from it with the given function name; i think call this function like normal.

This works great because each time I run a script the events table is dumped, as such the code doesn't hang around from call to call (allowing me to know if a function is not defined); and simply replacing the table for each call is a lot less ugly than 'scrubbing' the state for functions and deleting them; in addition to feeling 'cleaner' it also means I can have global non-event related functions (which I couldnt before, as they got scrubbed each time) which makes for easier programming and fewerer pitfalls.
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