Results 1 to 16 of 16

Thread: Writing to a text file

  1. #1
    Join Date
    Mar 2014
    Posts
    9
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default Writing to a text file

    I'm trying to make a drop trader. It'll be a multi-bot script that uses text file reads and writes to communicate between bots(requesting, ready, world 777, location ect).

    The issue I'm having is that all the write commands I'm finding go to the debug box, and I need to write to an external file.
    And read, but writing comes first.

    EDIT:WOOHOO, I can apparently post threads if I do so during class on uni computers. Yay learning.
    Pollinate, don't hate.

  2. #2
    Join Date
    Jan 2012
    Posts
    1,596
    Mentioned
    78 Post(s)
    Quoted
    826 Post(s)

    Default

    Simba Code:
    FID := RewriteFile(FILEPATH, True);
    WriteFileString(FID, COMMANDTOWRITE)
    CloseFile(FID);

    And...
    Simba Code:
    FID := OpenFile(FILEPATH,true);
    ReadFileString(FID, VARFORFILECONTENTS, FileSize(FID));
    CloseFile(FID);

    So the first one would overwrite to a file, the second one would read to a file.

  3. #3
    Join Date
    Dec 2011
    Posts
    2,147
    Mentioned
    221 Post(s)
    Quoted
    1068 Post(s)

    Default

    Isn't this an unnecessary workaround seeing as scripts do have multiplayer capability?
    I'm not sure how it works with Rafiki atm, but for this you could potentially tell player[0] to do something and player[1] to do something else.

  4. #4
    Join Date
    Mar 2014
    Posts
    9
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Thank you very much. That gives me lots to work with.

    EDIT:
    Quote Originally Posted by Clarity View Post
    Isn't this an unnecessary workaround seeing as scripts do have multiplayer capability?
    I'm not sure how it works with Rafiki atm, but for this you could potentially tell player[0] to do something and player[1] to do something else.
    I read a bunch of places that you couldn't have multiple RS clients up from the same simba instance, and that multiplayer was for cycling through players on logout. I didn't check the dates though so those might have been outdated posts. Global variables would be worlds easier.

    The (eventual)idea is to have some bots mining clay or cutting wood while another is softening clay or plank running, with the softeners and plank runners requesting restocks in the form of bank notes.
    Last edited by Arseface; 04-01-2014 at 06:43 PM.
    Pollinate, don't hate.

  5. #5
    Join Date
    Dec 2011
    Posts
    2,147
    Mentioned
    221 Post(s)
    Quoted
    1068 Post(s)

  6. #6
    Join Date
    Jan 2012
    Posts
    1,596
    Mentioned
    78 Post(s)
    Quoted
    826 Post(s)

    Default

    There are also some built in read/write functions for settings files. those can be found... here
    http://docs.villavu.com/simba/script....html#writeini

  7. #7
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    Quote Originally Posted by Turpinator View Post
    There are also some built in read/write functions for settings files. those can be found... here
    http://docs.villavu.com/simba/script....html#writeini
    It might be easier to go with the Write/ReadINI functions like Turpinator said.

    You can easily distinguish between the commands you're looking for. Like:
    Progress Report:
    [TRADE]
    RequestTrade=true
    
    [WORLD]
    SwitchWorld=false
    CurrentWorld=100
    
    [LOCATION]
    CurrentLocation=ClayMine

  8. #8
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Create a socket and communicate via that. More robust, much easier.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  9. #9
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by Daniel View Post
    Create a socket and communicate via that. More robust, much easier.
    My suggestion too, but the reason I did not suggest this, is because if he has to ask, he most likely wants something simple. I know I know, bad assumptions.
    There used to be something meaningful here.

  10. #10
    Join Date
    Mar 2014
    Posts
    9
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Quote Originally Posted by Coh3n View Post
    It might be easier to go with the Write/ReadINI functions like Turpinator said.

    You can easily distinguish between the commands you're looking for.
    Quote Originally Posted by Daniel View Post
    Create a socket and communicate via that. More robust, much easier.
    I want to do things the "right" way as soon as possible so I form better habits. If sockets are better for one reason or another I'd love to try using them, but I don't see any scripts or tutorials I can use for reference.

    Unfortunately I don't have any examples that use sockets to base my code off of, and I'm not entirely sure what a socket IS. Is it stored in memory, is it written to a file? Does it broadcast its location to the system or does it wait for access? Do I use multiple socket objects to store my information or a single one? I need to know how something works to make it reliable.

    I've fiddled with both the file reader and ini reader. Neither seems any faster, .ini is definitely easier, I can't tell which is easier to check for errors yet.
    Pollinate, don't hate.

  11. #11
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    Quote Originally Posted by Arseface View Post
    I want to do things the "right" way as soon as possible so I form better habits. If sockets are better for one reason or another I'd love to try using them, but I don't see any scripts or tutorials I can use for reference.

    Unfortunately I don't have any examples that use sockets to base my code off of, and I'm not entirely sure what a socket IS. Is it stored in memory, is it written to a file? Does it broadcast its location to the system or does it wait for access? Do I use multiple socket objects to store my information or a single one? I need to know how something works to make it reliable.

    I've fiddled with both the file reader and ini reader. Neither seems any faster, .ini is definitely easier, I can't tell which is easier to check for errors yet.
    A socket is used to communicate over a network (i.e. communicate between two computers). In this case I don't know what the advantage would be, maybe @Frement; or @Daniel; can elaborate.

    Since you just want to communicate between two instances of Simba, and you're a beginner, I would just go with INI writing/reading. It's very simple and does exactly what you need.

  12. #12
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    661
    Mentioned
    35 Post(s)
    Quoted
    102 Post(s)

    Default

    The binary file read\write example with LaPe:
    Simba Code:
    program new;

    procedure WriteToFile;
      var
      FileStream1: TFileStream;
      Int1, Int2, Int3, Int4: Integer;
    begin
    Int1 := 100;
    Int2 := 200;
    Int3 := 1918986307;
    Int4 := 1702521171;
    FileStream1.init('D:\Test1.file',
                     $FF00 or $0001 or $0020);
    try
      FileStream1.WriteBuffer(Int1, SizeOf(Int1));
      FileStream1.WriteBuffer(Int2, SizeOf(Int2));
      FileStream1.WriteBuffer(Int3, SizeOf(Int3));
      FileStream1.WriteBuffer(Int4, SizeOf(Int4));
      finally
      FileStream1.Free;;
      end;
    end;

    procedure ReadFromFile;
      var
      FileStream1: TFileStream;
      Int1, Int2, Int3, Int4: Integer;
      Str1: String;
    begin
    FileStream1.init('D:\Test1.file',
                     $0000 or $0020);
    try
      FileStream1.ReadBuffer(Int1, SizeOf(Int1));
      FileStream1.ReadBuffer(Int2, SizeOf(Int2));
      FileStream1.ReadBuffer(Int3, SizeOf(Int3));
      FileStream1.ReadBuffer(Int4, SizeOf(Int4));
      finally
      FileStream1.Free;;
      end;
    Str1 := 'Int1-'+IntToStr(Int1)+' Int2-'+IntToStr(Int2)+' Int3='
             +IntToStr(Int3)+' Int4'+IntToStr(Int4);
       WriteLn(str1);
    end;

    begin
     WriteTofile;
     ReadFromfile;
    end.

    Output:

    Compiled successfully in 390 ms.
    Int1-100 Int2-200 Int3=1918986307 Int41702521171
    Successfully executed
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  13. #13
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by Coh3n View Post
    A socket is used to communicate over a network (i.e. communicate between two computers). In this case I don't know what the advantage would be, maybe Frement or Daniel can elaborate.
    Advantages:
    before dropping item(s) check that other client is still connected
    * script crash / termination
    using text files the other client could still write that it is online, and after that crash / terminate
    * this could be countered with a check if the other player is online, but it will take a while for him to automatically disconnect due to being afk.

    no need to constantly read or write to a file

    extension possibilities for multiple players with ease
    There used to be something meaningful here.

  14. #14
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Everyone is forgetting one thing.. Sockets in Simba is pretty garbage..

    1. You cannot poll/select simba sockets.
    2. You cannot accept clients without blocking.
    3. Every operation in Simba's sockets block.

    Server.Simba:
    Simba Code:
    {$I Sockets.Simba}

    procedure onSocketConnect(sock, id: Int32);
    var
      str: String;
    begin
      if (id <> -1) then
        while(true) do
        begin
          Str := ReadSocket(sock);
          if (Length(Str) > 0) then
            writeln('Received from client: ', str);

          WriteSocket(sock, 'hello client');

          if (Not Server_PollClient(sock)) then
            break;

          Sleep(100);
        end;
    end;

    begin
      SocketSetup;
      Server_AcceptClients;
    end.


    Client.Simba:
    Simba Code:
    {$I Sockets.Simba}

    procedure onSocketConnect(sock, id: Int32);
    var
      Data: String;
    begin
      ClearDebug;
      writeln('Connected to server..');

      while(true) do
      begin
        WriteSocket(sock, 'hello server');
        Data := ReadSocket(sock);

        if (Length(Data) > 0) then
          writeln('Received from server: ', Data);

        if (Not Server_PollClient(sock)) then
          break;

        Sleep(100);
      end;
    end;

    begin
      SocketSetup;
    end.


    The above uses:

    Sockets.Simba:
    Simba Code:
    {$IFDEF RIDDLER}
    {$loadlib Riddler}
    {$ENDIF}

    var
      sockets_shutdown: boolean;
      server_socket: Int32;
      client_sockets: TIntegerArray;

    procedure onSocketConnect(socket, id: Int32); forward;

    procedure Client_Connect(var sock: Int32);
    begin
      sock := CreateSocket;
      SetSocketTimeout(sock, 10);

      try
        ConnectSocket(sock, '127.0.0.1', '27016');
      except
        writeln('Server not running..');
        FreeSocket(sock);
        sock := -1;
      end;
    end;

    procedure Server_Listen(var sock: Int32);
    begin
      sock := CreateSocket;
      SetSocketTimeout(sock, 10);

      try
        BindSocket(sock, '127.0.0.1', '27016');
        ListenSocket(sock);
      except
        writeln('Server Port in use..');
        FreeSocket(sock);
        sock := -1;
      end;
    end;

    function Server_PollClient(sock: Int32): boolean;
    begin
      Result := True;
      try
        SendSocket(sock, #0#0 + 'Ping' + #0#0);
      except
        Result := False;
      end;
    end;

    procedure Server_ClearDisconnectedClients;
    var
      I, J: Integer;
    begin
      For I := 0 To High(client_sockets) Do
        If (Not Server_PollClient(client_sockets[I])) Then
        begin
          try
            CloseSocket(client_sockets[I]);
            FreeSocket(client_sockets[I]);
          except
            try
              FreeSocket(client_sockets[I]);
            except
            end;
          end;

          For J := I To High(client_sockets) - 1 Do
            swap(client_sockets[J], client_sockets[J + 1]);

          SetLength(client_sockets, High(client_sockets));
        end;
    end;

    procedure Server_AcceptClients;
    var
      sock: Int32;
    begin
      while(Not sockets_shutdown) do
      begin
        sock := -1;
        Server_ClearDisconnectedClients;
        sock := AcceptSocket(server_socket);

        if (sock <> - 1) then
        begin
          SetLength(client_sockets, Length(client_sockets) + 1);
          client_sockets[Length(client_sockets) - 1] := sock;
          onSocketConnect(sock, Length(client_sockets));
          Server_ClearDisconnectedClients;
        end;
      end;
    end;

    Function ReadSocket(sock: Int32): String;
    begin
      try
        Result := RecvSocket(sock);
      except
        writeln('Read operation timed out.');
      end;

      If (Result = #0#0 + 'Ping' + #0#0) Then
        Result := '';
    end;

    Procedure WriteSocket(sock: Int32; Data: String);
    begin
      try
        SendSocket(sock, Data);
      except
        writeln('Write operation timed out.');
      end;
    end;

    procedure ShutdownSockets;
    var
      I, L: Int32;
    begin
      sockets_shutdown := true;

      if (server_socket <> -1) then
      begin
        try
          CloseSocket(server_socket);
          FreeSocket(server_socket);
        except
          FreeSocket(server_socket);
        end;
      end;

      L := High(client_sockets);

      For I := 0 To L do
      begin
        try
          CloseSocket(client_sockets[I]);
          FreeSocket(client_sockets[I]);
        except
          FreeSocket(client_sockets[I]);
        end;
      end;
    end;

    procedure SocketSetup;
    begin
      AddOnTerminate('ShutdownSockets');
      Server_Listen(server_socket);

      if (server_socket = -1) then
      begin
        SetLength(client_sockets, 1);
        Client_Connect(client_sockets[0]);
        onSocketConnect(client_sockets[0], 0);
      end else
        onSocketConnect(server_socket, -1);

      if ((server_socket = -1) and (length(client_sockets) = 0)) then
        TerminateScript;
    end;


    The problem is when you have to accept more than one client.. That's when you're in trouble. Otherwise it is easy to write back and forth using a single socket.. If OP plans on using multiple clients then the above will not be very good for him. Otherwise if it is just two Simba's talking to eachother then the above will work.
    I am Ggzz..
    Hackintosher

  15. #15
    Join Date
    Nov 2006
    Posts
    2,369
    Mentioned
    4 Post(s)
    Quoted
    78 Post(s)

    Default

    I'd just go with files unless you want to run the clients on different machines.
    Quote Originally Posted by DeSnob View Post
    ETA's don't exist in SRL like they did in other communities. Want a faster update? Help out with updating, otherwise just gotta wait it out.

  16. #16
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    Quote Originally Posted by Frement View Post
    Advantages:
    before dropping item(s) check that other client is still connected
    * script crash / termination
    using text files the other client could still write that it is online, and after that crash / terminate
    * this could be countered with a check if the other player is online, but it will take a while for him to automatically disconnect due to being afk.

    no need to constantly read or write to a file

    extension possibilities for multiple players with ease
    You could still do that with an INI file. Just have a section called IsPlayerAConnected. I'm sure either would work, but I suggested INIs because OP is a beginner scripter and INIs are pretty straight forward.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •