PDA

View Full Version : Using the Player Array



Abyssal
07-02-2007, 08:53 PM
Using the Player Array, by Abyssal

In this tutorial, you will learn the following:
How to setup the array
Switching players in a script============================================ =====
= Setting up the array
=================================================
To start off, make sure you don't forget to add this to every one of your SRL scripts.
This will ensure that all of the SRL variables/functions/procedures are usable in the script:
{.include SRL\SRL.scar}
Now - we have to declare the player array, using a simple procedure called DeclarePlayers.
The first three lines of this procedure are:
HowManyPlayers:= 3;
NumberOfPlayers(HowManyPlayers);
CurrentPlayer:= 0;
Good! Now we're getting somewhere. The first line of that code tells SRL that there are going to be a total of 5 players in the array. The second line is a procedure that sets that value within SRL. The third tells SRL which player is going to start. If you have HowManyPlayers set to 5, then there is actually Players[0] - Players[4], because arrays in SCAR start at 0, not 1.

Next we need to add the code that tells SRL what the username and password of all the characters. You do this by adding the following code, for each player. Replace Players[0] with Players[NUMBER] for each different player.
Players[0].Name:= '';
Players[0].Pass:= '';
Players[0].Nick:= '';
Players[0].Active:= True;
The first line, obviously declares the username, and the second line declares the password. The third line lets SRL know the nickname of the player. This is one of the most important thing because without it, anti-randoms will not work. Make sure this value is 3-4 letters from the middle of the username, without any capital letters. The fourth tells SRL if the player is active. If this is false, the player will not be used, and will be skipped if it is asked to login. Most of time, you should set it to true.

Some scripts require you to let SRL know where your player is in the world, however most of the time it is not required. To have the user manually input this information, add this line with the rest of them:
Players[0].Loc:= '';
Alright, good. Now the player array is almost complete! However, if you are making a script that needs to keep track of certain booleans/integers/strings for each different player, you can use ANY of the following to track those things.
Players[0].Boolean1:= True;
Players[0].Boolean2:= True;
Players[0].Boolean3:= True;

Players[0].Integer1:= 0;
Players[0].Integer2:= 0;
Players[0].Integer3:= 0;

Players[0].String1:= '';
Players[0].String2:= '';
Players[0].String3:= '';
There are three booleans, three integers, and three strings for each player in the array. There is also one more, I am not 100% what it is used for, but you could use it for what it is named for:
Players[0].Ore:= '';
At the very end of the DeclarePlayers procedure, you should always add this line for safety. This ensures that the anti-randoms for the first player will work:
NickNameBMP:= CreateBitmapMaskFromText(Players[CurrentPlayer].Nick, UpChars);
That simply created a bitmap mask from the first players nickname. Don't worry, SRL takes care of all that for you. By now, your procedure should look like this:
procedure DeclarePlayers;
begin
..HowManyPlayers:= 3;
..NumberOfPlayers(HowManyPlayers);
..CurrentPlayer:= 0;

..Players[0].Name:= '';
..Players[0].Pass:= '';
..Players[0].Nick:= '';
. Players[0].Active:= True;

. Players[1].Name:= '';
. Players[1].Pass:= '';
. Players[1].Nick:= '';
. Players[1].Active:= True;

. Players[2].Name:= '';
. Players[2].Pass:= '';
. Players[2].Nick:= '';
. Players[2].Active:= True;

. NickNameBMP:= CreateBitmapMaskFromText(Players[CurrentPlayer].Nick, UpChars);
end;
Good, now that we have setting up the array out of the way, let's move on to the next part!



=================================================
= Switching players in a script
=================================================
Basically, and simply put, to switch players in a script, use the following command. True means that the player will be usable next time it comes to it's turn. False means it's state is inactive. Active:= False.
NextPlayer(True);
But a little bit more in depth would be nice, no? Of course! Let's say you are attempting to open a bank, and if after 5 tried, if the player cannot find the bank it is lost, and we need to log it out and move on to the next player. But we don't want the script trying to run the lost player again, we can use the following code:
procedure OpenTheBank;
var
. Tries: Integer;
begin
..while(Tries <= 5)do
..begin
..if(OpenBank)then
... Exit;
... Inc(Tries);
..end;
. NextPlayer(False);
end;
Disregard the rest of the code, but basically what it does, is perform a loop of bank openings. If the bank is opened, or OpenBank returns true, the procedure will end. But if after 5 tries, the bank is still not open, it will logout and move on to the the next player without coming back to the lost one.

And with those failsafes, you can also add in Next Player at the end of the main loop. Like so:
begin
. repeat
... WalkToMine;
....repeat
..... MineOre;
....until(Players[CurrentPlayer].Integer1 >= 28);
... WalkBack;
... OpenTheBank;
... NextPlayer(True);
..until(False);
end.
Again, ignore the extra code, but basically what that does is start a loop, for the players. Inside there is another loop that mines 28 ore for the current player, then the loop ends, presuming the ore mined is in the player variable Integer1. If all of the procedures went well, the NextPlayer(True) procedure will be called. But if the OpenTheBank procedure went amiss, it would have switched players earlier. However there is a slight error in coding I just caught. If it switched players in the Bank procedure, and continues on it will switch again, in the main loop. To be honest, I cannot think how to fix this, unless you just don't call NextPlayer in the bank procedure. I apologize deeply for this unfortunate happening - I hope you can forgive me! This leaves a gap for you to use your awesome scripting skills.

Here ends the tutorial. I hope you have learned a lot about the player array. Just writing this made me realize some things! Like my little error at the end - which I again, apologize for. Make sure those things don't happen in your script :eek:!


~ ABYSSAL

EDIT: We really ought to get SCAR tags that do that above. I don't like the little extra quote box.

Diabloizzle
07-20-2007, 01:22 AM
How do you set player variables?

EDIT: Nevermind I know now.