0

I'm building a simple video game and I'm trying to find the best way to remove objects from and array that is the player inventory.

I want to use an ID for each game item but I'm not sure how to generate those IDs.

Obviously it wouldn't be productive to manually give every instance a unique Id.

I was thinking I could add a property on the constructor functions prototype or just directly on the constructor itself, called generated that increases by 1 with each instance created from it and then have each instance reach back up and use that as its ID.

Alternatively I could just randomly generate a random number for each object on its creation, however even with a large number their is very very small risk that you could have multiple objects with the same ID.

How I can add a unique ID to every object instance?

  function Item(name,weight,value,description,type){
        this.name=name;
        this.weight = weight;
        this.value=value;
        this.description=description;
        this.type=type;
        this.id= this.generated;/*"this" here obviously means the a property immediately on the object its self on not something further up the chain*/

        this.generated+=1;
     }
    Item.prototype.generated=0;

  function Item(name,weight,value,description,type){
        this.name=name;
        this.weight = weight;
        this.value=value;
        this.description=description;
        this.type=type;
        this.id= this.__proto__.constructor.generated;/* this doesnt work either I'm assuming maybe because the constructor and __proto__ properties are added after everything in the constructor function runs, so its undefined?*/

        this.__proto__.constructor.generated+=1;
     }
     Item.generated=0;
Nope
  • 22,147
  • 7
  • 47
  • 72
  • 2
    What are you asking? Remove an object or create a unique id? – Lars Peterson Sep 05 '17 at 08:45
  • I'm asking how to generated unique ID for object instances –  Sep 05 '17 at 08:48
  • You already seemt to have the right idea with how to make UID's. All you really need is a global number that increments every time you need a new ID. I can give a more comprehensive explanation, but I'd need to ask first if the player inventory in your game is meant to have a maximum size or not. – Mr. Reddy Sep 05 '17 at 08:58
  • Please avoid big paragraph, the question is not even what you are asking for ... – Nolyurn Sep 05 '17 at 09:03
  • thanks mr.smith, I could do that but the problem is I have multiple different types of item constructor functions and if I'm using global variables and not some encapsulated generated property. I think its going to make the more difficult to access because I need to be aware of about a 100 different generated variables for each different object type, with different names. Preferable I could have just a different generated properties with the same name but encapsulated and easily accessible to the object instances it relates to. –  Sep 05 '17 at 09:08
  • @user8398574 See this for generating Unique identifiers in Javascript ► [**Create GUID / UUID in JavaScript?**](https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript) I have used this myself in the past and yet have to encounter a duplicate number. – Nope Sep 05 '17 at 09:25
  • Possible duplicate of [Create GUID / UUID in JavaScript?](https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript) – Vanquished Wombat Sep 05 '17 at 11:25

1 Answers1

1

Use a closure with a counter that is incremented each time an object is made for example:

var item=(function(){
var id=0;
return function(name,...){
this.name=name
...
this.id=id;
++id; 
}
})()
//to remove an object use Array.splice()
user7951676
  • 377
  • 2
  • 10