Author Topic: GUI That displays text in file  (Read 1341 times)

Hey,

I am taking a very large plunge into BLR Scripting. I am trying to make a GUI which displays all the text in a file on the server host's computer. Such as the one on the Rise of Blockland server that displays Updates, Rules etc. Also, if possible, I'd like to at one point make a GUI which can edit the text in the file (so I can edit for example the Updates and Rules in-game).

Thanks!!

EDIT: Sorry if this has been posted already, I searched many times before posting.
« Last Edit: December 12, 2007, 04:46:57 PM by bfen92 »

Here's a function to get the contents of a file:
Code: [Select]
function getFileContents(%path) {
   %file = new FileObject();
   %file.openForRead(%path);
   while( !%file.isEOF() ) {
      %line = %file.readLine();
      %contents = (%contents $= "" ? %line : %contents @ "\n" @ %line);
   }

   %file.close(); %file.delete();
   return %contents;
}

To display it in a GUI:
Code: [Select]
function [MyGuiMLTextCtrl]::loadFile(%this, %path) {
   %this.setText( getFileContents(%path) );
}



To save text to a file:
Code: [Select]
function setFileContents(%path, %data) {
   %file = new FileObject();
   %file.openForWrite(%path);
   while( (%next = strstr(%data, "\n")) >= 0 ) { // Split the data into lines
      %datum = getSubStr(%data, 0, %next);
      %data = getSubStr(%data, %next + 1, strlen(%data));
      %file.writeLine(%datum);
   }

   %file.close(); %file.delete();
}

To save from a GUI:
Code: [Select]
function [MyGuiMLTextCtrl]::saveToFile(%this, %path) {
   setFileContents(%path, %this.getText());
}



All of that would go together and you'd have to make a GUI with a textbox (GuiMlTextCtrl) that has a name (replace the "[MyGuiMLTextCtrl]" in the code with the name of your text box). Then you can make buttons that use saveToFile() and loadFile() methods, getting the filepath from a GuiTextEditCtrl.
« Last Edit: December 12, 2007, 07:32:01 PM by exidyne »

Sorry I'm completely new to scripting and I am not sure if I did this right. I put the first two scripts (I started with the just displaying the rules in the GUI before attempting the second part (writing the txt file in-game).

When I tried the GUI in the Editor and clicked on the button (which has the command:    loadFile(Rules);

And in the console it says when I click the button, loadFile: unknown command    [or something along those lines]

I have the two commands or functions in the first part all in one file in the add-ons folder. I replaced the GUI Name with my name for it as you said to.

Any ideas what I am doing wrong?

Thanks a lot!!

They should go in your client folder.

Did you replace MyGUIMltextctrl with the name of yours? Did you even put a MLtextCtrl on your gui and name it?

Client folder under add-ons? And is that the GUI file, the script file or both?

And yes I did do the second part you just mentioned.

Thanks



Alright, I'll make sure it's like that.

For the button in the gui, should the command line say: " loadFile(Add-Ons/text.txt) " or " loadFile("Add-Ons/text.txt"); " or " loadFile();

Thanks

Code: [Select]
"[MyGuiMLTextCtrl].loadFile(\"Add-Ons/text.txt\");"
Replacing [MyGuiMLTextCtrl] with the name of the text control.


About the \"s:
A \ in a text string means that the next character or few characters are treated specially:
\c# changes the colour. Type /colortest in your server for a list.
\n adds a newline - "String" NL "String" could be replaced with "String\nString".
\x## adds a special character. (\x99 is a TM sign, \x97 is a long dash, etc)
\" (\' in a tagged string) adds the quote sign without causing errors by 'ending' the string.
echo("Some Guy says "Hi"."); would give an error - Hi isn't recognised as a string correctly.
echo("Some Guy says \"Hi\"."); would work fine.

Is anyone willing to make this for me. I will give full credit to whoever does it. Plus, for some reason when I save my GUI (I save it in multiple directories), it doesn't show up to load when I reopen to mission editor after have quit the game.

Please let me know,

Thanks!