What approach to use?

what approach to use I want to make use of localStorage to put there array of positions, so then the positions will be persistent even when I refresh the game
but maybe there is other solution?
Additionally I put each position into array on press key P
why I want this: because the position would set the positon of car based on position of trigger, so then the car will go down or up to follow the bump

If you need to persist data on a per browser level, yes localStorage would be your best bet.

Bear in mind that no-one else can access that data unless they access that same browser on the same computer.

Not really sure what you are aiming for though in terms of a feature.

2 Likes

I think there is a misunderstanding, @grzesiekmq. I believe you are refering to this post:

By “store off that position” that Will was mentioning, he did not mean a localStorage or a database, but simply the instance of your application. You can store your variable by attaching it to the script via this, for example:

initialize: function() {
  this.position = new pc.Vec3();
},

add: function() {
  this.position.add(pc.Vec3.UP);
},

update: function() {
  var newPosition = this.entity.getPosition(); // new position each frame
  var existingPosition = this.position;  // existing position, persisted between frames
  
  // do something
}

The key word here “persisted between frames”, not persisted between page refreshes. So, instead of creating a new position every frame, you re-use the existing one, which can be manipulated outside of .update() method.

Also, you generally don’t want to use localStorage for storing the positions of your bumps or whatnot. As @yaustar mentioned, nobody can access that data later, except same browser, same person. The problem will come later, when you want to change the way you store these positions or the way you calculate them, or the way you read them, etc. Some browsers then will have one version of your positions, others another, etc. It all can be managed, but why make it hard for yourself in the first place?

Instead, simply calculate all the positions when your game starts and store them in an array. This way, you will always have up to date coordinates, and you don’t need to worry about the localStorage mess. And by “store them in array”, I mean simply attach it to your script via this, like this.positions = myArray;. It will be available for you while your application is alive, and will be destroyed once it is closed.

2 Likes