How do I define a variable in JavaScript?

ok so I wanna do something like this:

var entity = ball'
var player = entity.camera();

But I keep getting underfined.
How do I fix this?

Well you have a syntax error first of all, after ball you need a semicolon ; and also … ball has to be be defined somewhere before using it to reference a existing entity.

Otherwise yes, you will keep getting the undefined error.

what do you mean defined somewhere?

Something like this:

var ball = this.app.root.findByName('ballEntityName');

So that’s all I have to do to define a variable?

No, that’s what you do to grab a reference to an entity and store it to a variable.

Here is a small tutorial on how Javascript variables work:

https://www.w3schools.com/js/js_variables.asp

I’ve seen that tutorial before Btw I think I got the code right:

var Al = pc.createScript('Al');

//variables and scripts//
var r;
r = 4;
r = "entity";
r = true;




Al.prototype.e = function() {
    var e = this.entity();
    
};

If you don’t explicitly define an entity() method in your script, PlayCanvas doesn’t provide a method with that name.

Apart from that, yes your code would compile though not sure what is the expected output.

ooh so like there’s a input when your coding there must be a output so basically that’s all coding is

1 Like

Yes, that the most common definition of a computer program: given an input it produces an output by executing a number of methods on it.

oooh I have always thought of coding as talking like you write the code and then the computer magically does it for you.Nah jp but I did think on coding as a way of talking to the computer

1 Like

I think of it as a way of instructing the computer. But I’m not really a programmer, so that may not be the best mindset.

1 Like

The way I think of it, you type in an input to a computer which gathers the info and does as instructed, giving an output. Basically, it’s all just stating rules and logic and using some of that logic to create something functioning (basically a game).

1 Like