Help with cookies and such

ive already done a little bit of research on the forums about saving. according to a few pages, you have to use cookies to store the progress and stuff. and, to use cookies, you have to store them with the setCookie and document.cookie functions, according to w3schools. i also do not know how to use strings, so i need a bit of help. thank you.

Hey @lazybutter, you might find this thread useful:


In it, blurredPixels shares a cool example of cookies being used to save data inside of the editor!

As a side note, since you seem a bit new to coding, I’d recommend trying to use the local storage first to save strings.

(click this text)

I’m not sure how well you are with strings, so I’ll try to define what a string is starting from zero.

string = A type of value that can have any type of character in it.

A basic example:

var name = "bob"

This will set the value of name to “bob.”
The string is “bob,” and is written in quotation marks to help the computer recognize it as a string.

Strings can be added together, and unlike numbers, when added, they are “joined.”
Here’s an example:

var name = "bob"
alert("Hello there "+name+", how are you?");
// Expected results: "Hello there bob, how are you?"

When adding "Hello there " with name with “, how are you?”, you get “Hello there bob, how are you?”

1 Like

don’t use cookies to store game data. use localStorage. cookies were originally created to store data sent from the server, and the browser sends your cookies to the server whenever you load a page. this is used to, for one, store login session IDs, so the user doesn’t have to enter their username and password every time they go to a different page. i mean you could use cookies, but localStorage is better suited for your purpose.

3 Likes

thanks, @Hamzah_Al_Ani and @pumpkinhead , but i dont really know how to use localStorage. i mean, for example, if i made a global variable, like project.pilltotal, but then every time you choose a pill between the blue pill and the red pill, then it would add to two variables, project.redpills and project.bluepills.then, you add up the total of the two, which would be project.pilltotal. of course, you would have a text thing so you can tell which is which. but, if you refresh the page, then how would you save that variable?

ive been researching a bit more, and i found a few examples already. i now understand how strings work. the problem is that i still dont know how to save global variables with localStorage, like project.cardboardtotal and such. how does that work?

you can save something to local storage like this

localStorage.setItem('myCat', 'Tom');

Sorry for the late response, this might help with the local storage:


To summarize:

To save a variable on the local storage, you’d do as Jordy mentioned above:

window.localStorage.setItem('name', value);

(Replace “name” with the name of a variable, and keep it as a string, and replace “value” with what you want the value of the variable to be).

To get the value of a saved string, I believe you’d use this line of code:

window.localStorage.getItem('name');

(Replace “name” with the name of the variable you want to get from the storage)
Keep in mind that if you didn’t save anything with the name you use here in the storage, you will get a value of “null.”

A default script of:

project.cardboardtotal = window.localStorage.getItem("cardboardtotal");

with an update script of:

window.localStorage.setItem("cardboardtotal" , project.cardboardtotal);

Should save all changes you make to the variable, project.cardboardtotal. Keep in mind that this variable will be equal to null at first since you don’t have any saved values for it in the storage, in that case, you should add a load script with the following code:

if(project.cardboardtotal===null){
project.cardboardtotal=0; 
}

(This checks if it’s equal to null, and if it is, it sets it to zero)

I hope this helps :+1:

thank you @Hamzah_Al_Ani,@pumpkinhead, and @Jordy. i basically got everything i learned into a wick project example. you can just share it to help people get a good idea of how to use localstorage with global variables. thanks though.

localStorageWithGlobalVarExample10-17-2022_10-53-23.wick (11.4 KB)

1 Like

Wouldn’t it initially be undefined? Or is my thinking wrong?

when creating the example, it was first undefined, and then once i tried adding something to the undefined variable, it turned into NaN. so, yes, your thinking is not off.

That’s odd, I always get null instead of undefined when working with the local storage.

Here’s another quick test I did in the console:
image

@pumpkinhead I think it just gives you null and not undefined for an item inside of the local storage that doesn’t have a value.
(Let me know if I’m wrong or if I misunderstood the question)

Oh, I thought you were talking about Objects.

You were talking about Objects, but I didn’t see that you first set the value of project.cardboardtotal to a result from localStorage before checking if it was null.

1 Like