Guardar y cargar datos WebStorage

2

Stats

2,274 visits, 3,826 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 10 Oct, 2013. Last updated 19 Feb, 2019

Save and load data

[ Create all kinds of games and learn a complete course in Construct 2 ] [7 ]

First in a new project , add a Text object just change the name to "Data " , add a Textbox object , put it under Text and name "Area" , finally creates two Buttons place them under " Area " to one name it " Guadar " and in the text the same, and the other do the same but instead of " Save " will " Load " . We have created our form but lacking the most important object ... WebStorage , and we'll add it to our events sheet .

When we choose to use to save and load data there is only one variable that we want to use , but also , is the variable which stores this data is that , if we keep the maximum score then we should have the variable where you observe a data persistently ( That alone is erased if we chose ) and the variable where the persistent data will be loaded . That said we will create a normal variable with name " Test " Event!

                EVENT 1 :
                Condition 1: Local >> key exits WebStorage [ "Test" ] ( If there is a variable "Test" in the WebStorage )
                Sub Condition : Every tick ( This will allow us to be able to load the data in the same session and not have to restart the game to load )
                Action 1 : System >> Set value [Test , WebStorage.LocalValue ( "Test" ) ] ( So would pass the variable " test " stored in the variable WebStorage Test match)
                Else [ Else To add a right click on the red box and then click Add ] :
                Action : System >> Set value [Test , 0] ( Otherwise ( Else) if the data is not in WebStorage is the test value will be 0 )

To read it right parenthesis I wrote, this event will help us load the data if there so to speak , in the absence or in other words , if the user has never saved the game then the value will stay at 0. Events !

            EVENT 2 :
            Condition : Save >> On Clicked
            Action : Local September WebStorage >> value [ "Test" , Area.Text ]

This is easier if the user clicks on save then the data entered in Area.Text be stored in local variable WebStorage "Test" , you can change a variable Area.Text eg HighScore or whatever you want , but for now do it in this way to follow the tutorial. Last event!

            EVENT 3:
            Condition : Load >> On Clicked
            Action : Text >> September text [ Test]
    

This time when the user clicks Load global variable is sent ( The Game ) "Test" to the text of the object "Data " and should go something like this :

Now recharge and see it without having to rewrite the same data , just by clicking load will reload the same, even if it sometimes does not happen that's why you need to export your project. To learn how to export to html5 or read a full course of Construct 2 enters the link I posted earlier in this tutorial.

[1] [ http://creando-los.blogspot.com/ ]

[2] [ https://www.scirra.com/images/articles/3_12.png ]

[3 ] [ https://www.scirra.com/images/articles/4_9.png ] [ image description ] [3 ]

[4 ] [ https://www.scirra.com/images/articles/5_12.png ] [ image description ] [4 ]

[5 ] [ https://www.scirra.com/images/articles/6_8.png ] [ image description ] [5 ]

[6 ] [ https://www.scirra.com/images/articles/7_6.png ] [ image description ] [6 ]

[7] [ http://creando-los.blogspot.com/ ]

  • 0 Comments

Want to leave a comment? Login or Register an account!