• Content Count

  • Joined

  • Last visited

About xRa7eN

  • Rank

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. that worked.. sometimes you just need a second pair of eyes. Thanks player = JSON.parse(localStorage.getItem("copperSave"));
  2. Hi b10b! Trust me, the nightmare is worse LOL The log actually shows player correctly - just the original initgame () values. weird huh. With cookies, there is no fail. works perfect. So should I just use "player = gameLoad? at the moment, back to dealing with the math of the incremental. I just put this part on hold, and stuck with cookies, and I am currently developing it, but later will pursue what is going on with the localstorage - probably something weird. player = JSON.parse(localStorage.getItem("copperSave"));
  3. ok went the cookie approach. I heard this is not usually the best way, but it works. I set the date way in the future, so should not be an issue - hopefully. /* SETUP A SAVE STATE */ function save() { // Using cookie method. problems with localstorage. var d = new Date; // other possible future date: d.setTime(d.getTime() + 10 * 365 * 24 * 60 * 60); // 2038 :-) document.cookie = "copperSave" + "=" + JSON.stringify(player) + ";path=/;expires" + d.toGMTString(); }// FUNCTION: SAVE GAME /* LOAD PREVIOUS game */ function loadGame() { // Split cookie string and get all individual name=value pairs in an array var cookieArr = document.cookie.split(";"); // Loop through the array elements for(var i = 0; i < cookieArr.length; i++) { var cookiePair = cookieArr[i].split("="); /* Removing whitespace at the beginning of the cookie name and compare it with the given string */ if("copperSave" == cookiePair[0].trim()) { // Decode the cookie value and return player = JSON.parse(cookiePair[1]); } } console.log(player.gold); } initGame(); var answer = confirm('Continue from previous saves?\nWarning, "Cancel" creates a new game wiping previous saves'); if ( answer ) { loadGame(); }
  4. ok, another oddity I noticed, it actually loads correctly EVERY-OTHER-TIME! ?? so I added localStorage.removeItem("save") in the load function, but still same pattern
  5. Hi, I've been working on my game project clicker (helps me learn JS and other languages). So it is going smooth so far. Thought it might be a good idea implement a save/load feature now. The following has no errors, but loading it has no effect. INITIAL VARS var player = { name:"", level:0, gold: 190,// CHANGE BACK TO 0 AFTER TESTING artifact: 0, weaponId:0, weaponLevel:0, spellId:0, spellLevel:0, goldPerClick:5, goldPerSec:0, currentTrainer:0, }; SAVE STATE /* SETUP A SAVE STATE */ function save() { // TRY TO SAVE THE GAME try { localStorage.setItem('copperSave',JSON.stringify(player)); }catch(err) { console.log('Cannot access localStorage - browser may be old or storage may be corrupt') } console.log('Game saved successfully'); }// FUNCTION: SAVE GAME LOAD STATE /* LOAD PREVIOUS game */ function loadGame() { var gameLoad = JSON.parse(localStorage.getItem("copperSave")); player= gameLoad.player; console.log(player); // this shows the starting vars NOT the saved var from getItem! } SOME MISC VARS TO HELP DEBUG initGame(); var answer = confirm('Continue from previous saves?\nWarning, "Cancel" creates a new game wiping previous saves'); if ( answer ) { loadGame(); } console.log('here after load query'); // GAME LOOP FOLLOWS BELOW.... It looks right, can anyone suggest why it might not be loading correctly. It just gives the starting variable, not the saved variables.
  6. OK Here is what i was able to come up with, research arrays. var weaponLevelNfo = Array.from({length:14}, () => Array.from({length:9}, () => [0, 0])); Very simple, and allows instant assigning off array at any level. The clean up part, will be to make it an object so I can read it better example weaopnLevelNfo[x][y].cost vs weaponLevelNfo[x][y][z]. Currently not an issue, as there are only two indexes to be concerned about.
  7. Let me soak some of this up.. I have been non-stop searching, reading and you tube and it slowly coming into focus I have a QB64 code that is working. but it handles arrays differently (and no objects, instead I use TYPE but from the looks of it, that could be used as objects in JS) but the other thing I see, is yes, I did try the above and it worked, the issue came in when I was trying to dynamically increase the array/object and have it auto put in new values for cost and dps instead of doing all by hand. As I kept experimenting, I found that it might be easier to break it down to the names in their own array. (its just for looks, and plays no role) the items/ spells or weapons, in their own array with cost, dps gear[ {cost, dps}] then i could use gear[gearlevel].cost, gear[gearlevel].dps then the other tracking level, gold, etc.. can be in a player object. so that made sense to me, and simple, I could do that, but 20 items each having 10 upgrades and each with cost, dps or dpc can get redundant. thats where I ran into the bump. many languages I work with can do a for x loop => gear[x].cost = xyz and gear[x].dps = xyz, loop, but when I did it, it got some goofy error of gear[x] not defined after the first iteration. so if I read your code correctly, inserting the gear = {} inside the loop should fix that, so that the cost,dps will then be inserted without error? ps I usually have lots of probs with code since I know quite a few languages, sometimes, the code syntax is correct, but not for the language I am working with LOL!!
  8. OK, I think i have "BASIC" understanding.. Let me know if this is correct: So a "bracket" is used to make the basic traditional array game [] which will give game[0], game[1] etc... correct? Braces will give an object? game{}, so game {name: coolplayer, hp:10} which yeilds game.name // coolplayer? then to combine, make your arrya first? then put in anything you want, but if you want another array, you use brackets again - like so? game [ player[{name:'george', hp:25}]] then I can pull it as such: game[3].player[2].name // george So do I have the gist of that? Seems to work when I test it. Totally not like arrays I am used to LOL, but unique and more flexable So... when i get to this: weapon = [ { name, rank:[ {damagePerClk, cost} ] } ]; works fine if i manually insert data, but if I loop as shown below: function initGame() { var wLength = weaponList.length; var sLength = spell.length; for(let i = 0; i < 14; i++) { weapon[i].name = weaponList[i]; //spell[i].name = spellList[i]; console.log(weapon[i].name); } } crashes. Says "i" is not defined, but that is not the issue. and stops at 1. (example, log shows 0,1 crash). "i" is fine, and if i loop through a list - (spellist for example) it list all my spells as they are. I am guessing it is how I have weapon defined (declared). Right?
  9. I just wanted to hop on and say thank you for your suggestions. object is probably better, and I like referencing things by name for simplicity. I have some huge IRL jobs going on, but will take a look at this and maybe post a modified code of mine using one of your examples. thank you very much again.!
  10. Hi hi guys, I do hope I am posting in the correct area. I written a couple clicker games for myself mostly too lean the language. So now I've been tricked out on a more complex game. And of course with complexity comes new methods of writing when using different aspects of the code. Which is why I like writing games. Also, I want to do this in JavaScript, not jQuery. Just a personal preference. The section of game is this: 20 mobs. Each of those mobs attack the player in nine waves. Inside of those waves are the criteria needed to beat that wave. I want to be able to access the data as follows: To initialize: Mob(1) = mobname Mob(1).wave(1).hp = 100 .... Mob(19) = mobname Mob(19).wave(1).hp = 100 Mob(mobxCnt).wave(currwavecnt).hp etc. Unfortunately, I am at work so I cannot post the various codes I've tried. Most of them work but only 2 deep. I am used to programming in PHP which this is extremely simple tasks to do. However, I'm having difficulties with JavaScript and it's arrays. How do I get the third set get the various criteria is inside that wave? I'll be home in a few hours, if this is still a little blurry I'll post some of the codes I've tried. Thanks for your assistance