Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Dynamic Lua!

Sign in to follow this  


Back in august I was hanging out on IRC when VertexNormal came up with an intresting Lua related question todo with dynamically compiling Lua code on the fly and storing it for later access.

At the time I gave it a go and managed to get something working where, via two helper functions you could supply Lua code and it would compile it, save it and allow you to re-run it using global variables. After a bit of fiddling it would even return a value from the function [smile]

However, I wasnt happy with the solution, as it required that you have the variables the function was called with visable in global scope and as we all know global variables, while they have their place, are often best avoided.

So, after a break I woke up today with a drive to come up with a more flexible solution which didnt polute the global variables and allowed for as many dynamic variables as required to be supplied.

about 10h and much swearing, crying, confusing and hacking later I have something which works!

I'm still working on the dynamic variables bit, however you can now supply the function with varibles which are set elsewhere and which dont polute the global name space. Its done via the magic of metatables, allowing me to lookup the variable in the orignal global namespace (first) and then a supplied parameters table (second).

It also still requires two support functions in Lua, which while it might not be a great deal I would like to try to remove them and do it via the C-API, as I think its posible, its just a matter of working out how to setup metatables and switch the enviroment on functions when they are used.

Once I've got this nailed down and cleaned up I think I'll follow evolutional's advice and write something about it as I think it might make a reasonable intresting article or sweet snippet, in the mean time if anyone is instrested drop me a line and I'll share the code around [smile]
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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!