Changing variables to change weapons

Can someone help me use variables to change the weapon. When a key is pressed (the weapon that is equipped with the press follows the character bone, the unequipped one gets disabled) i try to use variables and attributes but i can’t get it too work.

Hi @Gavin_Durbin,

This is a good example of a question that is vague enough to answer in a number of different ways. Could you elaborate on what exactly you’re trying to do and what you have tried? Are you attempting to just keep track of the weapon as it changes, or literally wanting to change a variable that changes the weapon? Are there different actions that would need to take place when changing the weapon? (animations, effects, ammo counts, etc.)

Try to keep these questions in mind when making a post as it makes it easier for others to help you:

What behavior am I trying to code?
What code have I tried in order to implement this behavior? (Include this code for others to help)
What behavior is my project exhibiting now?
What (if any) errors is my project generating?
How might others get to my project?
Why do I think the code I have used is not working? (Mostly optional, but can help with troubleshooting on your own)
1 Like

If i use attributes for my guns it won’t work, so i need to use multiple variables I’m thinking, so when a key is pressed it will change the current weapon to another variable, but I don’t know how to do this, its kinda confusing

I’m trying to make it where you can switch guns in my game

@Albertos
Do you think this would work?


I’m trying to use attributes and such to make weapon swapping for the game you helped my fix (the weird raycast direction)

Hi @Gavin_Durbin,

So I think the best way to get to the right answer here is Rubber Duck Debugging:

Let’s talk through what’s going on in your script.

When a key is pressed (1 or 2), you:

  • Search the current entity for a child that goes by a specific name and call it this.current.
  • Search the current entity for a child that goes by a specific name and call it this.notcurrent.
  • Disable the entity that you call this.notcurrent.

It seems like you have a couple of steps missing. What do you think those might be?

One option you might want to consider is to keep track of whatever the current weapon should be (updating it with each weapon change). Then you would need to:

  • Disable the current weapon
  • Find the desired weapon
  • Enable the desired weapon
  • Declare that the desired weapon is now the current weapon

I hope that is helpful

2 Likes

I don’t get it. I have the weapons labeled, both the current and not current, i don’t see how it wouldn’t work

https://playcanvas.com/editor/scene/1233246?scrlybrkr=e3fb3fcb

Let’s look at the script you posted again.

When a key is pressed (1 or 2), you:

  • Search the current entity for a child that goes by a specific name and call it this.current.
  • Search the current entity for a child that goes by a specific name and call it this.notcurrent.
  • Disable the entity that you call this.notcurrent.

Look at each of those steps carefully. As it is written, the only thing it does is disable this.notcurrent . Break down each of the steps that you want to complete into bite size chunks that you can work with and individualize each action that you are trying to do. It will feel slow and tedious at first, but you will find that by slowing down, you will actually finish things more quickly.

2 Likes

I think i know, enable current

Well, it seems to work but it is saying “undefined is not an object (evaluating ‘this.keyboard.isPressed’)”

Would this work?


What I mean is that will the “this.current” change when the key is pressed, because i get an error because when the game starts there is no assigned weapon, so can i assign a weapon first, and then change it?

Your thought process is good there. Does doing it that way generate any errors?

1 Like

no errors, but it says this child entity undefined, and I’ve had it enough to know that it means weapon or current is undefined or wrong

It looks like you are defining current as a variable inside of initialize like this:

var current = this.entity.findByName('m4');

When you call the function later you use:

reparentLockTransform(this.current, this.rhand, this.scaleOffset);

But you never defined this.current

1 Like

Is there a way to use var current instead of this.current

When using var inside of a function in javascript, it is only available within the scope of that function, meaning you can only use it inside of your initialize function. If you want something to be available to the entire script, you would use this.randomVarName which makes it part of the entire script object.

Is there a reason you do not want to use this.current?

1 Like

Because it won’t change on button press, or at least in my experience of tests

And if i do it in the update function it will update each frame to the default, so you’d have to hold down 2 or 1 to switch it

image
So would i have to use variables like this?
Or can i useThis.weapon = this.current Being able to change it’s value