I disagree... a little. the point of encapsulation is to hide parts of the interface that are likely to change or that other shouldn't be touching. that does [edit]not[/edit]translate into "don't directly access variables" if we had Private and Public options for variables this wouldn't be an issue. now on the other hand... always using setters and getters does give you many advantages and keeps your interface consistent but since BMax doesn't have Properties it can be quite cumbersome.
The author of the type or class should be free to implement a module in any way which fulfills it's requirements. Functions do add overhead and as mostly game programmers here you might need the tightest loops. there is no problem with directly accessing the field as long as you stay consistent in your design.
to answer you first question...
only by casting them to the correct type. which you did not do in your example. your for loop casted the returned objects from alienlist to TAlien. your next line did the exact same thing for castedalien.
you need to know what and why you are casting... to access those fields you need to directly cast to TAlienSmall or TAlienBig. but that's not what you want here....
my first question to you would be; why did you think you need a health field in each type? doing that means you are overloading the field that's already in the base. you can do this BUT there is generally no reason to and it is most often a product of bad design and should be avoided.
all your aliens have health right? that means it should be in your base ONLY.
here is what I would do. (without reflection)
and as you continue to code you will probably find it's easier and more convenient to use Create(blah:blah,blah:blah) *functions* in addition to New() *methods*. Also, encapsulating your lists, especially your base list (but I say all of them) will gain you many advantages.
I can show you that for this example if you would like.
The author of the type or class should be free to implement a module in any way which fulfills it's requirements. Functions do add overhead and as mostly game programmers here you might need the tightest loops. there is no problem with directly accessing the field as long as you stay consistent in your design.
to answer you first question...
I can call the right methods of the objects, but how
do I get access to the fields of the objects that are not Baseobjects?
do I get access to the fields of the objects that are not Baseobjects?
only by casting them to the correct type. which you did not do in your example. your for loop casted the returned objects from alienlist to TAlien. your next line did the exact same thing for castedalien.
you need to know what and why you are casting... to access those fields you need to directly cast to TAlienSmall or TAlienBig. but that's not what you want here....
my first question to you would be; why did you think you need a health field in each type? doing that means you are overloading the field that's already in the base. you can do this BUT there is generally no reason to and it is most often a product of bad design and should be avoided.
all your aliens have health right? that means it should be in your base ONLY.
here is what I would do. (without reflection)
SuperStrict Type TAlien Field health:Int = 1 Field name:String = "Alien" ' Method PrintHealth() Print "Health in "+name+" = "+health End Method End Type Type TAlienSmall Extends TALien Method New() health = 2 name = "AlienSmall" End Method End Type Type TAlienBig Extends TAlien Method New() health = 3 name = "AlienBig" End Method End Type ' Main Local alienlist:TList = New TList Local as:TalienSmall = New TAlienSmall Local ab:TAlienBig = New TAlienBig alienlist.AddLast(as) alienlist.AddLast(ab) For Local alien:TAlien = EachIn alienlist ' alien.PrintHealth Print alien.health Next
and as you continue to code you will probably find it's easier and more convenient to use Create(blah:blah,blah:blah) *functions* in addition to New() *methods*. Also, encapsulating your lists, especially your base list (but I say all of them) will gain you many advantages.
I can show you that for this example if you would like.