1

I don't know how to import and Export the localstorage data in a godot game. I use localstorage on my entire website and I thought it would be cool if people could trade the items of different games. I only would like to know how to import for example coins, do some stuff with them (coins+=10) and then I would like to save them in localstorage again. So the coin variable is always in localstorage and godot is completely working with localstorage.

I just tried to import the variable coins and asked chatGPT but nothing worked.

User3455
  • 11
  • 1

1 Answers1

0

I don't know what do you want exactly, but I'll try :

First :

if you want to store any objects, or arrays in localStorage, I made a whole system in javascript for doing that.
let's imagine we bave this class :
    class Player {
  constructor(ctx,x,y,width,height,color,...etc){
    ...
  }
  ...
}

now you have this variable you need to put inside local storage :

var playersList = [
new Player(attributes),
new Player(attributes2)
]

you are not allowed to store playerList in localStorage by a simple command like localStorage.setItem("playersList", playersList); but you need to convert them to a text sotue localStorage understands them, with the JSON wonderful API :

//The variable that will handle the text of playerList
var playersListText = JSON.stringify(playersList)

//Horray !!! you can store playersList inside localStorage !!!
localStorage.setItem("playersList", playersListText)

and once you need to get the info you just type the opposite of JSON.stringify and it is JSON.parse :

var playersList = JSON.parse(localStorage.getItem("playersList"))

WAIT, you will face a huge problem here : when you use JSON.parse, you will recieve the Player class as an object, how ? the Player class will be converted to a simple object like :

{
  ctx: someValue,
  x: someValue,
  y: someValue,
  width: someValue,
  ...etc
}

so you should here loop all the objects and turn them into Player objects just like that :

var playersList = JSON.parse(localStorage.getItem("playersList")
playersList.forEach(function(player){
  if(player.type == "player") { //Here you maybe have multiple objects so you do that to separate them
    player = new Player(
      player.ctx,
      player.x,
      player.y,
      ...etc
    )
  }
})



Second :

You have a number in localStorage and you need to have some operations in it, simple !
var number = Number(localStorage.getItem("coins"))
console.log(`your added coins : ${number + 10}`)
//Include it also in for loops !
for(let i=0 ; i<=number ; i++) {
  console.log(i)
}

Hope I helped you, if you want something else, just ask me

hasan darwish
  • 117
  • 1
  • 6
  • Do I have to use json? I tried it like this: extends Area2D var coins = JavaScript.eval("localStorage.getItem('coins')") var coinsCalc = int(coins) func _on_coin_body_entered(body): if body.name == "player": coinsCalc += 1 var coinsString = str(coinsCalc) var a = JavaScript.eval("localStorage.setItem('coins'," + coinsString + ");") OS.execute("eval", a) queue_free() – User3455 Jul 08 '23 at 09:04
  • I don't know the godot engine until I searched about it, and I don't know what is the exact language you used, but for Javascript, instead of using `JavaScript.eval("localStorage.getItem('coins')")` you can use the Number function (I don't know the format in the engine) or you an write for example `JavaScript.eval("Number(localStorage.getItem('coins'))")`. please more describe the problem, because I didn't know any one of these functions you used, I thought you want to build everything in js. – hasan darwish Jul 08 '23 at 09:35
  • Would it help if i host the project and sent you a link? I think you are really experienct and may find the error fast if you have the project in Front of you. – User3455 Jul 08 '23 at 12:17
  • Why not ? I maybe discover the problem, but shall I have godot to see the file ?? – hasan darwish Jul 08 '23 at 12:39
  • here is my project https://my-simple-storage.netlify.app/ i also put all in a zip package that you can download it. Sorry that it needed so much time. (the big black stone that flys should represent the coin) – User3455 Jul 08 '23 at 13:44
  • Ok I'm in Syria so the time while I am replying is night, so I will see it in the morning and I'll reply, please forgive me if the time is not good but electricity doesn't come only 1 hour every 5 hours so the time is so limited to answer, also tell me if the file should need godot engine or not to open – hasan darwish Jul 08 '23 at 19:02
  • Yes you would need godot but maybe you can find the error in the terminal of the Website. I also saw that it could work with postMessage because the godot game is in an iframe but i wasn't able to Do it – User3455 Jul 08 '23 at 23:25