Reading a JSON file immediately after writing it during game runtime in Haxe Code

Good day guys. I need help on the following issue…

I have saved the game control keys to a file (eg controls.json). There is a Game scene which reads this controls.json file and displays the current keyboard controls to the user. Problem is when the user changes these settings during game run time, the control.json is overwrited successfully but the game does not reload the file in the RAM so the new settings are read. The Scene will continue displaying old controls until the game has been closed and reopened for it to read the updated controls.json.

Is there a way in which the game can quickly update the overwitten file so that the new settings are displayed without closing the game first using Haxe code.

Hope my problem is clear. To those who wish to test a similar issue create a canvas that reads input text from the user and writes to a json file. Create another Scene which will be loaded after the user submits the Inputed text and that Scene must display what the user wrote to that file in the previous scene. You will notice that during runtime, before reloading the game, the reading Scene will display nothing. But after closing and opening the game, it will now read the true file state. How can I quickly read the overwitten text without closing the game in Haxe code.

Thank you in advance

Hi @ArmoredDP

Assuming you use the Read/ Write JSON node, have you tried unchecking the option Use Cache?

Below I post a simple example where a number 1 or 0 is stored in JSON, and is then retrieved. It’s self-explanatory.

ReadWriteJSON.blend (779.1 KB)

1 Like

Very likely the “cache” is the problem. Not sure how the cache-check is implemented – it really should not be using stale data, but maybe it is.

1 Like

Thanks for the reply @QuantumCoderQC, Unfortunately I am not using the Read/Write JSON node, I am using Haxe code not JSON nodes. Is there a way to achieve same results using Haxe code.

Thanks @MikeRobinson, I dont know why the cache is using stale data, Is there a way to disable it?

@ArmoredDP

I am sorry, I didn’t realize it was a Haxe specific question :wink:

Anyways, if it helps, here is the Haxe code that the logic node uses to clear cache and load back the file. You may use something like this I think.

Best,
QC

1 Like

I tried following that haxe code but it still reads stale data.

	 if(useCache == false){
			 trace(iron.data.Data.cachedBlobs.get("control.json"));
			 if(iron.data.Data.cachedBlobs.get("control.json") != null){
				 iron.data.Data.cachedBlobs.remove("control.json");
			 }
			 trace(iron.data.Data.cachedBlobs.get("control.json"));
			 iron.data.Data.getBlob('control.json', function(blob:kha.Blob) {
				var User2:{name:String} = haxe.Json.parse(blob.toString());
				canvas.getElement("Text").text= welcome + User2.name; 
			});
		 }

This is my sample code. The first trace outputs the data in my json file eg {id:1; move:“left”}. Then I check if the cache is not null, if so I call the cachedBlobs.remove() as you see. To show that the cache has been cleared successfully, the next trace outputs a null. The then I go further to read my control.json file but it still displays stale data during runtime. If anyone can track and see whats happening, the second trace() shows cache is cleared but still it shows stale data.

@ArmoredDP

Hmm, Could you please share a minimalist example project file(.blend file + Haxe Traits) for this issue here? That way more people can try easier and maybe come up with a solution/ workaround.

Ooops the project file is a bit big. What I will do is to trim it to the main problem so that there wont be any confusion, let me see if I can do it now.

Funny things always happen with code. I managed to trim the problem and cleaned the project and suddenly, the code worked as I expect. Let me investigate what the problem was with the cache and update on this thread for the benefit of others. Thank you @QuantumCoderQC and @MikeRobinson for your contribution on this problem. The cachedBlobs.remove() function was working correctly after all.

2 Likes