[SOLVED] Problem with undefined keyboard

I am making an Fps game and need help fixing this code that should Swap weapons when a key is pressed but I just can’t get it to work.

// code here

var WeaponSwap = pc.createScript('weaponSwap');

WeaponSwap.attributes.add('Weapon1', {type : 'entity'});
WeaponSwap.attributes.add('Weapon2', {type : 'entity'});

WeaponSwap.prototype.initialize = function(){

};

// update code called every frame
WeaponSwap.prototype.initialize = function(){
    
};

   if(this.app.keyboard.isPressed(pc.KEY_Q)){
    this.Weapon1.enabled = true;
    this.Weapon2.enabled = false;
}
   if(this.app.keyboard.isPressed(pc.KEY_E)){
    this.Weapon1.enabled = false;
    this.Weapon2.enabled = true;
}

Hi @Connor_Briggs,

Are you getting any error when running it?

yes

Also if i don’t respond i am at school i have so free time now but i got some time till the bell rings

This should be in the update function not the initialize (that is when all scripts are running and everything is loaded)

okie thank u i’ll try that

1 Like

It keeps on saying keyboard is undefind

Can you send a link to your project?

Da code is under the arms entity

It should be on the player

okie i’ll try dat

it still says that keyboard is undefined

Hi @Connor_Briggs! Did you have the lines below in your initialize function?

this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);

no but i could try that

nope

it didn’t work

var WeaponSwap = pc.createScript('weaponSwap');
WeaponSwap.attributes.add('Weapon1', {type : 'entity'});
WeaponSwap.attributes.add('Weapon2', {type : 'entity'});
// initialize code called once per entity
WeaponSwap.prototype.initialize = function() {
  this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
};

// update code called every frame
WeaponSwap.prototype.update = function(dt) {
    
};
if(this.app.keyboard.isPressed(pc.KEY_Q)){
this.Weapon1.enabled = true;
this.Weapon2.enabled = false;
}
 if(this.app.keyboard.isPressed(pc.KEY_E)){
this.Weapon1.enabled = false;
this.Weapon2.enabled = true;
}

For some reason it still is saying that keyboard is undefinded

Your if statements are not in a function. this.app doesn’t exist outside the script functions. Your keyboard .on event handlers reference a function this.onKeyDown and this.onKeyUp, you need to create those functions. For example:

WeaponSwap.prototype.onKeyUp = function() {
    if (this.app.keyboard.isPressed(pc.KEY_Q)) {
        this.Weapon1.enabled = true;
        this.Weapon2.enabled = false;
    }
};

I would actually recommend not using onKeyDown, unless something continuous needs to happen (like moving forward). For something that happens just once at a time, I’d recommand only using onKeyUp for swapping weapons. If you use onKeyDown, your weapons will be swapped once like every a couple milliseconds until you stop holding down the button. onKeyUp on the otherhand triggers only once at a time.

2 Likes

Thank you all so much

I got it to work

Good that you have solved the problem with a little help. Next time, try to give your topic a better title so that everyone can easily see what the topic is about. Also try to avoid posting many short messages in a row.