How to do something if attribute is enabled?

Hi, sorry for such terrible questions, I’m a new coder. How do you do something if the boolean on the attribute is set to true only? I’ve tried this:

if (PipeHeight.attributes.hasOwnProperty('Move')){
// etc etc
};

But, as I suspected while writing the code, it did not work… Sorry for simple questions :slight_smile:

Hi @_Lio3LivioN,

Your code checks if the attributes property of your PipeHeight script has a property called Move. I am not sure what you are trying to do with that. But If you would like to define a boolean (true/false) attribute on your script, to expose it for editing in the editor, you would do something like this:

var PipeHeight = pc.createScript('pipeHeight');

// here we define the attribute
PipeHeight.attributes.add('myAttribute', {
    type: 'boolean',
    default: true
});

// initialize code called once per entity
PipeHeight.prototype.initialize = function() {

   // here we check the value of **myAttribute**
   if( this.myAttribute === true ){
      alert('This is true!');
   }
};

Here you can read more regarding script attributes:

https://developer.playcanvas.com/en/user-manual/scripting/script-attributes/

You can name the attribute anyway you like, respecting the Javascript naming conventions, so myAttribute can be Move per your example.

Thanks @Leonidas, you have fixed my errors :slight_smile:
I appreciate it.

1 Like