Set variable to a txt files specific line

Heres my code for now

import lime.utils.AssetLibrary;
content = Assets.getText("assets/data/games.txt");

basically, how would i go about assigning something like

var title:String = content.line1

and if line one of games.txt = “Super Mario”, then title would equal “Super Mario”
please help if you can and sorry if this isnt really clear

Hi,
I would recommend using JSON, getting data from plain text is gonna be painful :slight_smile:

games.txt would look like this :

{
"title":"Super Mario"
}

and the code would look like this :

var json:Dynamic = Json.parse(Assets.getText("assets/data/games.txt"));
var title:String = json.title;

Ok, i will try that, Thanks!

can you do something like

{
"example": {
    "title": "Super Mario"
}
}

and what would be the code for that. i havent used json before so im probably wrong

Sure, you can really go crazy with the structure : I use it to save and load several MegaBytes of data with no problem.

Here the code to access “title” would simply be
var title:String = json.example.title;

You can also create json from haxe easily

var json:Dynamic = {};
json.example = {};
json.example.title = "Super Mario";
var str:String = Json.stringify(json);
trace(str);

if you rename games.txt to games.json and open it in a browser you can check if your data is valid and have it displayed nicely
You can also use a json editor to make typing the data easier, for example JSON Editor Online: JSON editor, JSON formatter, query JSON (first link I got when searching “json editor”, no idea if it’s good or not :slight_smile: )

I feel like I should have added something about arrays, if I understand correctly you want to be able to list games from your file

example json

{
   "games":[
      {"id":0, "title":"Super Mario", "isCool":true},
      {"id":1, "title":"Zelda", "isCool":true}
   ]
}

let’s spice it up a bit and create a GameInfo class

class GameInfo
{
   public var id:Int;
   public var title:String;
   public var isCool:Bool;

   public function new()
   {
   }

   public function fromJson(json:Dynamic):Void
   {
      id = json.id;
      title = json.title;
      isCool = json.isCool;
   }

   public function toJson():Dynamic
   {
      var json:Dynamic = {};
      json.id = id;
      json.title = title;
      json.isCool = isCool;
      return json;
   }
}

Now the code to load

var json:Dynamic = Json.parse(Assets.getText("assets/data/games.txt"));
var data:Array<Dynamic>;
var info:GameInfo;
data = json.games;
for (node in data)
{
   info = new GameInfo();
   info.fromJson(node);
}

This is forum-haxe so hopefully I didn’t do any mistake :stuck_out_tongue:
This is pretty much how I use Json, there might be a better way but it works for me and I find it very easy and practical

this is perfect! thank you so much!

{"person":"Matse", "isCool":true}

:slight_smile:
btw, i previously did not know how to use a class, so you have subconciously taught me!

1 Like

Nice ! Glad I could help :wink:

1 Like

hi uh one more question, sorry to bug you
can you do something like

var id:Int = 0;
function onRightArrowClick()
{
id = id + 1;
}

and then get the corresponding games id?
so if id = 1 then GameInfo.title = Zelda

thanks for all you help by the way, i appreciate it

I suggest that you take some time to read the OpenFL developer’s guide Introduction · GitBook
Especially the first 2 chapters :slight_smile:

You need a way to store those GameInfo objects so you can retrieve them when you need. This can be done with an Array, in your main class to keep things clear and simple

class Main extends Sprite
{
   private var gameIndex:Int = -1;
   private var gameInfoList:Array<GameInfo> = [];
   private var currentGameInfo:GameInfo;

   private function nextGame():Void
   {
      gameIndex++;
      if (gameIndex >= gameInfoList.length) gameIndex = 0; // when gameIndex is off limits, start again at 0
      currentGameInfo = gameInfoList[gameIndex]; // info about the current game
   }

   private function loadGameData():Void
   {
      var json:Dynamic = Json.parse(Assets.getText("assets/data/games.txt"));
      var data:Array<Dynamic>;
      var info:GameInfo;
      data = json.games;
      for (node in data)
      {
         info = new GameInfo();
         info.fromJson(node);
         gameInfoList.push(info); // add the GameInfo object to the list
      }
   }
}

Now you need a button to click and call nextGame, let’s keep it simple and use the whole stage as the button

stage.addEventListener(MouseEvent.CLICK, onStageClicked); // put this in a function somewhere

private function onStageClicked(evt:MouseEvent):Void
{
   nextGame();
}

Again untested forum-haxe but should work