I am trying to create a javascript game in which a player object has a bag/inventory with a set number of slots (similar to a Minecraft hotbar). To do this, I am trying to create an array, or new object, inside of my Player
class. I imagine I could access each slot like the following:
player.inventory.slot_1 = "stick"; OR player.inventory[1] = "stick";
Where I could store each item as either an ID or string. However, doing so has not worked for me. Here is what I have attempted:
class Player {
constructor() {
// Player inventory
let inventory = {
slot_1 : null,
slot_2 : null,
slot_3 : null,
slot_4 : null,
slot_5 : null,
offhand : null,
armor : null
}
console.log("Player constructed!");
}
}
But VSCode tells me that the inventory object is "declared but its value is never read.ts(6133)".
If I try to use the structure, I get the following error: engine.player.js:36 Uncaught TypeError: Cannot read property 'slot_1' of undefined
How can I work around this, or reproduce the effect?