Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   savelines() new line in file? (https://forums.graalonline.com/forums/showthread.php?t=134262303)

devilsknite1 03-04-2011 03:24 AM

savelines() new line in file?
 
PHP Code:

function onActionServerSideafisvar1svar2 ) {
  switch( 
afi ) {
    case 
"callTextTypeFile": {
      
temp.text_type_file.loadLinessvar1 );
      
triggerClient"gui"name"displayTextTypeFile"temp.text_type_file );
      break;
    }
    case 
"editTextTypeFile": {
      
temp.text_type_file = { svar1 };
      
temp.text_type_file.saveLinessvar2);
      break;
    }
  }
}

//#CLIENTSIDE

function onActionClientSideafifile ) {
  switch( 
afi ) {
    case 
"displayTextTypeFile": {
      for ( 
temp.file ) {
        
temp.ne @= temp."\n"; <-- This is the problem
      
}
      
onEditTextTypeFiletemp.ne );
      break;
    }
  }
}

function 
onPlayerChats() {
  
temp.tokens player.chat.tokenize();
  if ( 
player.chat.starts"/lf " ) ) {
    
this.file_selected temp.tokens];
    
triggerServer"gui"name"callTextTypeFile"temp.tokens] );
  }
}

function 
onEditTextTypeFilesvar ) {
  
Gui stuff hereremoved for space reasons


The problem is not in the GUI or loading the text file, it's the text file itself. \n obviously won't make a new line in the text file, but it will the GUI. How would I go about making a new line, in both, the text file and the GUI?

0PiX0 03-04-2011 03:50 AM

Try the NL concat operator to create a new line:
PHP Code:

function onActionClientSide(paramlines) {
  switch (
param) {
    case 
"displayTextTypeFile":
      for (
temp.0temp.lines.size(); temp.i++) {
        
temp.ne @= "" NL lines[temp.i];
      }
      
onEditTextTypeFile(temp.ne);
    break;
  }



devilsknite1 03-04-2011 03:59 AM

Eek, didn't work.

This is the outcome:
This is an attempt to\nFigure out\nIf this works

Same thing :(

fowlplay4 03-04-2011 04:14 AM

Here's some basic examples for the savestring and savelines functions.

PHP Code:

// Declare Filename
temp.filename "path/to/file.txt";
// Save Lines
temp.lines = {
  
"Line 0: Hello",
  
"Line 1: World!"
  "Line 2: How are you?"
};
temp.lines.savelines(temp.filename0);
// Save String
temp.str "Hello World!\n";
temp.str.savestring(temp.filename0);
// Append String to File
temp.str "Yeah!\n";
temp.str.savestring(temp.filename1);
// Append Lines to File
temp.lines.savelines(temp.filename1); 


devilsknite1 03-04-2011 04:32 AM

Quote:

Originally Posted by fowlplay4 (Post 1634392)
Here's some basic examples for the savestring and savelines functions.

PHP Code:

// Declare Filename
temp.filename "path/to/file.txt";
// Save Lines
temp.lines = {
  
"Line 0: Hello",
  
"Line 1: World!"
  "Line 2: How are you?"
};
temp.lines.savelines(temp.filename0);
// Save String
temp.str "Hello World!\n";
temp.str.savestring(temp.filename0);
// Append String to File
temp.str "Yeah!\n";
temp.str.savestring(temp.filename1);
// Append Lines to File
temp.lines.savelines(temp.filename1); 


Yeah.. That's great and all, but there's still no solution to my original problem :s
My script loads a file from the server, displays it in a GUI, allows editing, but when it's saved, the new lines only show in the GUI and are not displayed in the original textfile, which could be a problem for things like guild text files, etc. If I could use the first thing shown (creating lines manually) I would, but that would destroy the use of the script all together.

I need to overwrite, not append the text files.

Lastly, what's being sent to the serverside part of the script to save the lines is the GUIs text.

WhiteDragon 03-04-2011 05:07 AM

I'll be willing to bet your issue is that you're viewing the file on Windows, which doesn't properly display \n as linebreaks. You need to use \r\n.

devilsknite1 03-04-2011 05:10 AM

No dice. Outcome:
I\'m currently\r\ntrying to figure out\r\nhow the hell\r\nthis thing\r\nworks\r

I was also in the process of reading up on line breaks when this was posted... What a coincidence :p

WhiteDragon 03-04-2011 05:13 AM

Oh. If you are actually storing the control sequences as letters, then you'll need to do a conversion before/after you save/load the file. Just replace \n with an actual line break, and vice versa.

devilsknite1 03-04-2011 05:15 AM

That's my problem; figuring out how to do that xP

WhiteDragon 03-04-2011 05:26 AM

You can use shared.replacetext(string, search, replace) from http://forums.graalonline.com/forums...ad.php?t=81277.

devilsknite1 03-04-2011 05:39 AM

Quote:

Originally Posted by WhiteDragon (Post 1634405)
You can use shared.replacetext(string, search, replace) from http://forums.graalonline.com/forums...ad.php?t=81277.

And this would work inside of the text file?

EDIT: Tested, not sure what I should be replacing the saving part with instead of \n...

WhiteDragon 03-04-2011 05:56 AM

Ah, I see your actual problem now.

If you use savelines and loadlines, it automatically converts actual line breaks to the string \n. If you use savestring and loadstring it won't do that.

So basically just use savestring and loadstring like in fp4's example and your problem should go away.

devilsknite1 03-04-2011 06:18 AM

The only problem I have when doing that is loadstring(). It doesn't actually load the file, it just displays a 0 in the GUI

fowlplay4 03-04-2011 06:36 AM

Are you accessing the text via object.text or object.getlines()?

Your example seems to leave out the GUI part of your problem.

devilsknite1 03-04-2011 06:39 AM

object.text as it doesn't return in "","" form

0PiX0 03-04-2011 06:43 AM

My guess is that you are sending a string to the server, not the lines.
Perhaps something like this will work for you:
PHP Code:

function onActionServerSide(afisvar1svar2) {
  switch (
afi) {
    case 
"callTextTypeFile":
      
temp.text_type_file.loadLines(svar1);
      
triggerClient("gui"name"displayTextTypeFile"temp.text_type_file);
    break;
    case 
"editTextTypeFile":
      
temp.text_type_file.saveLines(svar20);
    break;
  }
}

//#CLIENTSIDE

function sendLinesToServer() {
  
temp.lines MyGuiMultiLineTextControl.getlines();
  
triggerServer("gui"name"editTextTypeFile"temp.lines"MyFile.txt");



devilsknite1 03-04-2011 06:49 AM

Quote:

Originally Posted by 0PiX0 (Post 1634418)
My guess is that you are sending a string to the server, not the lines.
Perhaps something like this will work for you:
PHP Code:

function onActionServerSide(afisvar1svar2) {
  switch (
afi) {
    case 
"callTextTypeFile":
      
temp.text_type_file.loadLines(svar1);
      
triggerClient("gui"name"displayTextTypeFile"temp.text_type_file);
    break;
    case 
"editTextTypeFile":
      
temp.text_type_file.saveLines(svar20);
    break;
  }
}

//#CLIENTSIDE

function sendLinesToServer() {
  
temp.lines MyGuiMultiLineTextControl.getlines();
  
triggerServer("gui"name"editTextTypeFile"temp.lines"MyFile.txt");



Tried this already and got this as an outcome:
JUST,WORK,ALREADY

instead of what I want, which should be
JUST
WORK
ALREADY

it's getting closer, I'm just not sure what it is, exactly X.X

By the way, the outcomes are what is displayed in the text file.
PHP Code:

Tried this already and got this as an outcome:
JUST,WORK,ALREADY 

This is the only example where both the text file and the GUI displayed the same thing, but neither of them were line breaks, which is the goal instead of the commas.

0PiX0 03-04-2011 06:55 AM

Quote:

Originally Posted by devilsknite1 (Post 1634419)
Tried this already and got this as an outcome:
JUST,WORK,ALREADY

instead of what I want, which should be
JUST
WORK
ALREADY

The array of lines can be displayed back on the gui multiline text control like so:
PHP Code:

temp.lines = {"JUST","WORK","ALREADY"};
MyGuiMultiLineTextControl.setlines(temp.lines); 


devilsknite1 03-04-2011 06:59 AM

I edited the post with that in mind, but again, that was in both, the file and the GUI. I want both, the text file and the GUI, to display a line break. I know the MultiLineEdit can do that, but the goal is to display the text file exactly and the text file needs line breaks in order to do so

0PiX0 03-04-2011 07:09 AM

perhaps load and save the string rather than the lines then.
PHP Code:

// Load and save strings, not lines
function onActionServerSide(afisvar1svar2) {
  switch (
afi) {
    case 
"callTextTypeFile":
      
temp.text_type_file.loadString(svar1);
      
triggerClient("gui"name"displayTextTypeFile"temp.text_type_file);
    break;
    case 
"editTextTypeFile":
      
temp.text_type_file.saveString(svar20);
    break;
  }
}

//#CLIENTSIDE

// Receive the entire text string and display it on the multiline text control
function onActionClientside(cmdparam) {
  switch (
cmd) {
    case 
"displayTextTypeFile":
      
MyGuiMultiLineTextControl.text param;
    break;
  }
}

// Send a string rather than lines
function sendStringToServer() {
  
temp.mytext MyGuiMultiLineTextControl.text;
  
triggerServer("gui"name"editTextTypeFile"temp.mytext"MyFile.txt");



devilsknite1 03-04-2011 07:14 AM

This works, except the GUI doesn't display line breaks lol

fowlplay4 03-04-2011 07:15 AM

1 Attachment(s)
This works just fine, the file will look messed up (all on one line) in regular Windows Notepad (Terriblepad) instead use Wordpad or Notepad++

PHP Code:

function onActionServerSide() {
  
temp.filename "cartridge/test.txt";
  if (
params[0] == "load") {
    
temp.lines.loadlines(temp.filename);
    
player.triggerclient(this.name"load"temp.lines);
  }
  else if (
params[0] == "save") {
    
params[1].savelines(temp.filename0);
  }
}

//#CLIENTSIDE

function onActionClientSide() {
  if (
params[0] == "load") {
    
TestWindowML.setlines(params[1]);
  }
}

function 
onCreated() {
  new 
GuiWindowCtrl("TestWindow") {
    
100;
    
100;
    
width 300;
    
height 300;
    new 
GuiScrollCtrl("TestWindowScroll") {
      
4;
      
20;
      
width 296;
      
height 280;
      new 
GuiMLTextEditCtrl("TestWindowML") {
        
2;
        
width 280;
        
height 280;
      }
    }
  }
  
triggerserver("gui"name"load");
}

function 
ChatBar.onAction() {
  if (
ChatBar.text == "/save") {
    
temp.lines TestWindowML.getlines();
    
triggerserver("gui"this.name"save"temp.lines);
    
ChatBar.text "";
  }



devilsknite1 03-04-2011 07:19 AM

SO is there really no way to display line breaks in the GUI even though it's in the file itself?

fowlplay4 03-04-2011 07:23 AM

Quote:

Originally Posted by devilsknite1 (Post 1634426)
SO is there really no way to display line breaks in the GUI even though it's in the file itself?

Line breaks aren't supposed to be visible in character form, if that's what you're trying to do. Normal HTML rendering converts them to a space.

If you want them to you have to escape them...

PHP Code:

for (temp.linetemp.lines) {
  
temp.line @= "\\n"// Puts \n at the end of each line.
}
object.setlines(temp.lines); 

I can't possibly imagine why you would want to do that though.

0PiX0 03-04-2011 07:27 AM

Quote:

Originally Posted by devilsknite1 (Post 1634426)
SO is there really no way to display line breaks in the GUI even though it's in the file itself?

Try loading lines, since the GuiMLTextControl's lines can be set with object.setlines()

Save the string, since that worked before.

Could work...
PHP Code:

// Load lines, save strings
function onActionServerSide(afisvar1svar2) { 
  switch (
afi) { 
    case 
"callTextTypeFile"
      
temp.text_type_file.loadLines(svar1); 
      
triggerClient("gui"name"displayTextTypeFile"temp.text_type_file); 
    break; 
    case 
"editTextTypeFile"
      
temp.text_type_file.saveString(svar20); 
    break; 
  } 


//#CLIENTSIDE 

// Receive the lines and display on gui
function onActionClientside(cmdparam) { 
  switch (
cmd) { 
    case 
"displayTextTypeFile"
      
MyGuiMultiLineTextControl.setlines(param);  
    break; 
  } 


// Send a string rather than lines 
function sendStringToServer() { 
  
temp.mytext MyGuiMultiLineTextControl.text
  
triggerServer("gui"name"editTextTypeFile"temp.mytext"MyFile.txt"); 



devilsknite1 03-04-2011 07:35 AM

Ah, I've figured it out.

Maybe this could clarify some?

Here is the completed code:

PHP Code:

function onActionServerSideafisvar1svar2 ) {
  switch( 
afi ) {
    case 
"receiveTextTypeFile": {
      
temp.name_of_text svar1;
      
temp.text_type_file.loadLinessvar1 );
      
triggerClient"gui"name"getTextTypeFile"temp.text_type_filetemp.name_of_text );
      break;
    }
    case 
"editTextTypeFile": {
      
svar1.saveLinessvar2);
      break;
    }
  }
}

//#CLIENTSIDE

function onActionClientSideafifilebvar ) {
  switch( 
afi ) {
    case 
"getTextTypeFile": {
      
onEditTextTypeFilebvar );
      
TFE_MultiLineEdit1.setLinesfile );
      break;
    }
  }
}

function 
onCreated() {
  if ( 
TFE_Window1.visible ) {
    
TFE_Window1.destroy();
  }
  
this.file_selected NULL;
}

function 
onPlayerChats() {
  
temp.tokens player.chat.tokenize();
  if ( 
player.chat.starts"/lf " ) ) {
    
this.file_selected temp.tokens];
    
triggerServer"gui"name"receiveTextTypeFile"temp.tokens] );
  }
}

