Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Export variables to files or Import files

    2D Blocks
    1
    1
    152
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

       

      For every variable in a project, you can write code to export it to a text file on the user’s computer, or import a file from the user’s computer into a variable. This is useful when you need to allow the user to save some data that can be used later. For example, when you program a game, you may want to allow the players to save their current progress into a file and reload it later.

       
       

      Export a Variable

       

      You can use the “export variable [VARIABLE NAME]” block to export the value of a variable to a text file. For example, when you run this program, a new file named “variable1” will be downloaded into the “Downloads” folder on the user’s computer, and its content will be “22”. Of course, you can store much more data in a variable, and it will all be saved into the file.

      cbcd340c-33a0-4709-990a-03ca3ddb49ea-image.png

       
       

      Import a File into a Variable

       

      You can also import a file into a variable like this:

      eb2bb03b-dde3-4c98-ac5f-1c709d2d5b85-image.png

      When the user runs this program, an “Open File” dialog window will pop up, and the user can select ANY file that ends with “.txt” or “.csv”. Note that the file name doesn’t have to be the same as the variable name. Once the user selects a file, that file’s content will be stored in the variable you specified. If the user has cancelled the dialog window without selecting any file, then the variable’s content will not be changed.

      1 Reply Last reply Reply Quote 1
      • First post
        Last post