OpenFileForWriting

Definition

OpenFileForWriting(string filePath)

Description

This function opens a binary file for writing. After writing the information to this file, you should use the CloseFile function to close the file.

Parameters

filePath
Specifies the file path. This path is located in the Assets/Data/ folder.

Example

bVar = false
fVar = 0.0
iVar = 0
sVar = "init"

function Init()
   --Create a folder in Assets/Data/ path
   CreateFolder("Lev1")

   --Create and open file to write data
   OpenFileForWriting("Lev1/level1.bin")
   WriteBoolVariableToFile(true)
   WriteFloatVariableToFile(2.0)
   WriteIntVariableToFile(3)
   WriteStringVariableToFile("level1")
   CloseFile("Lev1/level1.bin")

   --Open File to load data
   OpenFileForReading("Lev1/level1.bin")
   bVar = ReadBoolVariableFromFile()
   fVar = ReadFloatVariableFromFile()
   iVar = ReadIntVariableFromFile()
   sVar = ReadStringVariableFromFile()
   CloseFile("Lev1/level1.bin")
end
First, using the CreateFolder function, we create a folder called “Lev1” in the Assets/Data/ path. Then, using the OpenFileForWriting function, we open the level1.bin file located in the Assets/Data/Lev1/ path for writing. After writing information to the file, we close it using the CloseFile function.
Then, using the OpenFileForReading function, we open the level1.bin file located in the path Assets/Data/Lev1/ for reading and read its information in the same order as we wrote. Finally, we close the file using the CloseFile function.
adminOpenFileForWriting