How do I save the data in my array to my android phone?

0 favourites
  • 9 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • Hello, I made a game for my android smartphone. now I need to add a feature that will save the player's score permanently on the mobile phone.

    I use 2D array to keep track of the player's various scores on each level of the game. As far as my research goes, I think my option is using Webstorage or NodeWebkit. If I understand it correctly ( please correct me if I'm wrong...), I think that NodeWebkit is better as the saved data will not be deleted when the web browser's cache or cookies or something is cleared, and is not easily edited by the user. But I still have not found any sample capx that shows how data is saved locally on the phone's memory using NodeWebkit. I tried reading the manual on NodeWebkit, but I still can not figure it out yet. A working sample will be a great help.

  • I think you're a bit confused about Node Webkit - that is a wrapper to create stand alone versions of your game for Windows / OSX / Linux - as an export option it's got nothing to do with android or saving data!

    That aside, at the bottom of the Array page in the manual is the AsJSON expression - use that to return the contents of your array as JSON format, which you can then save using webstorage. If you want to load the array from webstorage, load the saved string into the array using the Load expression. Have a search of the tutorials for these features and you'll be able to find some examples of how to do this.

  • Colludium - Thanks for the feedback, and you are right! I am still very much confused about NodeWebkit being a wrapper, or whatever part it actually plays in construct. But I did use it as part of my code together with AsJSON and I was able to save my array to a text file and was able to load it during start up of the game. I am now able to continue playing my game from where I left it. But this does not solve my original problem of saving the game on android phone.

    The code works when I "preview" the game on my PC but when I exported the game on my android, it won't save. Maybe this is what you meant by the "Nodewebkit is a wrapper for Windows/OSX/Linux?!?

    While not giving up hope, I am thinking that may be I just did not specify the right "path" for an android phone. I hope someone can share some of their ideas on how to save games on android phone.

    PS. I used Intel XDK to export my phone to android. Except for the saving part, the game works fine on my android phone.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • baodingball,

    I know - it's all a bit confusing at first, but you'll quickly pick things up I'm sure. I think you should be using Webstorage link to Webstorage in Manual rather than Node Webkit (maybe the word Web is confusing things here) if you're using a browser or wrapper on mobile. To keep things easy to read and understand in your event sheet, I suggest you first load the array into a string variable and then save that variable to Webstorage. Then, on game resume you do the reverse - load the Webstorage string into a variable and then load that into the array. You've already got the basics, looking at your event sheet, but the node webkit isn't going to work for you here.

    Something like this:

    On game end:

    1. Set MyVariableString to MyGameArray.AsJSON

    2. WebStorage: set local value ( Key: "SaveData" , Value: MyVariableString )

    On game resume:

    1. Set MyVariableString to WebStorage.LocalValue("SaveData")

    2. MyGameArray : Load (MyVariableString)

    In my limited experience I've had no problem using this technique to save data on android and ios using cocoonjs and phonegap (and, I think, Intel XDK as well - the old one, mind, not Crosswalk which I haven't tested extensively yet).

    I hope this helps - let me know if it's not clear!

  • I think I managed to get Webstorage working... at least for the PC browser. I still have not tested it on the android phone. I uploaded the game on Kongregate, below is the link:

    http://www.kongregate.com/games/baodingball/japanese-flashcard

    I tried uploading it on the Arcade, but the size is too large.

  • Colludium, hi, it's me again. Is there some special setting that I should know about, like during export? During preview, the game seems to work ok. The scores are saved and when I restart the game, the score data are retrieved ok and I can continue the game from where I left it. But when I exported it on Android phone using Intel XDK, the scores will not save. The moment I close the apps the data are gone. When I restart the apps, I am back to level 1 again and the score board is totally blank. I am thinking maybe the browser automatically deleting the phone's cache or something. I hope you can give me some clue. Thanks. it's a bit late, so I will post the code later. Thanks.

  • To me it sounds like you're using Session Storage rather than Local Storage to save the data. Session Storage saves only while the game is running/loaded in the browser and is then deleted.

    Without a capx to look at its hard to say why it might not be working on the XDK but it is working everywhere else... I don't regularly follow the XDK thread but it might be worth looking in there to see if there are any known bugs associated with webstorage. If not, you might have found one - but only if your events are correct! If you don't want to share your work, see if you can use similar events in a simple demo and have the community check it for you.

  • I am trying to save the data from the array aswell. However, I couldn't understand how webstorage works after reading the manual.

    Could you take a look in my test capx and tell me what I am doing wrong?

    In this capx, one would like to save the status of each checkboxes.

  • Thanks Colludium - I think there is something wrong with my code. I made a simple app that will increase the variable by 1 with every click of the button. I created a new Intel XDK project folder. Export my construct2 apps to that folder... build it and it is now working. Every time I opened up the apps, it's start with the latest variable. Even when I turned off the phone, the data in the array is saved. Attached is my sample capx:

    I guess I need to review the code to my game and find out where I screwed up! Again thanks.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)