Dynamics property assign

Hi, I want to pass optional parameters to a new instance of class via an object.
I can read the parameters but i don’t know how to assign them to the new instance.
In as3, i did that :
all the optionnals are in params and i allows me to add new properties in object and to be free to pass the parameters in any order.

function Forme(cible:DisplayObjectContainer, x:Number, y:Number, largeur:Number, hauteur:Number, params:Object = null) {
  if (params) {
	for (var property:*in params) {
		if (hasOwnProperty(String(property))) {
			this[property] = params[property];
		} else {
			trace("Attention, la propriété " + property + " n'est pas disponible pour l'objet :" + this);
		}
       }
  }
}

In haxe, idon’t know what to put instead of ??? (above)

private function new(cible : DisplayObjectContainer, x : Float, y : Float, largeur : Float, hauteur : Float, ?params : Dynamic = null){
    if (params != null) {
         for (property in Reflect.fields(params)){
		if ( Reflect.field(params, property)!=null) {
                      ??? = Reflect.field(params, property);
                }
                else {
                    trace("Attention, la propriété " + property + " n'est pas disponible pour l'objet :" + this);
                }
        }
   }
} 

thanks for your help

Perhaps something like this:

for (property in Reflect.fields(params)) {
    if (Reflect.hasField(this, property)) {
        Reflect.setField(this, property, Reflect.field(params, property));
    }
}

Instead of setField, you may need setProperty if you are setting get/set properties (such as sprite.x or shape.alpha)

There’s more information here

http://api.openfl.org/Reflect.html

thanks, that’s what i’ll do but now i’ve got a problem with the _ use before the variable for getter setter. I’m searchnig and post if i’m lost
thanks again
17 years of as1,2 and 3 and one week of openfl, it’s hard!!

1 Like

A getter for _hello would be get__hello (note the two underscores) but you don’t need getters for read-only properties like you do in AS3, var hello (default, null) makes a property public read, private write, which is a lot nicer than getters IMHO :smile:

For reflection, Reflect.setProperty (object, "hello", 100) would work for both cases

It works.
thanks to you.
do i have to mark it resolved ?

Yeah, if you do, it may help other users who come through with a similar question. Just hit the checkbox :slight_smile: