How can i get this variable

I can’t figure out how to code a variable, so when in the editor i can change the amount in the code.

1 Like

Read this article: https://www.kidscodecs.com/variables/#:~:text=In%20software%20programming%2C%20variables%20are,database%20query%2C%20or%20other%20value.

It has the basics for variables, also, it was the first thing on google just so you know.

oh, ok, thnks

If you want more advanced, let me know and I can find another article that has more advanced methods.

Also, your welcome.

Wait,

Do you mean coding a variable that can be changed in the editor with fields in the script component?

@eproasim
YES, cuz im making a shooting script, so variables for ammo, dmg, range, accuracy, that’s what i need

This link might help:

if you want to use the editor to edit the values you need to define it as script attribute. there are many types of attributes like entity, asset, number, string, enums etc. you can find the exhaustive list in:

In the documentation, those parts of a Script Component are referred to as Script Attributes

Of course, I will start with telling you that the best place to start is here:

That being said, I am making an effort to write out detailed answers breaking down the most common questions posed here detailing some of the how’s (at least as I understand them) for interacting with the code in PlayCanvas’s documentation. If someone manages to answer before me, thank you!

Ok, so let’s get started:

By now, you should have already created your new script and have populated with the auto-generated with something like this:

var NewScript = pc.createScript('newScript');

// initialize code called once per entity
NewScript.prototype.initialize = function() {
    
};

// update code called every frame
NewScript.prototype.update = function(dt) {
    
};

// swap method called for script hot-reloading
// inherit your script state here
// NewScript.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

If you take a look at the first line of the script, you will see that our new script is given a variable called NewScript. This is the variable that we will use to to interact with and add properties to our script from within this file. It defines itself as a PlayCanvas Script object with the name “newScript”. That will be the name of the script if you ever try to access information on this script from another script.

“I asked about Script Attributes!” you say. I know, and we’re getting there.

So, now that we’ve define our scripted we want to create a property of the script that can be edited within the Playcanvas editor. To do that we will need to access the script object that was created with its variable “NewScript”, and then start diving into its attributes. We would do this by using the period character and then writing attributes:

NewScript.attributes

Great, now we’re in the attributes section of our script object, but that’s not helpful unless we’re adding something in our case. Luckily, the attributes section has a built in function to add an attribute. We access it by adding add() after attributes:

NewScript.attributes.add();

Now we can get to the meat and potatoes of what you want to do. After all, the add function needs to know exactly what it is that you want to add to the editor. The add function accepts two arguments. The first one is a string. This string is the name of this attribute in code. The next argument is an object that tells PlayCanvas exactly what this attribute is supposed to represent. For now, let’s just put an empty string and an empty object that we can build from:

NewScript.attributes.add('', {});

Since you’re interested in things like Ammo, range, damage, etc, it looks like you will ne looking for a number type of variable. Let’s start by making a damage attribute and naming our attribute ‘damage’:

NewScript.attributes.add('damage', {});

Great! But this still won’t work. We still need to tell PlayCanvas what damage should be. Here is where you define that. The object that is the second argument for our add function is wrapped in curly brackets {}. Let’s go ahead and tell PlayCanvas that we are working with a number here. You can always check ScriptAttributes | PlayCanvas API Reference for all of the parameters you can set here. Right now We will only be working with type, title, description, and default. The type value we’re passing will always be a string, so it should be wrapped in quotes.

NewScript.attributes.add('damage', {
	type: 'number'
});

Now let’s tell PlayCanvas exactly who damage is. Since we’re working with an object, you will want to separate your parameters with a comma. Let’s go ahead and give your attribute a title and description. The title and description values we’re passing will always be strings, so they should be wrapped in quotes. The Title value is what will appear in the editor as the name for the field, and the description is what will appear when you hover over the title in the editor:

NewScript.attributes.add('damage', {
	type: 'number',
	title: 'Damage',
	description: 'The damage value for this object.'
});

This is all that you will need to have a fully labeled attribute in your editor. You will want to click the parse button on either your script asset, or your script component on the object that this script is applied to.

Let’s say we want this field to always have a value without you specifying one when you add this script to your object. You can optionally define a default field so that you don’t have to worry about always setting something. Here your value will change because PlayCanvas is expecting to receive a number for the default. Let’s look below:

NewScript.attributes.add('damage', {
	type: 'number',
	title: 'Damage',
	description: 'The damage value for this object.',
	default: 100
});

Notice how I didn’t wrap the number in quotes? This is because the number is it’s own type of value. The same is true for true/false or boolean values. If you wrapped 100 in quotes, PlayCanvas would complain because it received a string instead of a number. Again I highly recommend you give the documentation a once over here:

…as it gives a nice window into what you’re allowed to put inside of this object.

Remember that you need to click ‘parse’ on the script in the editor after adding an attribute, or it won’t appear.

Now that you have a functioning attribute in your script you can call it elsewhere in your script using ‘this’ and the name of your attribute. In this case ‘damage’. As an example, if I wanted to see the value of of damage on startup, I would do something like this:

NewScript.prototype.initialize = function() {
    console.log(this.damage);
};

I hope this is helpful. I hope to start building a collection of these detailed answers in the future for commonly asked questions like this. Everyone else in the community, feel free to tell me if something is missing or could be better explained!

3 Likes

@eproasim
wow, thank you very much