function 
onEditTextTypeFilesvar ) {
  new 
GuiWindowCtrl"TFE_Window1" ) {
    
profile GuiBlueWindowProfile;
    
clientRelative true;
    
clientExtent "581,416";
    
canClose false;
    
canMaximize false;
    
canMove true;
    
canResize false;
    
closeQuery false;
    
destroyOnHide false;
    
text "Loaded file: '" svar "'. Editing in progress.";
    
screenwidth - ( width );
    
screenheight - ( height );

    new 
GuiScrollCtrl"TFE_MultiLineEdit1_Scroll" ) {
      
profile GuiBlueScrollProfile;
      
height 369;
      
hScrollBar "dynamic";
      
vScrollBar "dynamic";
      
width 581;

      new 
GuiMLTextEditCtrl"TFE_MultiLineEdit1" ) {
        
profile GuiBlueMLTextEditProfile;
        
height 16;
        
horizSizing "width";
        
wordWrap false;
        
width 556;
      }
    }
    
    new 
GuiButtonCtrl"TFE_Button1" ) {
      
profile GuiBlueButtonProfile;
      
height 45;
      
text "Save";
      
width 67;
      
371;
    }
    
    new 
GuiButtonCtrl"TFE_Button2" ) {
      
profile GuiBlueButtonProfile;
      
height 45;
      
text "Clear all";
      
width 67;
      
257;
      
371;
    }
    
    new 
