I am making a gun game, and it finds the current gun using variables and attributes, and that works fine, but i need a way to be able to disable an entity. The guns are attached to the character, so when you press the switch gun button (keys 1 or 2) it will disable the gun you aren’t using so that way you don’t have two guns floating in your hands.
Something like this?
// update function
if (this.app.keyboard.wasPressed(pc.KEY_1)) {
gun2.enabled = false;
gun1.enabled = true;
}
if (this.app.keyboard.wasPressed(pc.KEY_2)) {
gun1.enabled = false;
gun2.enabled = true;
}
Yes, that I you and sorry, i didn’t have knowledge of a built in way to do this
Wait, will this work even if the entity isn’t attached to the thing that has the script
Yes, as long as you define ‘gun1’ and ‘gun2’, so that the script knows which entities those are.
Wait, when i add the script it is saying (‘app.keyboard.isPressed is not defined’)
Can you show me the code please?
My guess is you need to define app above that portion of the code as in:
var app = this.app;
Or just use the code I sent.
I tried but it said it was undefined still
@Gavin_Durbin could you please share your code so we can have a look?
Yes
Is there a way i can make the weapon change button press in another script, then it sends the info to the other script to use, so that way it is defined because the other one doesn’t have that problem
Hi @Gavin_Durbin,
If you look at the comment in @Albertos 's code, you will see that it needs to go inside of your update function.
Oh….
Ok
Can someone help me code this? I can’t get it to work at all
Hi @Gavin_Durbin,
The forum is certainly happy to help, but it’s difficult without specific questions. You should consider these questions to get better responses:
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)
If you break down each issue you have into chucks with these questions it will make things a lot more simple for both you, and anyone attempting to assist.
I hope this is helpful.
First you have to learn that you can’t put your code everywhere. A script is divided into functions. Placing code outside these functions can cause your script to stop working. Below you can see the two most important functions. The initialize function, which only runs once when you start the game and the update function, which runs continuously.
In your current script, you have to solve the problems below.
- Line 13 is outside a function and you don’t need line 13 in this case.
- Line 28 to line 40 is outside a function and need to be inside the update function.
- The variables
gun1
andgun2
are not defined, probably you want to usethis.weapon1
andthis.weapon2
in this case. - Line 34 to line 36 will not work this way.
- You need to parse the script and drag the weapon entities to the attributes.
If I’m setting a part of the script, such as”this.gun = this.blablabla”
Does it need to be in the update function?
You can do that in the initialize function.