How to use variables as attributes

Basically I have different types of bullets, and I want specific guns to use different bullets. The bullets are global variables. How do I turn an attribute into a specific variable.

Not sure how you’ve done this but to answer your question directly, you can’t do this.

Add an attribute on a script type means that it will create a property of the same name on the script type instance

For this type of thing, I would use enums to say what type of bullets it uses and then use a switch or if statement wherever you need specific logic on the type of bullet

https://developer.playcanvas.com/user-manual/scripting/script-attributes/#enumeration-attribute

1 Like

nonono I mean like
Fivefivesix is a global variable, I want the gun to use the variable, But I cant add “if(Fivefivesix >= 0)” because that will just prevent using Ninemillimeter. So I need some sort of inbetween that can take on the responsibilities of a global variable without having to be physically inserted into the code

Its either that, or a massive:

if(this.caliber === 1)
{
this.gunvar= Fortyfive
}
else if(this.caliber === 2)
{
this.gunvar = Sevenpointsixtwo
}
else if(this.caliber === 3)
{
this.gunvar = Ninemillimeter
}
else if(this.caliber === 4)
{
this.gunvar = Sevenpointsixtwo
}

Technically that would work, but it would be a massive pile of laggy trash.

Either that or make multiple nearly identical scripts with the only difference being the variable.

The bullets are literally just amounts.

It won’t really affect performance in any measurable way tbh.

Edit: if you wanted to improve this, you can put this logic in an array/dictionary so that you can directly index the value you need

That can be optimized 1 of 2 ways, a switch case or an array.
This is an example of using a global array to get the bullet values.
You could have a global array with the bullet types in their enum values

BulletArray = [Fortyfive,Sevenpointsixtwo,Ninemillimeter,Sevenpointsixtwo];

and in the gun script you could have

Gunscript.attributes.add("bulletType", {
    type:"number",
    enum: [
         { "fortyFive": 0},
         {"sevenpointsixtwo":1},
         {"nineMillimeter":2},
         {"sevenpointsixtwo":3}
    ]
});

Gunscript.prototype.initialize = function() {
     this.gunvar = BulletArray[this.bulletType];
};
1 Like

@lifeofpain Bear in mind that because Fortyfive etc are POD, it will do a value copy and not a reference copy. It will need a little more changing for that to work

I would avoid the use this.gunvar for simplicity and make the BulletArray global instead of using Fortyfive, Sevenpointsixtwo separately.

I would have to have that running every single frame, and if i do that people will laugh at me and call me yanderedev :frowning:

Seriously, it’s fine. It won’t hurt performance that is anyway measurable way

I thought of trying to shove the name of the variable into an attribute and parsefloating it but that just gave me a nan

I personally don’t like to use globals but this is how I would do it if I had to:

// In globals

BulletInventory = {
    fortyFive: 0,
    sevenpointsixtwo: 0,
    nineMillimeter: 0,
    sevenpointsixtwo, 0
};


// In script

Gunscript.attributes.add("bulletType", {
    type:"string",
    enum: [
         { "fortyFive": "fortyFive"},
         {"sevenpointsixtwo": "sevenpointsixtwo"},
         {"nineMillimeter": "nineMillimeter"},
         {"sevenpointsixtwo": "sevenpointsixtwo"}
    ]
});

Gunscript.prototype.fire = function() {
    BulletInventory[this.bulletType] -= 1
};

Technically, you can do this but I like this less

// In globals

var fortyFive = 0;
var sevenpointsixtwo = 0;
var nineMillimeter = 0;
var sevenpointsixtwo = 0;


// In script

Gunscript.attributes.add("bulletType", {
    type:"string",
    enum: [
         { "fortyFive": "fortyFive"},
         {"sevenpointsixtwo": "sevenpointsixtwo"},
         {"nineMillimeter": "nineMillimeter"},
         {"sevenpointsixtwo": "sevenpointsixtwo"}
    ]
});

Gunscript.prototype.fire = function() {
    window[this.bulletType] -= 1
};

I was wondering why you were using 7.62 twice, then I realized that its because I did first

I now have to somehow combine all my reload scripts and turn them into booleans.