Files
Leenkx_Examples/file_read/Sources/lnx/ReadFile.hx
2026-05-06 17:52:45 -07:00

35 lines
796 B
Haxe

package lnx;
import leenkx.trait.internal.CanvasScript;
import iron.data.Data;
import iron.Scene;
class ReadFile extends iron.Trait {
public function new() {
super();
notifyOnInit(function() {
// Relative or absolute path to file
// In this case we load the file placed in the "Bundled/" directory
var file = "my_file.json";
// Load the file asynchronously
Data.getBlob(file, function(b:kha.Blob) {
// File is now loaded
// Get string from loaded bytes
var string = b.toString();
// Get json from string
var json = haxe.Json.parse(string);
// Get canvas trait
var canvas = Scene.active.getTrait(CanvasScript);
// Display "my_text" entry found in the loaded json file
canvas.getElement("Text").text = json.my_text;
});
});
}
}