OK, that's not a great title for this thread but here goes. I'd like to be able to dynamically call a class. So, let's say I have class1, class2, class3 etc. all setup using the gideros.class(Sprite) method.
But, I want to create instances of those classes dynamically, e.g. using "class" and appending the number to the end depending on which one I need at any given time.
I've only been using Lua for a few weeks and I'm wondering if there is any way within it for creating an instance of a class by referencing the class name as a string which I'd concatenate to correspond to the actual class I want.
That still probably makes no sense, but any help appreciated (if you understand any of that).
Comments
Well easiest solution, I can come frop the top of my head, to do something like this:
Is there much overhead to doing this? By that, I mean memory usage by putting the classes in a local table? To be honest, this is the part about Lua that I'm finding the most difficult to fully get to grips with (after years of C, C++, Objective-C etc.) as everything seems so loose which is great, and liberating, but also feels wrong on so many levels!
ar2rsawseen: 1
atilim: 619
Almost got him. Unless atilim has better suggestion
@moopf
Well I don't think there's much overhead, because myClasses would only store reference to your class, not it's copy.
But I can understand you, I'm a fan of loosely typed languages, and all of my friends and colleagues are quite opposite. So we usually have discussions, and mostly result is, that I can create more, by writting less, but they can optimizing things on levels, which I can't.
Both have their good and bad sides, and you can't live only in one world
Likes: gorkem, atilim, chipster123
Whilst I've done a lot of dev work in PHP over the years (and I mean a lot!) and that's loosely typed, it is fundamentally structured not too dissimilar to C in a lot of respects. The whole tenet of Lua seems to be different from languages that use the organisation and basic structure of something akin to C and that, I think, is where I'm finding the most difficulty transferring to Lua.
It's actually really clever, but after years of using languages that are all constructed around the same basic organisation, Lua is a bit like learning coding again because it's so fundamentally setup in a different way.
Although php, kind of going that way, by supporting closures etc, it's class structures are still "hardcoded" and cannot be so easily changed on runtime.
Besides that, I also had a week with Objective-C, and although I could dive in and make a game prototype, I'd really prefer not to go back there.
My apps: http://www.yummyyellow.com
But most verbose thing I find in strong typed languages, is actually a data type, example from Java
MySuperAwesomeClass myInstance = new MySuperAwesomeClass()
It's not descriptive, when you get it twice - it's annoying.
[a setColor:255 :255 :255];
I had a friend who would code like this. So yeah, I see what you mean about Obj-C encouraging verbosity, but it isn't required and it has more to do with the API and especially Apple telling everyone to quit abbreviating and things like that, which is something I also believe and so my variable names are usually huge.
But at the same time, I keep thinking that something should be easy to do in Cocoa and an hour later I'm wondering why I haven't got anything to show yet. I haven't written anything serious for Gideros yet but I can see that the Gideros examples that I've run would take a ton of code in Cocoa.
My apps: http://www.yummyyellow.com