Page 1 of 2
TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 6:23 pm
by Philocalist
Hi,
I'm making a tease and got to the point where I want to store some variables.
I have a player variable and it contains a lot of information (in variables of course).
The problem is, that when I try to save it, it doesn't seem to work. I don't want to do it for each member, because then I will end up with 100+ variables to be saved individually and I did not even mention the scenario when I want to modify these vars later and having to edit it in the saving function.
My question is: what am I doing wrong?
Code is like this:
var player={
stuff:"",
otherStuff:{member:0,member:1,},
currentSaveSlot:"Slot1",
}
function saveGame()
{
teaseStorage.setItem(player.currentSaveSlot+"Player",player);
}
function loadGame(slot)
{
player=teaseStorage.getItem(slot+"Player");
}
Any help is appreciated.
Ph.
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 7:12 pm
by kerkersklave
I seem do be able to store complex objects just fine.
Did you enable the storage module in the settings?
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 7:13 pm
by Philocalist
Of course I enabled it, thanks for the reply.
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 7:21 pm
by Philocalist
kerkersklave wrote: Mon Dec 23, 2019 7:12 pm
I seem do be able to store complex objects just fine.
Did you enable the storage module in the settings?
Could you paste here the code snippet for me to inspect?
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 8:12 pm
by undeniable_denial
My advice would be: Start with a minimal working example (Just recreate the one from the tutorial) and flesh it out from there. Make it more complex step by step. That way you will quickly identify the problem.
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 8:15 pm
by Philocalist
undeniable_denial wrote: Mon Dec 23, 2019 8:12 pm
My advice would be: Start with a minimal working example (Just recreate the one from the tutorial) and flesh it out from there. Make it more complex step by step. That way you will quickly identify the problem.
I'm trying just that, but still could not figure out why it didn't save what it needed to.
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 8:16 pm
by kerkersklave
Philocalist wrote: Mon Dec 23, 2019 7:21 pm
Could you paste here the code snippet for me to inspect?
I just did the following:
Code: Select all
teaseStorage.setItem("asd", [{asd: "foo"},2,3]);
teaseStorage.getItem("asd")[0].asd;
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 9:55 pm
by Philocalist
kerkersklave wrote: Mon Dec 23, 2019 8:16 pm
Philocalist wrote: Mon Dec 23, 2019 7:21 pm
Could you paste here the code snippet for me to inspect?
I just did the following:
Code: Select all
teaseStorage.setItem("asd", [{asd: "foo"},2,3]);
teaseStorage.getItem("asd")[0].asd;
So the teaseStorage can store Arrays, and objects in arrays, but not just objects.
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 10:37 pm
by Philocalist
Code: Select all
var DataHolder=
[
slot1InUse=false,
slot2InUse=false,
slot3InUse=false,
Slot1Player=undefined,
Slot1Coordinates=undefined,
Slot2Player=undefined,
Slot2Coordinates=undefined,
Slot3Player=undefined,
Slot3Coordinates=undefined,
]
function saveGame()
{
switch(player.currentSaveSlot)
{
case "Slot1":
DataHolder[3]=player;
teaseStorage.setItem("DataHolder",DataHolder);
break;
}
}
function loadGame(slot)
{
DataHolder=teaseStorage.getItem("DataHolder")[0];
switch(slot)
{
case "Slot1":
player=DataHolder[3];
break;
}
}
This is an actual snippet, and it does not work either.
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 11:31 pm
by undeniable_denial
Arrays are objects in JavaScript.
There are several things wrong with your code, such as the entire declaration of
DataHolder
What you probably meant was something like this:
Code: Select all
var DataHolder={
slot1InUse : false,
etc...,
Slot3Coordinates : undefined
};
It seems to me that you are confused about arrays and objects.
Arrays are bit like lists:
array1 = [value1, value2, value3];
Objects are bit like 2-column-tables:
object1: {key1:value1, key2:value2, key3:value3};
It seems you were trying to create some kind of "map" that exists in other programming languages, but not in JS. Objects are usually used for this.
DataHolder[3] should be
DataHolder.Slot1Player or
DataHolder["Slot1Player"]
None of this is important right now, however, until you have determined if TeaseStorage is actually working for you. As I have mentioned earlier you should try to recreate the tutorial and
check if it works.
If it does: Your code is wrong.
If it doesn't: Something else is wrong and the code doesn't matter.
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 11:48 pm
by Philocalist
undeniable_denial wrote: Mon Dec 23, 2019 11:31 pm
Arrays are objects in JavaScript.
There are several things wrong with your code, such as the entire declaration of
DataHolder
What you probably meant was something like this:
Code: Select all
var DataHolder={
slot1InUse : false,
etc...,
Slot3Coordinates : undefined
};
Furthermore I have absolutely no idea what you mean by
DataHolder[3]
None of this is important right now, however, until you have determined if TeaseStorage is actually working for you. As I have mentioned earlier you should try to recreate the tutorial and
check if it works.
If it does: Your code is wrong.
If it doesn't: Something else is wrong and the code doesn't matter.
Thanks for the help, I'll check.
by DataHolder[3] I meant the fourth instance, thinking of it as an array
the basic tutorial example works for me, I just can not put the player var inside, so I made this DataHolder as an ultimate storage unit to simplify things
Re: TeaseStorage with complex variables
Posted: Mon Dec 23, 2019 11:51 pm
by undeniable_denial
You caught me while I was editing. I eventually realized what you were trying to do. Please see my edited post.
Re: TeaseStorage with complex variables
Posted: Tue Dec 24, 2019 12:00 am
by Philocalist
undeniable_denial wrote: Mon Dec 23, 2019 11:51 pm
You caught me while I was editing. I eventually realized what you were trying to do. Please see my edited post.
As you might have guessed JS is not my go-to language. The advice is wellcomed and noted, I have been using objects in the project, but I thought that the TeaseStorage only supported Arrays.
Still, I can not store this DataHolder variable in teaseStorage.
Any idea?
Code: Select all
var DataHolder=
{
slot1InUse:false,
slot2InUse:false,
slot3InUse:false,
Slot1Player:undefined,
Slot1Coordinates:undefined,
Slot2Player:undefined,
Slot2Coordinates:undefined,
Slot3Player:undefined,
Slot3Coordinates:undefined,
}
function saveGame()
{
switch(player.currentSaveSlot)
{
case "Slot1":
DataHolder.Slot1Player=player;
teaseStorage.setItem("DataHolder",DataHolder);
break;
}
}
function loadGame(slot)
{
DataHolder=teaseStorage.getItem("DataHolder");
switch(slot)
{
case "Slot1":
player=DataHolder.Slot1Player;
break;
}
}
(the functions were of course invited in the eos's JSON part, and the first page loads in the DataHolder in the teaseStorage. I first thought the problem was that it always got set to undefined, but no)
Re: TeaseStorage with complex variables
Posted: Tue Dec 24, 2019 12:20 am
by undeniable_denial
I think you're trying to do too much at once. I still would recommend modifying the tutorial step-by-step.
Since you like to go all in, may I introduce you to this useful command:
How to see the console depends on your browser. Usually it can be accessed by pressing the F12-key. There will be numerous tabs. One of them is called "console".
This command is super helpful in debugging. For example insert it inside your
switch so that you can tell if it even gets executed.
Here are 2 examples:
Code: Select all
function saveGame()
{
console.log("Save slot: "+player.currentSaveSlot);
switch(player.currentSaveSlot)
{
case "Slot1":
console.log("Hooray");
DataHolder.Slot1Player=player;
teaseStorage.setItem("DataHolder",DataHolder);
break;
}
}
Re: TeaseStorage with complex variables
Posted: Tue Dec 24, 2019 12:45 am
by Philocalist
undeniable_denial wrote: Tue Dec 24, 2019 12:20 am
I think you're trying to do too much at once. I still would recommend modifying the tutorial step-by-step.
Since you like to go all in, may I introduce you to this useful command:
How to see the console depends on your browser. Usually it can be accessed by pressing the F12-key. There will be numerous tabs. One of them is called "console".
This command is super helpful in debugging. For example insert it inside your
switch so that you can tell if it even gets executed.
Here are 2 examples:
Code: Select all
function saveGame()
{
console.log("Save slot: "+player.currentSaveSlot);
switch(player.currentSaveSlot)
{
case "Slot1":
console.log("Hooray");
DataHolder.Slot1Player=player;
teaseStorage.setItem("DataHolder",DataHolder);
break;
}
}
helpful indeed, does this mean that teaseStorage has an input limit after all?:
https://postimg.cc/crXG0rW9