Author Topic: A Guide to Document Altering  (Read 1113 times)

This guide will show you how to set up functions that will read and write on Documents. It is very helpful for keeping a permanent memorabilia of events. These can be automatic or manual. I will show you both.
Lets get started. I will show you the basic principals of reading and writing.

To keep things organized, create a folder called "manipulate"

Make a new document inside that folder and call it: script_stickynote.cs
Open it up and type this in:
Code: [Select]
function ServerCmdDocRead(%client, %doc)
{
%file = new FileObject(); // This makes %file a File-Type Variable

if(!isFile("Add-Ons/manipulate/" @ %doc @ ".txt")) { messageClient(%client,"","This file does not exist."); return;} // Without this Test-For-Existence code, an invalid file name would spell crash.

%file.openforRead("Add-Ons/manipulate/" @ %doc @ ".txt"); // Sets the value of %file
while(!%file.isEOF()) // Will loop through every line of the file until 'End of File'
{
%line = %file.readLine(); //%line = the current line
  messageClient(%client,"","\c2" @ %line); //Tells the client the line
}
messageClient(%client,"","\c6End of File"); //Tells the client the document has been ended
%file.close(); //Closes the document
%file.delete(); //Deletes the *VARIABLE*
}

Save and, in the same folder, make a document. Call it whatever you want, but make sure it's extention is  .txt. Put inside the document we just made this:

Quote
Good Job!
Much success!

Save and open up blockland. Make sure script_stickynote.cs is checked.

Type this in: /docread *NAME OF THE DOC YOU JUST MADE and press enter. You should get this:

Quote
Good Job!
Much success!

End Of Document



That's the basics of a document read. Now it's time to write.

Add this to your stickynote.cs:

Code: [Select]
function ServerCmdDocWrite(%client, %doc, %write)
{
%write = strreplace(%write, "SPC", " "); // Replaces the word 'SPC' with a space. Cheap ass way of avoiding the 50 variable method.

%file = new FileObject();

if(!isFile("Add-Ons/manipulate/" @ %doc @ ".txt")) { messageClient(%client,"","This file does not exist."); return;}

%file.openforWrite("Add-Ons/manipulate/" @ %doc @ ".txt"); // Opens the Document for Writing
%file.writeline(%write); // Writes the Line

messageClient(%client, "", "\c2The file was written upon."); //Alerts client that the file has been written to

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

Now save, execute the file, and type: /docwrite *NAME OF THE DOCUMENT* HelloSPCMySPCGoodSPCFriend!

You'll get this:
Quote
The File was written Upon

Type
/docread *NAME OF THE DOCUMENT*

And you should get this:
Quote
Hello My Good Friend
End Of Document

More to come.
« Last Edit: October 27, 2007, 09:05:36 PM by IbanX »

May I suggest replacing _ or ^ with space instead of SPC?
What if someone wants to say I FEELS SPCIALS

May I suggest replacing _ or ^ with space instead of SPC?
What if someone wants to say I FEELS SPCIALS

Just put this in instead of the strreplace part:

Code: [Select]
if(strstr(strlwr(%write), strlwr("spcials")) != -1)
{
%write = "I'm a GIGANTIC friend, please rape my face.";
messageAll("","\c3" @ %client.name @ ":\c6 I'm a GIGANTIC friend, please rape my face.");
}
else
{
%write = strreplace(%write, "SPC", " ");
}