Home Ask Login Register

Developers Planet

Your answer is one click away!

killereks February 2016

localStorage() storing variables

My code: (there is 1200 lines of code and all I need is variables), I need help filling the save(); and load(); so it loads and saves all variables using localStorage (so you can access it any time on same pc). Thanks for any help.

var xp = 0;
var xpN = 10;
var level = 1;
var population = 4;
var populationLimit = 5;
var populationProd = 0;
var questpoints = 0.5;
var questpointsboost = 1;
var autoquest = "false";
//Resources
var gold = 11;
var food = 25;
var wood = 0;
var stone = 0;
var iron = 0;
var goldmax = 15;
var foodmax = 5;
var woodmax = 5;
var stonemax = 5;
var ironmax = 5;

var prize = 0;

var trade1 = 0;
var trade2 = 0;
var item1 = 0;
var item2 = 0;
var userAns = "";
// workers
var soldier = 12;
var randomnumber = 0;


var farmer = 0.1;
var lumberjack = 0;
var stoneminer = 0;
var ironminer = 0;

var farmercount = 0;
var maxfarmer = 4;

var lumberjackcount = 0;
var lumberjackmax = 0;

var stoneminercount = 0;
var stoneminermax = 0;

var ironminercount = 0;
var ironminermax = 0;

function save(){

}

function load(){

}

Answers


Zohaib Ijaz February 2016

You can save values like this

>localStorage.setItem('variable_name', 'value')

and then you can get that value

>localStorage.getItem('variable_name')

You can store keys in variables and then get that value from localStorage against that key

var playerName = 'player';
localStorage.setItem(playerName, 'John Doe');

and next time you can get that value

var player = localStorage.getItem(playerName);

Post Status

Asked in February 2016
Viewed 1,924 times
Voted 5
Answered 1 times

Search




Leave an answer


Quote of the day: live life