SRL Archive - Research & Development Lounge

Some SendInterSCARMessage works


Floor66

2009-11-09 03:34:59

The Idea
I'm sitting here at home, being ill, so I thought, why not make up something useless in SCAR! It's been quite a while since I produced any code :p
Anyway, this time it's InterSCARMessaging; using this you can make two (or more) SCAR's on the same computer communicate, sending messages, integers, booleans, etc. I've only used simple messages for now.

How
I use two scripts, a 'Sender' and a 'Receiver' (whereas the Sender constantly checks for F-key presses and then sends the corresponding command via InterSCARMessaging to other SCAR instances currently running).
So Sender sends a command through a channel when you press an F-key, and the Receiver is constantly checking 10 different channels to see if there's a command being put through them.
I've made it clear all channels after a command has been executed, this is necessary!

Receiver.scar

Program New;

{*************************************************
~~ SendInterSCARMessage test by Floor66 ~~
THIS IS THE RECEIVING SCRIPT

Contains:
* Co-operates with the SENDER
* A maximum of 10 channels (can be changed by
changing the array length of 'Received')
* Clears all channels after a command has
been sent, to prevent bugs
**************************************************}

Var
Received : TStringArray;

Procedure ClearAllChannels;
Var
i : Integer;
Begin
For i := 0 To High(Received) Do
SendInterSCARMessage(IntToStr(i), '');
End;

Procedure ScriptTerminate;
Begin
ClearAllChannels;
End;

Procedure Proggy;
Begin
WriteLn('---------------------------------');
WriteLn(' ~SCARMessager by Floor66~ ');
WriteLn('You sent command: progress report');
WriteLn('---------------------------------');
End;

Procedure HandleMsg(Command : String; id : Integer);
Begin
Case LowerCase(Command) Of
'progress report': Proggy;
'stop script': TerminateScript;
'clear debug': ClearDebug;
End;
Wait(150);
ClearAllChannels;
End;

Procedure CheckChannels;
Var
i : Integer;
Begin
For i := 0 To High(Received) Do
Begin
Received[i] := LowerCase(ReadInterSCARMessage(IntToStr(i)));
If Received[i] <> '' Then
HandleMsg(Received[i], i);
End;
End;

Procedure Init;
Begin
ClearAllChannels;
SetLength(Received, 10);
End;

Begin
Init;
Repeat
CheckChannels;
Wait(10);
Until(False);
End.

Sender.scar

Program New;

{*************************************************
~~ SendInterSCARMessage test by Floor66 ~~
THIS IS THE SENDING SCRIPT

Contains:
* Co-operates with the RECEIVER
* In the procedure 'Init' you can change the
amount of commands, what they do(*) and
which F-key activates them!
* The integer 'Received' here must be equal
to the array length of 'Received' in the
RECEIVER

(*): Make sure this matches with the
'HandleMsg' procedure in the RECEIVING
script!
**************************************************}

Var
Commands : TStringArray;
CommandsFKeys : TIntegerArray;
Received : Integer;

Procedure ClearAllChannels;
Var
i : Integer;
Begin
For i := 0 To Received Do
SendInterSCARMessage(IntToStr(i), '');
End;

Procedure ScriptTerminate;
Begin
ClearAllChannels;
End;

Procedure CheckFKeys;
Var
i : Integer;
Begin
For i := 0 To High(CommandsFKeys) Do
Begin
If IsFKeyDown(CommandsFKeys[i]) Then
SendInterSCARMessage(IntToStr(i), Commands[i]);
End;
End;

Procedure Init;
Begin
SetLength(CommandsFKeys, 12);
SetLength(Commands, 3);

Commands[0] := 'progress report';
Commands[1] := 'stop script';
Commands[2] := 'clear debug';

CommandsFKeys[0] := 1;
CommandsFKeys[1] := 2;
CommandsFKeys[2] := 3;

Received := 10;
End;

Begin
Init;
Repeat
CheckFKeys;
Wait(10);
Until(False);
End.

Ideas for the future
  • A simple txt/ini file with the commands/fkeys/etc. so you don't have to go and change both scripts each and every time
  • Actually using it in a script
  • Maybe a simple form with a few buttons that trigger the commands, instead of F-keys (they trigger other things like a help menu (F1) or something like that aswell usually)

RAM

2009-11-09 12:39:06

Nice Work, hope you get to feeling better :)

Another application would be when you have a need for 2 characters to interact with each other…say…making a script to log in every 15 minutes and give your trade limit to another character ;)

Floor66

2009-11-09 12:51:41

Thanks :)

Ah, yes, that'd be quite easy. Another idea is maybe a script that mines ore/chops logs, then when the inventory is full, send a message for the other SCAR to log in and take the load off of him, walk to the bank, bank, walk back, log out, and just keep waiting (can be done with 1 or 2 SMARTs).