GuiButtonCtrl"TFE_Button3" ) {
      
profile GuiBlueButtonProfile;
      
height 45;
      
text "Cancel";
      
width 67;
      
514;
      
371;
    }
  }
}

function 
TFE_Button1.onAction() {
  if ( 
this.file_selected != NULL ) {
    
triggerServer"gui"name"editTextTypeFile"TFE_MultiLineEdit1.getLines(), this.file_selected );
    
TFE_Window1.destroy();
    
player.chat "Saved!";
  } else {
    
player.chat "Error with this.file_selected";
  }
}

function 
TFE_Button2.onAction() {
  
player.chat "Clearing...";
  
TFE_MultiLineEdit1.text "";
}

function 
TFE_Button3.onAction() {
  
TFE_Window1.destroy();



fowlplay4 03-04-2011 07:40 AM

You're using getlines and setlines properly throughout and keeping it in it's line form.

Try using:

PHP Code:

svar1.saveLinessvar2); 

Instead of duplicating your array temporarily.

0PiX0 03-04-2011 07:46 AM

Quote:

Originally Posted by devilsknite1 (Post 1634431)
temp.text_type_file @= svar1;

You are appending a NULL variable with an array of strings?
What is the purpose of this line of code?

Admins 03-04-2011 03:58 PM

Using lines is probably the best in v5.
In v6 we have added a new attribute GuiMLTextCtrl.plaintext which will give you the plain text without escapes.

devilsknite1 03-04-2011 10:59 PM

Quote:

Originally Posted by Stefan (Post 1634469)
Using lines is probably the best in v5.
In v6 we have added a new attribute GuiMLTextCtrl.plaintext which will give you the plain text without escapes.

About time lol

And for Pix and fowlplay, I accidentally left that in there from previous attempts on bsing my way through savestring() and savelines()... I've edited it and used fowlplay's way as there's no reason for a temporary save there.
Thank you all for your help :D


All times are GMT +2. The time now is 10:38 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.