COMMUNITY

How to set value in macro?

obj.onLoad=instance_.onLoad;//how to write here?

macro static public function build():Array<Field> {

        var fields = Context.getBuildFields();

        var cls = Context.getLocalClass().get();

        var pack = cls.pack.concat([cls.name]);

        var name = pack.join(".");

        var obj:Dynamic = {onLoad:null};

        fields.push({

            name: "main",

            access: [Access.APublic, Access.AStatic],

            kind: FieldType.FFun({

                expr: macro {

                    var instance_ = Type.createInstance(Type.resolveClass($v{name}), []);

            

                    obj.onLoad=instance_.onLoad;//error! how to write here?

                    
                    

                },

                args: [],

                ret: null

            }),

            pos: Context.currentPos()

        });

        return fields;

    }

What’s the function you expect?

hi,@R32,I want to implement some macro magic to use haxe got the same code as typescript

const {ccclass, property} = cc._decorator; // Introduce ccclass and property from the cc._decorator namespace

@ccclass // use decorator to declare CCClass
export default class NewClass extends cc.Component { // ES6 Classes declaration syntax, inherited cc.Component

    @property (cc.Label)    // use property decorator to declare attributes, parentheses are attribute types, decorator type declaration is mainly used for editor display
    label: cc.Label = null; // here is the type used to declare the type of statement, the colon is followed by the type of property, the equal sign is followed by the default value.

    // You can also use the full attribute to define the format
    @property ({
        visible: false
    })
    text: string = 'hello';

    // member method
    onLoad () {
        // init logic
    }
}

will genrate js

cc.Class({
    extends: cc.Component,
    // name:"sonygod",
    properties: {
        label: {
            default: null,
            type: cc.Label
        },

        text: 'hello''
    },

    onLoad: function onLoad() {

        // debugger
        this.label.string = this.text;
    }

});

but unfortunately,it’s seem not so easy to do something like that.