0

I'm making a javascript game but (not browser-based) but the engine I'm using has a bug that it deletes stored arrays when you exit the game. So to get passed that I want to store my arrays as variables and then when you load up the game convert them back to the arrays.

At the moment my game has only 1 array. Someone suggested this code but it doesn't work properly in that when I run a script to print each element of the array on a different line it prints the first element on the first line but then on the second line it prints the rest like this: element1,element2,element3 Here is the code the person suggested:

This code runs every time the arrays are updated. Here, the array is "questsActive", and the var it is stored in is "questsActiveVar".

  questsActiveVar = questsActive.toString();
  questsActiveVar = questsActiveVar.replace(",", "\",\"");
  questsActiveVar = "questsActive=new Array(\"".concat(questsActiveVar,"\")");

This runs whenever the game loads; it recreates the array that was deleted

  eval(questsActiveVar);
1
  • As @Genius observe in is response it could be a good thing to use JSON in your context, if you have a fair amount of data to save. json.org Commented Jan 4, 2012 at 12:22

3 Answers 3

1

To create an array from a string in javascript you need to use the function .split(). Here is an example:

var someStr = 'item1, item2, item3';
var array   =  someStr.split(',');   // place any character in here to split on
alert(array[0]);                     // => item1

From what I understand in your context I would simply save the questsActiveVar variable, then .split() it when the game loads again.

Sign up to request clarification or add additional context in comments.

3 Comments

You can use join() to do the reverse; turn the array into a string: array.join(',').
I can't use split because it converts the array contents into a series of strings and my array contains a series of variables.
@gabriel101x I'm not sure to follow. How can an array have variable and not their value? Could you add a simple example of that in your question?
1

Not sure I understand the issue, but why you want to store a part of javascript if you need to store only data? I'd suggest that you store in your storage a JSON-string of the array, and then parse it as an array when load.

The code should look like this:

var questsActiveVar = JSON.stringify(questsActive);
// questsActiveVar can be stored anywhere as a string

And to load it:

var questsActive = JSON.parse(questsActiveVar);

In this case you can store even more complicated variables with commas, sub-arrays, etc.

Comments

0

You are only replacing the first item with the replace call.

Try this:

questsActiveVar = questsActiveVar.replace(/,/g, "\",\"");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.