I have a problem, basically I have to retrieve a value from sessionStorage to change the value of a variable.
I have a browser page with a camvas and some buttons, which trigger some games:
In the functions I have put alert to see if it entered, it is not relevant.
<canvas id="myCanvas4" width="890" height="590"></canvas>
<button onclick="loadScript('LASERV1Partida/GameLaser.js',function(){alert('de momento se quea')})">boton1</button>
<button onclick="loadScript('REPARACIONESV1Partida/GameRepairs.js',function(){alert('y de momento se quea tambien')})">boton2</button>
I then continue the script and add the following. I start session storage with the values of some variables, I do some console log to see how much they are worth and check it.
window.sessionStorage.setItem('structure',10); //modificando recursos de estructura
window.sessionStorage.setItem('health',5); //modificando recursos de estructura
console.log("estructura inicial"+window.sessionStorage.getItem('structure').valueOf()); //modificando recursos de estructura)
console.log("salud inicial"+window.sessionStorage.getItem('health').valueOf()); //modificando recursos de estructura)
then, when the game is played, win and lose conditions are set, if victory occurs nothing happens, but if a defeat occurs the variables in window.sessionStorage are modified:
function Fracaso(){
if(Derrota==true){
alert("MISSION FAILED\nTOO MUCH DAMAGE");
window.sessionStorage.setItem('structure',window.sessionStorage.getItem('structure').valueOf()-1); //modificando recursos de estructura
window.sessionStorage.setItem('health',window.sessionStorage.getItem('health').valueOf()-1); //modificando recursos de estructura}
If I do console.log I see that the values have been changed, i.e. in the fail() script I see that console.log defines me a new value for the sessionStorage values.
The problem and the issue is that I have to change the value of variables from the html where I call the script, which has declared variables and these have to take the new modified sessionStorage value (which in this case, being a failure, are worth 1 minus unit).
how do i do that step?
It is already solved, it was enough to take an html element by its id and pass it the session value, it was not necessary to make a listener, or cookies or anything like that, among other things because the div is in the same browser window .