Best way to set custom states/flags to a node

Hi guys,

Is there a proper way to set states, flags or properties to nodes?
for example, if I want to set custom states to a platform node:
Platform.material: ice
Platform.climbable: true
Platform.canCollideFromBottom: false
etc.

Ok, can I set a script component, but I’ve read that it’s not performant to use in the game loop.

any ideas? thanks!

Hi,
I always use node.PropertyName = “something”; as flags or custom data. You can always define object properties in Javascript which I really love :smiley:

Sometimes I use it to access components quickly if I need to frequently. Like this:

cc.Class({
    extends: cc.Component,
    properties: {
    },
    __preload(){
        this.node.citizen_control = this;
    },
    update(dt){
        //...
    }
}

tip: __preload() is called just before onLoad for all components and very useful to setup things before onLoad() called :wink:

Hi,
I’m using typescript, but I noticed that it works although vs code mark it as error.

after searching a bit, I found the elegant way to do the same in TS

(this.node as any).customProperty = 2;

and then you can access to customProperty:

cc.log ("customProperty: ", (platformNode as any).customProperty);

Thanks!
PS: Great videos in your youtube channel

1 Like