[SOLVED] How do I make a hotbar switch?

How do i make a hotbar switch (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) between guns and flashlite?

Hi @thebosser24,

There are many ways to approach this, a simple one:

You can monitor what key was last released and set your active inventory item to that. Basically disable all inventories items, except the active one.

Pseudocode:

if( this.app.keyboard.wasReleased(pc.KEY_3) ){
   activeIndex = 3;
} 

myInventoryItems.forEach( function(item, index){
   if(index === activeIndex){
      item.enabled = true;
   }else{
      item.enabled = false;
   }
});
2 Likes

do i copy and paste this several times for each item, and how do i assign an item to each key, like flashlight=1 gun=2

how do I make an inventory

No, you can’t use it as it is, it was meant to show you example logic. You will have to adapt it to your code.

1 Like

im sooooooo confused, do i change
seudocode:

if( this.app.keyboard.wasReleased(pc.KEY_3) ){
   activeIndex = 3;
} 

myInventoryItems.forEach( function(item, index){
   if(index === activeIndex){
      item.enabled = true;
   }else{
      item.enabled = false;
   }
});

to
seudocode:

if( this.app.keyboard.wasReleased(pc.KEY_4) ){
   activeIndex = 4;
} 

myInventoryItems.forEach( function(item, index){
   if(index === activeIndex){
      item.enabled = true;
   }else{
      item.enabled = false;
   }
});

Can you share some of your existing code on how you handle your inventory / items?

If you don’t have any, my code isn’t of much use to you. For a UI interface the closest example I can think of is this:

https://developer.playcanvas.com/en/tutorials/tutorial-shop-user-interface

what am i doing, i made a script, how do i attach it to gun

Is it possible to do a If press 2 enable Handgun, when press 1 disable handgun

im trying to do an inventory, is it possible to do that?

Hi @thebosser24,

It is actually possible you have to make UI for the inventory.

https://developer.playcanvas.com/en/user-manual/user-interface/user-interface-basics/

Then make a code for it so when you click it you go to the inventory.

@thebosser24 please use your existing topics about the same subject rather than creating new ones

i forgot this existed. im sorry

but i don’t want a user interface, unless I have to have 1
i want
When you press 2 it brings out pistol
when you press 1 it brings out flashlight
when you press 3 it brings out Shotgun

You can do something like below for every key.

if (this.app.keyboard.wasReleased(pc.KEY_1) ){
   this.app.root.findByName("Flashlight").enabled = true;
   this.app.root.findByName("Pistol").enabled = false;
   this.app.root.findByName("Shotgun").enabled = false;
} 

SWEET, do i put this in its own script and attach it to each, or in the Movement script?

You can add it to your movement script.

so do i do this

if (this.app.keyboard.wasReleased(pc.KEY_1) ){
   this.app.root.findByName("Flashlight").enabled = true;
   this.app.root.findByName("Pistol").enabled = false;
   this.app.root.findByName("Shotgun").enabled = false;
} 
if (this.app.keyboard.wasReleased(pc.KEY_2) ){
   this.app.root.findByName("Flashlight").enabled = false;
   this.app.root.findByName("Pistol").enabled = true;
   this.app.root.findByName("Shotgun").enabled = false;
} 
if (this.app.keyboard.wasReleased(pc.KEY_3) ){
   this.app.root.findByName("Flashlight").enabled = false;
   this.app.root.findByName("Pistol").enabled = false;
   this.app.root.findByName("Shotgun").enabled = true;
}

Yes, exactly. Make sure the names of the weapons are the same in your scene.

do i add it at the end or initialize

when i switch weapons ccan it switch images(the crosshair)

Hi @thebosser24,

Put it in your update function like here:

But for your question

It won’t you will have to create a different script for that.

on line 46?

No, in your update function that was the script I have for my game which I just used for an example.

i did that, but does it stay enabled or do i have to make a script to disable it