MZ [SOLVED] Inquiry: Can MZ read from an INI file and compare the value in a Conditional Branch?

I'm back again with another INI related question. Since tinkering with Event Tiles being able to write to INI Files (Of which you can find here: https://forums.rpgmakerweb.com/inde...an-mz-event-tile-write-to-an-ini-file.178363/), I've been curious on the reverse: reading from INI Files and subsequently using that data in a Conditional Branch?

Answer for Future Curious Minds:

INI:
[Red]

JavaScript:
const fs = require('fs');

const filePath = "C:/RPG Maker Folder/Test.ini";

$gameVariables.setValue(1, fs.readFileSync(filePath));

JavaScript:
$gameVariables.value(1) == "[Green]"

Please take note:
  • Within the $gameVariables.setValue function, the 1 is the variable number. RPG Maker uses numbers to store its variables rather than strings. The "1" above can be any number since it is just a variable.
  • The second Javascript block is the Conditional Branch Script you will be using for the If/Else.
  • The INI in the example above is "[Red]", where as the Conditional Branch is looking for [Green]. The result will be false since the 2 don't match up thus outputting the Else branch.


Niche Case Scenario:
If you are using something that is automatically writing to an INI file there is a chance it saves with a line break at the end of the INI file. In this situation the value comparison will be off as RPG Maker will be looking for "[Green]", but what you are giving it is "[Green]" with a line break. So they will not be the same. After some testing I have found a solution for this niche case scenario.

INI:
1: [Green]
2:

JavaScript:
const fs = require('fs');

const filePath = "C:/RPG Maker Folder/Test.ini";

const dataString = fs.readFileSync(filePath);

const newString = dataString.slice(0,-2);

$gameVariables.setValue(1, newString);

JavaScript:
$gameVariables.value(1) == "[Green]"

Additional Notes:
  • I have included "1:" and "2:" in the INI code block just to illustrate there is a blank new line, where line 1 is "[Green]" and line 2 is a blank new line. "1:" and "2:" will not be present in the INI file.
  • Slicing the last 2 characters from the dataString above will leave you with the "[Green]" you will be looking for when comparing the gameVariables.
As far as I know, you would need the precise data offset for that, e.g. in bytes. That would generally require reading the file to figure out how many bytes there are before, and on, the desired line.

I suggest you just read the whole file into memory, then parse the desired information from that.

Technical reference:
(Note that fs is a Node API applicable only to standalone games. If you want to support web deployment, it'd be better to use XHR or fetch instead.)
It sounds like what you really want is an INI file parser. It probably wouldn't be hard to roll your own (INI is a pretty simple file format), but I bet you can find one in the npm repositories (you can search for packages here).
Yeah luckily I'm only needing it for a standalone game, so fs should be sufficient.

As far as that npm website, seems like there are a few things in there that might suit me, though the installation process confuses me. Wants me to copy a line of text like "npm i read-ini-file". lol Not sure what to do with it. Ah the hilarity of an artist trying to pry around in the realm of programming.

If I cannot figure it out I at least got a fall back plan on just having a separate INI with just a single [section] in it without any Keys, for a single line read. I can only imagine how obtuse my questioning has been to the community at large. lol I want to thank you all for your help.
At last we reach the finale to this 3 part questionnaire saga, building off my inquiry on RPG Maker MZ reading INI files.

I see I can add a Script as the conditional statement in RPG Maker, this excites me as it makes me believe there is at least a chance I can compare a simple 1 line string from an INI file to return a true/false statement and thus have something happen based on if the string is what the conditional is looking for. I will try to be as clear as I can on what I'm seeking RPG Maker MZ to do.

  • First, let's imagine I have an INI file that simply has a section named "[Red]" in it and nothing more.
  • Second, RPG Maker would read the file and capture "[Red]" in a variable
  • Third, I would like to have a Conditional Branch in the form of a Script look for a string named "[Green]"
  • Fourth, have this Script compare what's in the INI file ("[Red]") to what what the right answer the Conditional is looking for ("[Green]") and have it spit back out false, since the 2 colors don't match up.
So I guess my final question on this subject is more of a 2 part question:
  • Can this even be done in the Conditional Branch Script option?
  • If it can, what would that even look like? "if(variable == '[Green]');"?
Any help shedding some light on this would be immensely appreciated. Oh and I should probably clarify that this would be a standalone game that I'm tinkering with, one that I would use exclusively to myself for my streaming channel in which chat would directly control aspects of it...or at least attempt to depending on the results of this final inquiry. lol
Yeah luckily I'm only needing it for a standalone game, so fs should be sufficient.

As far as that npm website, seems like there are a few things in there that might suit me, though the installation process confuses me. Wants me to copy a line of text like "npm i read-ini-file". lol Not sure what to do with it. Ah the hilarity of an artist trying to pry around in the realm of programming.
Indeed, that website is targeted towards people who have already installed the program called "npm" (which can likely also be downloaded either at that website) and are comfortable using the command prompt. But it is quite likely you can still download the packages manually. It might be slightly different depending on the package, but I took a look at a random package page and noticed a link to GitHub on the right-hand side, so if you click there, there would be a JS file you can download. That said, it's far from easy to do manually, especially since many of the packages depend on other packages, so you'd also have to manually follow those dependencies...

Installing npm and pasting the install line into a command prompt is certainly an option. I'm not sure exactly how you'd get from there to a point where you can use it in RPGMaker, however.

  • First, let's imagine I have an INI file that simply has a section named "[Red]" in it and nothing more.
  • Second, RPG Maker would read the file and capture "[Red]" in a variable
  • Third, I would like to have a Conditional Branch in the form of a Script look for a string named "[Green]"
  • Fourth, have this Script compare what's in the INI file ("[Red]") to what what the right answer the Conditional is looking for ("[Green]") and have it spit back out false, since the 2 colors don't match up.
I get the impression that you've messed up in attempting to make this as clear as possible. Your second and third steps both seem to be unnecessary.

But that said, it sounds like what you want can be done with simple string matching. If I understand correctly, you're basically trying to answer the question, "does this INI file contain a [Green] section?".

So I think you can do this in two steps. First, a Script command:
JavaScript:
var fs = require('fs');
// Replace 123 and "myFile.ini" as appropriate
$gameVariables.setValue(123, fs.readFileSync("myFile.ini"));

This puts the entire contents of the INI file into variable 123. Then you can add a Conditional Branch script:
JavaScript:
$gameVariables.value(123).match(/^\[Green\]$/m)

That returns true if the value of variable 123 (ie, your INI file) contains a line that contains exactly the text "[Green]". If you want it to match "[green]" too, just change that m at the very end to mi.
lol Yeah you can tell how inexperienced I am at Javascript and programming in general.

So first thank you Solar for your assistance in helping a lay person like myself understand. I will have to try this.

EDIT: YES! I got it working! Boy looking at it now, I realized I really was over complicating it. Okay time to edit my original post for any future inquisitive minds. Thank you again Solar!
This thread is closed due to being reported as solved. If you need it reopened, report it to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/solved-inquiry-can-mz-read-from-an-ini-file-and-compare-the-value-in-a-conditional-branch.178418/