[haxe] Dynamically create instance from a list of classes

240 views
Skip to first unread message

Georges Gabereau

unread,
Jan 13, 2013, 12:07:27 PM1/13/13
to haxe...@googlegroups.com
Hey all,

Not sure this is even possible in Haxe, but here's what I'm trying to do:

class A { }
class B { }
class C { }


class Main {
   
private var classes:Array<Dynamic>;
   
private var index:Int;
 
   
function new(){
        index
= 0;
        classes
= [A, B, C];
   
}

   
function nextInstance():Dynamic{
        klass
= classes[index];
       
return new klass();
   
}
}

main
=new Main();
main
.nextInstance(); # Returns in instance of A
main
.nextInstance(); # Returns an instance of B
main
.nextInstance(); # Returns an instance of C

Hopefully this example makes sense.

I'm creating a game and each level is a sub-class on my class Level. In the game, I want to have a list of the Level classes and just instantiate the next one once a level is complete. I could just create instances of every level on initial app load, but that seems wasteful and unnecessary.

Thanks!



Jason O'Neil

unread,
Jan 13, 2013, 12:14:19 PM1/13/13
to haxe...@googlegroups.com
Your list of levels would be defined like this:

var levels:Array<Class<BaseLevelClass>> = [Level1, Level2, Level3];

And then to create them

var level = Type.createInstance(levels[0], []);

With this syntax haxe will know that "level" is of the type
"BaseLevelClass" and will be able to use whatever methods or properties
your BaseLevelClass has.

Hope this helps...

Jason
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
>
>
>

Georges Gabereau

unread,
Jan 13, 2013, 12:23:02 PM1/13/13
to haxe...@googlegroups.com
This works perfectly. 
Coming from a much more dynamic language, I was having a hard time figuring out the types in that definition.

Thanks for the quick reply!

Jason O'Neil

unread,
Jan 13, 2013, 12:28:02 PM1/13/13
to haxe...@googlegroups.com
No worries, glad I was able to help :)

Good luck with your game!
> > To post to this group haxe...@googlegroups.com <javascript:>
> > http://groups.google.com/group/haxelang?hl=en
> <http://groups.google.com/group/haxelang?hl=en>

Heinz Hölzer

unread,
Jan 13, 2013, 12:46:41 PM1/13/13
to haxe...@googlegroups.com
You can also create anonymous functions that act as factories for your levels. Then everything is statically typed.

class BaseLevel
class A extends BaseLevel{ }
class B extends BaseLevel{ }
class C extends BaseLevel{ }


class Main {
   
private var classes:Array<Void->BaseLevel>;

   
private var index:Int;
 
   
function new(){
        index
= 0;

        classes
= [function () return new A(), function () return new B(), function () return new C()];
   
}

   
function nextInstance():BaseLevel{
       
return classes[index]();
   
}
}



main=new Main();
main.nextInstance(); # Returns in instance of A
main.nextInstance(); # Returns an instance of B
main.nextInstance(); # Returns an instance of C



Reply all
Reply to author
Forward
0 new messages