I need help making save files which can be selected and will save the last level you beat.
I have know I will have to use cookies but I don’t know how to do so I was hoping that someone could help.
Sincerely
William
Boersma
I need help making save files which can be selected and will save the last level you beat.
I have know I will have to use cookies but I don’t know how to do so I was hoping that someone could help.
Sincerely
William
Boersma
Hello to save something
localStorage.setItem(‘name’,player.name); where player.name is the variable you want save
to load something
localStorage.getItem(‘name’) to recover the variable
Hi @ayrin would I be able to call the variable in a different script?
also would it work like this?
localStorage.getItem(‘LVL’)
if (LVL = 1) {
sceneName = ‘lvl 2’
}
If you’re looking for an example of using the LocalStorage API, check out the Flappy Bird project:
https://playcanvas.com/project/375389/overview/flappy-bird
It’s used in the game.js
script:
https://playcanvas.com/editor/code/375389?tabs=4554213
Checking for availability of the API:
// The correct way to verify the availability of the local storage API taken from:
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
function storageAvailable(type) {
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch (e) {
return false;
}
}
Loading the high score from LocalStorage on game startup:
this.bestScore = 0;
if (storageAvailable('localStorage')) {
this.bestScore = localStorage.getItem('Flappy Bird Best Score');
if (this.bestScore === null) {
this.bestScore = 0;
}
}
Updating the stored high score after each game:
// Check if we have a new high score and write it to local storage
if (this.score > this.bestScore) {
this.bestScore = this.score;
if (storageAvailable('localStorage')) {
localStorage.setItem('Flappy Bird Best Score', this.score.toString());
}
}
Hi @will thanks for the help but I’ve done a some tests with it and I think I’m getting the hang of it.
but while I have your attention can you help me here Multiplayer in csgo
hi again @will I was wondering what does this error mean?
It says failed to execute 'setItem' on 'Storage'
. So presumably, you’re doing something like:
Storage.setItem(key, value);
Instead of:
localStorage.setItem(key, value);
oh I forgot the value thanks for the help
hey @will Now there is 2 of the player and causes a lot of lag can you take a quick look over my code?
https://playcanvas.com/editor/code/719734?tabs=41515184,41517491
Fix the part in ontriggerenter where it says if “LVL1”.
var lvlone = localStorage.getItem('LVL1');
if (lvlone) {
NUN = false;
setTimeout(function (){
self.loadScene(self.sceneName);
}, 1000);
}
ok thanks
without quotes.
thanks again
but it did not do anything it’s still the same. @Fus_ion
this isn’t solved though?
just do this instead:
LevelLoader.prototype.initialize = function(){
this.entity.collison.on("triggerenter",this.onTriggerEnter,this);
this.scene = "";
};
LevelLoader.prototype.onTriggerEnter = function(dt) {
var lvlone = localStorage.getItem('LVL1');
if (lvlone === true && this.scene !== "lvl1") {
setTimeout(function(){
this.scene = "lvl1";
this.loadScene(this.scene);
},1000);
}
};
@Fus_ion it does nothing
you have to replace your old code
@Fus_ion I did and it still did nothing