Part-2

We can read and write from/to file. And now we will read and write our lovely default cube's properties, let just create a trait CubeController to simply rotate and move cube to demonstrate saving and loading.

// In CubeController.hx

package arm;

import iron.math.Vec4;
import iron.system.Input;

class CubeController extends iron.Trait {
    var kb = Input.getKeyboard();
    public function new() {
        super();

        notifyOnUpdate(function() {
            //Translates (x, y, z) object with according to the key press.
            if(kb.down("up")) object.transform.translate(-0.2, 0.0, 0.0);
            else if (kb.down("down")) object.transform.translate(0.2, 0.0, 0.0);
            else if (kb.down("left")) object.transform.translate(0.0, -0.2, 0.0);
            else if (kb.down("right")) object.transform.translate(0.0, 0.2, 0.0);
            //Rotate randomly with 0.1 speed.
            else if (kb.down("r")) object.transform.rotate(new Vec4(Math.random(), Math.random(), Math.random()), 0.1);
        });
    }
}

This should give you following result:

We use typedef structure to have it hold some of the cube's properties, and use this structure to save/load to json.

// In SaveLoadMechanism.hx

package arm;

import iron.Scene;
import iron.math.Vec4;
import iron.system.Input;
import iron.data.Data;

//Define Anonymous Structure with Vec4 location and rotation.
typedef Cube = { loc : Vec4, rot : Vec4 }

class SaveLoadMechanism extends iron.Trait {

    var kb = Input.getKeyboard();

    var saveFile = "save_game.json";

    public function new() {
        super();

        notifyOnUpdate(function() {
            if(kb.started("f")){
                save();
            }else if(kb.started("g")){
                load();
            }
        });
    }
    public function save(){
        //Get 'Cube' from active Scene.
        var cube = Scene.active.getChild("Cube");
        //Get cube's location.
        var cubeLoc = cube.transform.loc;
        //Get cube's rotation.
        var cubeRot = new Vec4(cube.transform.rot.x, cube.transform.rot.y, cube.transform.rot.z);

        #if kha_krom
        //Set cube's loc and rot to anonymous structure defined above.
        var saveData: Cube = {loc: cubeLoc, rot: cubeRot};
        // Convert saveData to string.
        var saveDataJSON = haxe.Json.stringify(saveData);

        var path = Krom.getFilesLocation() + "/../../../" + "/Bundled/save_game.json";
        // Get bytes of string
        var bytes = haxe.io.Bytes.ofString(saveDataJSON);
        // Save bytes's data to file
        Krom.fileSaveBytes(path, bytes.getData());
        trace("Saved!");
        #end
    }

    public function load() {
        var cube = Scene.active.getChild("Cube");
        // Loads blob from `Bundled` folder
        Data.getBlob(saveFile, function(b:kha.Blob) {

            var string = b.toString();
            var json = haxe.Json.parse(string);

            //Set cube's location and rotation.
            cube.transform.loc = json.loc;
            cube.transform.setRotation(json.rot.x, json.rot.y, json.rot.z);
            //builds matrix (kind of like applying changes).
            cube.transform.buildMatrix();
        });
    }
}

For now, we will have to reopen the game to let game parse newly overwritten game_save.json.

You should get this as result:

If it work for you, then congrats! 🎉