Topic: I created a save system with Lua's file functions! Get it here!

I was inspired to create a save system when I read the idea from the creator of this thread:

I tried to use the "dofile()" function to load a new "Lua" file that was in the directory of my game, but that didn't work for published games, so I studied the File functions of Lua to build upon the idea from that guy's post.

I've been searching the internet for snippets of code during my whole day.

I'll write the solution here for future generations that discover Maratis:

1. Create a new Project, and place a 3D object on the screen. Use "Player" as the object's name.

2. Create an empty script file called "GameScript.lua" and attach it to your Scene.

3. Then, paste this script into the empty file:

Player = getObject("Player")

function onSceneUpdate()

    --  When the Z button is pressed, we will create the content of the file,
    --  to simulate what happens when a player saves the game for the first time.

    if isKeyPressed("Z") then
    file ="save_data.ini", "w+")
    file:write("1\n2\n3\nturn right")

    function checkLine(lineNumber, lineContent)

    -- Search for whatever you want by checking for a line number and then checking for the content of the line.
    -- You can copy and paste the next section to search for text in multiple lines.

        if lineNumber == 4 then
            if lineContent == "turn right" then
                rotate(Player, {10, 0, 0}, 1, "local")
            if lineContent == "Maratis is my closest friend" then
                rotate(Player, {-10, 0, 0}, 1, "local")

--  Pressing the X button on your keyboard will start the search by calling the function that's listed above.

    if isKeyPressed("X") then
        f ="save_data.ini", "r")
        count = 0

        for line in f:lines() do
            count = count + 1
            checkLine(count, line)


-- When the C button is pressed, you can modify a single line, like when your player unlocks more content.

    if isKeyPressed("C") then

        local hFile ="save_data.ini", "r") --Reading.
        local lines = {}
        local restOfFile
        local lineCt = 1
        for line in hFile:lines() do
            if(lineCt == 4) then -- This number is the line that you will modify.
                lines[#lines + 1] = "Maratis is my closest friend" --  Change the content of that line here.
                restOfFile = hFile:read("*a")
                lineCt = lineCt + 1
                lines[#lines + 1] = line

        hFile ="save_data.ini", "w") -- Write the file.
            for i, line in ipairs(lines) do
                hFile:write(line, "\n")



4. Now, press "File" to open the menu and click "Save", and then press "File" and click "Publish Project".

5. Find your project's directory, and then enter the "published" directory.

6. Create an empty file called "save_data.ini" in that directory.

7. Now, double-click the "exe" file to try the game.

8. When you hold the "X" button on your keyboard, the code will search line 4 of the text file for the text "turn right". Since the file is empty, it will not find the text, so your character won't rotate. When you press the "Z" button, four lines will be created in the text file, like this:

turn right

That's an example of what happens when a player starts a new game and doesn't have saved data yet.

Now that the words "turn right" have been saved in line 4, you can press the "X" button again, and your character will rotate while it's pressed!

You can use this to create a saving system. For example, when the game is first opened, you can make it search for a piece of text on line 27 that says "100", which can mean that all levels have been unlocked. If that text is different, you can choose to only unlock a few levels for your players.

After you do all of that, you should press the "C" button. That will change the text of line 4 from "turn right" to "Maratis is my closest friend". Now, when you press the X button, your character will rotate left instead of right. Study the code to see if you can understand how the rotation changed.

Note: I don't know if your characters will rotate in the directions that I've mentioned because this might depend on how your 3D models are created, but they should still rotate. If they rotate in different directions than the ones I mentioned, you shouldn't think it's because of a glitch.

Important idea about people who are worried about people cheating by changing the text of the file:

If you're worried about people changing the text to unlock the whole game immediately, you can just encrypt it in your head. For example, don't use messages like "All-Weapons-Are-Unlocked". Create random gibberish and track it in a journal while you create your game, so "tztz47sda" on line 27 might mean that all weapons are unlocked. That means that at least one person will have to fully complete your game without cheating before the functions of your gibberish phrases are known. He can share his perfect save file with all other people later, but that happens will all games. Even with games created by professional companies, one guy can finish a whole game without cheating and share that save file with everyone, so don't be scared by this.

Last edited by wild master (2017-12-10 23:52:33)

Re: I created a save system with Lua's file functions! Get it here!

Neat !
Good work.