Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
0
Members

Adding properties to behaviors?

A topic by MsDoe created Mar 11, 2016 Views: 780 Replies: 6
Viewing posts 1 to 4
Hi there! I wanted to know how it is possible to add properties to a behavior for use in its update loop; When registering a behavior, you need to call the class itself, not an instance, but I can't find any way to add properties to the class itself, only how to add to an instance of said class. How is it possible to refer to properties on a class from within the class? For instance, in this code all the variables within update() throw errors

class ballBehavior extends Sup.Behavior{
constructor(ball)
{
super(ball);
let ballActor = new Sup.Actor("Ball");
ballActor.setPosition(0, 0, 0);
let ballAngle = 0;
let ballPosition = new Sup.Math.Vector3;
let ballVelocity = 0;
ballActor.addBehavior(ballBehavior);
}

update(){

this.ballAngle += this.ballVelocity;
//Constrains the angle between 0 and 2pi
if(this.ballAngle > Math.PI * 2){
this.ballAngle = 0 + this.ballAngle - Math.PI * 2;
}else if (this.ballAngle < 0){
this.ballAngle = Math.PI - this.ballAngle + Math.PI * 2;
}
//Maybe a little spinback on the pointer?
this.ballPosition = new Sup.Math.Vector3(Math.cos(this.ballAngle) * wheelActor.getLocalScaleX() * 100 / 100,
Math.sin(this.ballAngle) * wheelActor.getLocalScaleX() * 100 / 100, 0);

if(Sup.Input.wasKeyJustPressed("LEFT")){
this.ballVelocity = 0.5;
}


}
}



(+1)
class ballBehavior extends Sup.Behavior{
    
    private ballActor: Sup.Actor;
    private ballAngle: number;
    private ballPosition: Sup.Math.Vector3;
    private ballVelocity: number;
    
    constructor(ball) {
        super(ball);
        this.ballActor = new Sup.Actor("Ball");
        this.ballActor.setPosition(0, 0, 0);
        this.ballAngle = 0;
        this.ballPosition = new Sup.Math.Vector3;
        this.ballVelocity = 0;
        this.ballActor.addBehavior(ballBehavior);
    }
    
    update(){
        this.ballAngle += this.ballVelocity;
        
        //Constrains the angle between 0 and 2pi
        if(this.ballAngle > Math.PI * 2) this.ballAngle = 0 + this.ballAngle - Math.PI * 2;
        }else if (this.ballAngle < 0) this.ballAngle = Math.PI - this.ballAngle + Math.PI * 2;
        
        //Maybe a little spinback on the pointer?
        this.ballPosition = new Sup.Math.Vector3(Math.cos(this.ballAngle) * wheelActor.getLocalScaleX() * 100 / 100, Math.sin(this.ballAngle) * wheelActor.getLocalScaleX() * 100 / 100, 0);
            
        if(Sup.Input.wasKeyJustPressed("LEFT")) this.ballVelocity = 0.5;
    }
}


dont declare all variables into constructor, always on top of this

Deleted 8 years ago

Thanks!

(1 edit)

Also, how would one be able to call on properties stored in an actor's behavior? Like, if I wanted to grab the velocity from an object's behavior and that property was public, how would I reference it? Calling Sup.Actor.getBehavior just returns the class itself, not the instance of the class that I'm trying to refer to.

(+1)

Modifying Sup.Actor,getBehavior() does change the properties of the instance. If you have two actors in a scene with the same behavior and the variable "health" altering "actorA.getBehavior(BEHAVIOR).health" does not affect "actorB.getBehavior(BEHAVIOR).health".

That's interesting, I can't seem to access properties that I've set in the class itself, and am only able to access variables that exist on Sup.Behavior as the superclass.... Strange. I'm sure I can figure it out, though.

(+1)

All of the properties in a behavior class can be accessed from within itself by using "this.propertyName". The actor it belongs to can be accessed with "this.actor". If you want a more advanced way of setting up an actor when created, you can add awake() which happens instantly, or start() which happens in the next "tick" of game logic. They are used in the same way update() is, but only called once.

class ballBehavior extends Sup.Behavior{
    private angle: number = 0;
    private position: Sup.Math.Vector3 = new Sup.Math.Vector3();
    private velocity: number = 0;

    awake() {
        // You can add code here that will be called only once when it's created.
    }

    update(){
        this.angle += this.velocity;
        
        //Constrains the angle between 0 and 2pi
        if(this.angle > Math.PI * 2) this.angle = 0 + this.angle - Math.PI * 2;
        }else if (this.angle < 0) this.angle = Math.PI - this.angle + Math.PI * 2;
        
        //Maybe a little spinback on the pointer?
        this.position = new Sup.Math.Vector3(Math.cos(this.angle) * wheelActor.getLocalScaleX() * 100 / 100, Math.sin(this.angle) * wheelActor.getLocalScaleX() * 100 / 100, 0);
            
        if(Sup.Input.wasKeyJustPressed("LEFT")) this.velocity = 0.5;
    }
}

If you want to create an actor with a behavior, you just need to use addBehavior() to give it one. You can even change it's default properties too.

// Create a new ball with a behavior.
new Sup.Actor("Ball").addBehavior(ballBehavior);

// Create a new ball with a behavior that starts with altered properties. (almost like a constructor, but every property is optional)
new Sup.Actor("Ball").addBehavior(ballBehavior, {
    angle: Math.PI,
    position: new Sup.Math.Vector3(2, 2, 2),
    velocity: 10
});

// Create a new ball with a behavior that has a different velocity. (properties are optional, so you only need to change the ones you want)
new Sup.Actor("Ball").addBehavior(ballBehavior, {velocity: 10});

If you want to modify the properties after creation, getBehavior() allows you to access the properties in an actor.

// You can do something like this...
let actor = Sup.getActor("Ball");
actor.getBehavior(ballBehavior).velocity = 0;

// Or this...
let actor = Sup.getActor("Ball").getBehavior(ballBehavior);
actor.velocity = 0;

Hope this helps! :)

(+1)

Thanks! I ended up figuring this out when I realized that it wasn't pure JS that added the ability to have classes, it was TypeScript... so I was digging through the wrong documentation entirely. My issue is that I wasn't defining variables properly with name: type, and was instead doing let name = type;