Coding King2
Coding King2

Reputation: 37

Declaring as an existing array doesn't work

Before you read this code and get exhausted, just know its an open source JavaScript library called p5.js

https://p5js.org/reference/

I have the basic package and no add-on's. just look at this...

var weapons = {
    //[WEAPON NAME]: [accuracy,repeat,recoil,recoilmax,recoil-recovery,damage,firerate,reloadspeed],
    M4A1: [10, 10, 0.2, 5, 1],

    //primary: weapons.M4A1,//ERROR
    //secondary: weapons.M4A1,//ERROR
    equipped: weapons.M4A1,
};

var inventory = {
  accuracy: weapons.M4A1[0],
  accuracyREF: 10,
  recoil: 0.2,
  recoilMAX: 3,
  recoilRecovery: 1,
  damage: 1,
  fireRate: 1,
};

Shows a blank screen till i get rid of the line that declares weapons.equipped

I ALSO TRIED USING SPLICE AS SHOWN BELOW IN p5.JS syntax SAME GOES FOR arrayCopy()

var weapons = {
  //[WEAPON NAME]: [accuracy,repeat,recoil,recoilmax,recoil-recovery,damage,firerate,reloadspeed],
  M4A1: [10, 10, 0.2, 5, 1],

  //primary: weapons.M4A1,//ERROR
  //secondary: weapons.M4A1,//ERROR
  equipped: [0,0,0,0,0],
};

splice(weapons.M4A1, equipped, 0);

var inventory = {
  accuracy: weapons.M4A1[0],
  accuracyREF: 10,
  recoil: 0.2,
  recoilMAX: 3,
  recoilRecovery: 1,
  damage: 1,
  fireRate: 1,
};

This is essential for my game and I will have to abandon it if this cant be done

Upvotes: 0

Views: 50

Answers (2)

Paul
Paul

Reputation: 2076

Another solution using getter functions. For an explanation why errors occur just check the answer of @bloodyKnuckles.

var weapons = {
  //[WEAPON NAME]: [accuracy,repeat,recoil,recoilmax,recoil-recovery,damage,firerate,reloadspeed],
  M4A1: [10, 10, 0.2, 5, 1],
  get primary() {
    return weapons.M4A1
  },
  get secondary() {
    return weapons.M4A1
  },
  get equipped() {
    return weapons.M4A1
  },
};

var inventory = {
  accuracy: weapons.M4A1[0],
  accuracyREF: 10,
  recoil: 0.2,
  recoilMAX: 3,
  recoilRecovery: 1,
  damage: 1,
  fireRate: 1,
};

console.log("weapons", weapons);
console.log("inventory", inventory);

Upvotes: 1

bloodyKnuckles
bloodyKnuckles

Reputation: 12079

var weapons fails because you're trying to assign one of it's properties to itself and it doesn't exist yet. Therefore var inventory fails because weapons is undefined due to the first error.

To understand what's happening, start here:

var weapons = {
  M4A1: [10, 10, 0.2, 5, 1],
  equipped: weapons.M4A1 // <-- weapons.M4A1 must be determined first
};
// weapons === undefined

Before instantiating var weapons JS needs to process the value of weapons.M4A1 in order to assign to equipped. The problem is weapons does not exist yet.

Next you're trying to instantiate inventory.

// remember, weapons === undefined
var inventory = {
  accuracy: weapons.M4A1[0], // again, weapons.M4A1[0] must be determined
  accuracyREF: 10,
  ...
};
// inventory === undefined

...therefore, inventory also fails to instantiate.


Attempt #2: splice(weapons.M4A1, equipped, 0);

p5/splice states splice is for arrays, not objects, (among other problems with your implementation).


Can you do this?

var weapons = {
  M4A1: [10, 10, 0.2, 5, 1],
  equipped: [10, 10, 0.2, 5, 1]
};

var inventory = {
  accuracy: weapons.M4A1[0],
  ...
};

Or this?

var weapons = {
  M4A1: [10, 10, 0.2, 5, 1],
  equipped: [0,0,0,0,0]
};
weapons.equipped = weapons.M4A1;

var inventory = {
  accuracy: weapons.M4A1[0],
  ...
};

Upvotes: 0

Related Questions