SRL Archive - OSR Advanced Scripting Tutorials

Multiplayer in forms.


jhildy

2007-07-13 23:16:47

Ok first things first. i know everyone has seen the forms with unlimited players and i am going to demonstrate how to do this. This technique was created by RSN and all of the procedures were probably made by him. I will just teach you to adapt them to your script.

1.First off go into Srl/SRL/misc/UsersManager.scar

2.Copy all of those procedures in there except "LoadForm" "TheForm" "ShowFormModal" and "TehForm" you don't have to copy the vars, only the procedure that i did not mention.

3.Setting up your form. First off you have to have created your form with every necissary object. For example
ignore everything that has an x over it they are all just to make it more user friendly. but everything else is necissary for the user manager to work for what i am doing.

In this script i had
Players[0].Name
Players[0].Pass
Players[0].Nick
Players[0].Integer[0]
Players[0].Active
I believe you can tell which of these represents each object in my form if not i will go into greater detail.
So i made my form for those specific Player variables.
Next you need to add those four buttons that are not Xed out.
Make one a left arrow(<-,<=,ect.) one say Add Player one say Delete Player and one with a right arrow(=>,->). The last thing you can do is add a groupbox to make it look neater (it is the Box that says Player Control around my buttons. Although this is not necissary to make this function, it looks neater. Another thing you should do is name your Edit1,Edit2 objects something easy like Eduser,Edpass,Ednick,Edloads,ect. Naming Eduser,Edpass,and Ednick makes your life easier since in RSN's procedures thats what the variables are already set as. Also add a label in the corner and name it lblUser.

4. Adapting the procedures. Ok this part takes the longest. Paste RSN's procedures into your script. As you look at his procedures, you probably can guess what you have to do. I will go through step by step anyway.
Look at this Procedure.
procedure AddUser;
begin
SetArrayLength(Players, GetArrayLength(Players) + 1);
Players[GetArrayLength(Players) - 1].Name := 'New Name';
Players[GetArrayLength(Players) - 1].Pass := 'New Pass';
Players[GetArrayLength(Players) - 1].Nick := 'New Nick';
Players[GetArrayLength(Players) - 1].Active := True;
Players[GetArrayLength(Players) - 1].Loc := 'New Location';
end;
Here you should set all of your Players Vars to something similar to this. For example if you have Players[0].Integer[0] and it is loads, i would reset that to 0 so you can just add in
procedure AddUser;
begin
SetArrayLength(Players, GetArrayLength(Players) + 1);
Players[GetArrayLength(Players) - 1].Name := 'New Name';
Players[GetArrayLength(Players) - 1].Pass := 'New Pass';
Players[GetArrayLength(Players) - 1].Nick := 'New Nick';
Players[GetArrayLength(Players) - 1].Active := True;
Players[GetArrayLength(Players) - 1].Loc := 'New Location';
Players[GetArrayLength(Players) - 1].Integer[0] := 0;
end;
And just do that for all the Players variables you have in your script.
The next Procedure is
procedure Inititate(Sender: TObject);
begin
lblUser.Caption := IntToStr(cUser);
Players[cUser].Nick := edNick.Text;
Players[cUser].Name := edUser.Text;
Players[cUser].Pass := edPass.Text;
Players[cUser].Loc := edLocation.Text;
Players[cUser].Integer[0] := (strtoint(edloads.Text));
if (cbActive.Text = 'True') then
Players[cUser].Active := True
else
Players[cUser].Active := False;
cUser := GetArrayLength(Players) - 1;
end;
This is where you would have saved time by naming your edit boxes accordingly. If not then you have to find out which of your editboxes are named what and input them in the place of edNick.Text or edUser.Text
if you have more such as an integer you would just add that as a variable like this
procedure Inititate(Sender: TObject);
begin
lblUser.Caption := IntToStr(cUser);
Players[cUser].Nick := edNick.Text;
Players[cUser].Name := edUser.Text;
Players[cUser].Pass := edPass.Text;
Players[cUser].Loc := edLocation.Text;
Players[cUser].Integer[0] := Strtoint(edLoads.Text);
if (cbActive.Text = 'True') then
Players[cUser].Active := True
else
Players[cUser].Active := False;
cUser := GetArrayLength(Players) - 1;
end;
I will be using Integer1 throughout this Tutorial and the edit box will be named edLoads. So if you have a string or a boolean then you can add it like that. The cbActive is a combobox. If you go back to the picture of my form you will see a combobox there. I find this the easiest way to use booleans but any other way would work.

Again all you have to do in this picture is put your players variables in and change the names of your form objects to what they are.
procedure CreateUser(Sender: TObject);
begin
Players[cUser].Nick := edNick.Text;
Players[cUser].Name := edUser.Text;
Players[cUser].Pass := edPass.Text;
Players[cUser].Loc := edLocation.Text;
Players[cUser].Integer[0] := Strtoint(edLoads.Text);
if (cbActive.Text = 'True') then
Players[cUser].Active := True
else
Players[cUser].Active := False;
AddUser;
cUser := GetArrayLength(Players) - 1;
lblUser.Caption := IntToStr(cUser);
edLocation.Text := Players[cUser].Loc;
edNick.Text := Players[cUser].Nick;
edUser.Text := Players[cUser].Name;
edPass.Text := Players[cUser].Pass;
Strtoint(edLoads.text) := Players[cUser].Integer[0];
if (Players[cUser].Active) then
cbActive.Text := 'True'
else
cbActive.Text := 'False';
end;
Same thing as you have been doing.
procedure ForwardUser(Sender: TObject);
begin
Players[cUser].Nick := edNick.Text;
Players[cUser].Name := edUser.Text;
Players[cUser].Pass := edPass.Text;
Players[cUser].Loc := edLocation.Text;
Players[cUser].Integer[0] := Strtoint(edLoads.Text);
if (cbActive.Text = 'True') then
Players[cUser].Active := True
else
Players[cUser].Active := False;
if (cUser + 1 > GetArrayLength(Players) - 1) then
cUser := 0
else
cUser := cUser + 1;
lblUser.Caption := IntToStr(cUser);
edLocation.Text := Players[cUser].Loc;
edNick.Text := Players[cUser].Nick;
edUser.Text := Players[cUser].Name;
edPass.Text := Players[cUser].Pass;
Strtoint(edLoads.text) := Players[cUser].Integer[0];
if (Players[cUser].Active) then
cbActive.Text := 'True'
else
cbActive.Text := 'False';
end;

Same thing once again.
procedure BackUser(Sender: TObject);
begin
Players[cUser].Nick := edNick.Text;
Players[cUser].Name := edUser.Text;
Players[cUser].Pass := edPass.Text;
Players[cUser].Loc := edLocation.Text;
Players[cUser].Integer[0] := Strtoint(edLoads.Text);
if (cbActive.Text = 'True') then
Players[cUser].Active := True
else
Players[cUser].Active := False;
if (cUser - 1 < 0) then
cUser := GetArrayLength(Players) - 1
else
cUser := cUser - 1;
lblUser.Caption := IntToStr(cUser);
edLocation.Text := Players[cUser].Loc;
edNick.Text := Players[cUser].Nick;
edUser.Text := Players[cUser].Name;
edPass.Text := Players[cUser].Pass;
Strtoint(edLoads.text) := Players[cUser].Integer[0];
if (Players[cUser].Active) then
cbActive.Text := 'True'
else
cbActive.Text := 'False';
end;
Almost there!
procedure DeleteUser(Sender: TObject);
var
lo, na, ni: string;
ac: Boolean;
in: integer;
begin
lo := Players[GetArrayLength(Players) - 1].Loc;
na := Players[GetArrayLength(Players) - 1].Name;
ni := Players[GetArrayLength(Players) - 1].Nick;
ac := Players[GetArrayLength(Players) - 1].Active;
in :=Players[GetArrayLength(Players) - 1].Integer[0];
Players[cUser].Loc := lo;
Players[cUser].Name := na;
Players[cUser].Nick := ni;
Players[cUser].Active := ac;
Players[cUser].Integer[0] := in;
SetArrayLength(Players, GetArrayLength(Players) - 1);
if (cUser + 1 > GetArrayLength(Players) - 1) then
cUser := 0
else
cUser := cUser + 1;
lblUser.Caption := IntToStr(cUser);
edLocation.Text := Players[cUser].Loc;
edNick.Text := Players[cUser].Nick;
edUser.Text := Players[cUser].Name;
edPass.Text := Players[cUser].Pass;
strtoint(edloads.text) := Players[cUser].Integer[0];
if (Players[cUser].Active) then
cbActive.Text := 'True'
else
cbActive.Text := 'False';
end;

Last one!!!
procedure Start(Sender: TObject);
begin
lblUser.Caption := IntToStr(cUser);
edLocation.Text := Players[cUser].Loc;
edNick.Text := Players[cUser].Nick;
edUser.Text := Players[cUser].Name;
edPass.Text := Players[cUser].Pass;
strtoint(edLoads.text) := Players[cUser].integer[0];
if (Players[cUser].Active) then
cbActive.Text := 'True'
else
cbActive.Text := 'False';
end;

Ok when you are finally done adapting all of those procedures to fit your script you can move on.

5. Your buttons. Remember the buttons (<=,Add User,Delete User,=>) well now is when you have to use these. Scroll down to your initform procedure and find your left arrow button (<-,<=).
When you get to it everything should be there except for
 btnBack := TButton.Create(gbControl);
btnBack.Parent := gbControl;
btnBack.Left := 12;
btnBack.Top := 22;
btnBack.Width := 45;
btnBack.Height := 25;
btnBack.Caption := '<-';
btnBack.TabOrder := 0;
btnBack.OnClick := @BackUser;//this

add that with the name of your button insted of btnback.
then with your (->) arrow
btnNext := TButton.Create(gbControl);
btnNext.Parent := gbControl;
btnNext.Left := 216;
btnNext.Top := 23;
btnNext.Width := 45;
btnNext.Height := 25;
btnNext.Caption := '->';
btnNext.TabOrder := 1;
btnNext.OnClick := @ForwardUser;// add this
again change the btnNext to whatever you named your button.
Now with Create User
  btnCreate := TButton.Create(gbControl);
btnCreate.Parent := gbControl;
btnCreate.Left := 138;
btnCreate.Top := 22;
btnCreate.Width := 75;
btnCreate.Height := 25;
btnCreate.Caption := 'Create';
btnCreate.TabOrder := 2;
btnCreate.OnClick := @CreateUser;//add
and again with delete user
  btnDelete := TButton.Create(gbControl);
btnDelete.Parent := gbControl;
btnDelete.Left := 59;
btnDelete.Top := 22;
btnDelete.Width := 75;
btnDelete.Height := 25;
btnDelete.Caption := 'Delete';
btnDelete.OnClick := @DeleteUser;//add
btnDelete.TabOrder := 3;
Now one last thing go to your frmdesign vars at the beginning of your initform procedure and add this.
frmDesign := CreateForm;
frmDesign.Left := 254;
frmDesign.Top := 107;
frmDesign.Width := 308;
frmDesign.Height := 316;
frmDesign.Caption := 'User Manager';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
frmDesign.OnShow := @Start;//add this

Now you are done with the button assignments.
6.Almost the last thing to do is add cUser as a Integer at the beginning and add this to your main loop
LoadUs;
cUser := 0;
if (GetArrayLength(Players) = 0) then
AddUser;
Safeinitform;
SafeShowFormModal;
Saveus;

Now you should be done.

Ashur2Good

2007-07-14 00:30:50

W00T, Thank you!!! I'm nearly done with my form! Now all i gotta do is add the other vars :) Thank you!:spot: :spot: :spot:


-Ashur:stirthepot:

jhildy

2007-07-14 00:31:25

Your welcome glad i could help.

Santa_Clause

2007-07-14 01:03:55

Wow. Very nice tutorial Jhildy. I've been asked many times before about this and I'm glad someone explains it.

jhildy

2007-07-14 01:16:52

thanks for the feedback yea iv seen people ask this many times aswell

Santa_Clause

2007-07-14 01:36:28

…I do know how to do it. I meant that many people have asked me how to do it before :p

BobboHobbo

2007-07-14 04:38:23

Aww i always needed this TUT

Thanks for posting this, when i come back to SCAR ill use it :)

jhildy

2007-07-14 10:55:40

From: SantaClause
…I do know how to do it. I meant that many people have asked me how to do it before :p


yes i edited my post i misunderstood.

Santa_Clause

2007-07-14 11:00:14

:p

How'd you learns forms?

jhildy

2007-07-14 11:07:44

idk just playing around with them. There my favorite thing to do lol.

n3ss3s

2007-07-14 11:24:47

Very good tut.

jhildy

2007-07-14 11:43:38

Thanks a lot everyone.

Lalaji

2007-07-14 11:57:15

great tut!

itSchRis917

2007-07-15 01:07:57

I need some help, i got an error, here's my thread:
http://www.villavu.com/forum/showthread.php?t=13573

Lalaji

2007-07-17 18:07:59

How do you creat the white boxes for username and pass?

Is that Lable or edit?

Nice tut by the way (again)

Widget

2007-07-17 18:26:08

Great Tut!

From: lalaji
How do you creat the white boxes for username and pass?

Is that Lable or edit?

Nice tut by the way (again)


TEdits :)

Lalaji

2007-07-18 12:25:43

I still cant do this, getting some errors…:(

Ill try once again.

EDIT :: 1 more question, the last part, the main loop thing, "loadUs' and "SaveUs" are your procedures right? we dont have to add them, Right?

jhildy

2007-07-18 17:40:55

you dont have to add them but they save your player array and load it every time the script is started and finished. they are not required however.

Lalaji

2007-07-18 19:58:08

yeah i know.. i have a better chance for srl member if i put the thought…;)

Ok, my script cant identify "players" in any of RSN's procedures. What do i do?

jhildy

2007-07-19 15:09:55

lol thats the player array are you sure your not including srl or something.

Lalaji

2007-07-19 15:13:25

:O I did actually forget…See i was making a seprate script to test it on first.
Thanks once again

EDIT:: YES!! finally got it!!! and it only took me 3 days :)

Garrett

2007-07-19 15:55:59

Thanks for this tutorial. Got myself some multiplayer forms now.

jhildy

2007-07-19 20:48:02

lalaji good job finally lol.

Santa_Clause

2007-07-20 05:58:07

Ahh…now you see how popular this tutorial is…;)

Nitro

2007-07-26 13:27:12

Thank you Jhildy! This tutorial basically saved my life. I was trying to find out how to add variables to my PlayerArrays

For example I thought this piece of code was right but I wasn't quite sure so I decided to check it up.

     Players[0].Name :='Username';
Players[0].Pass :='Password';
Players[0].Nick :='sern';
Players[0].Active := True;
Pkayers[0].Boolean1 := (ExampleVariable1)
Players[0].String1 := (ExampleVariable2)

However I think after reading this it should now be this.

     Players[0].Name := 'Username';
Players[0].Pass := 'Password';
Players[0].Nick := 'sern';
Players[0].Active := True;
Pkayers[0].Boolean1 := ExampleVariable1;
Players[0].String1 := ExampleVariable2;

However, if I have made another mistake please correct me ;)

-Nitro

dvdcrayola

2007-08-15 01:22:58

this is a great tut.. but should my declareplayers just be this?

Procedure DeclarePlayers;
begin
HowManyPlayers := 1;
NumberOfPlayers(HowManyPlayers);
CurrentPlayer := 0;

Players[0].Name :='';
Players[0].Pass :='';
Players[0].Nick :='';
Players[0].Integer1 := 3;
Players[0].Loc :='Loc1';
Players[0].Active := False;
end;

jhildy

2007-08-15 01:58:05

no need for declare players at all just delete it.

dvdcrayola

2007-08-15 02:11:27

oh ok.

thanks again for all your help.

rotflmfwao

2007-08-19 21:25:29

Sorry for grave digging if I did, but could you update this for 4.0 please Jhildy? Just when you get the chance.

jhildy

2007-08-20 10:14:08

sure lol i didn't even think of that.

rotflmfwao

2007-08-20 11:54:05

Thanks, I read the tut, did the stuff, and realised that there was no LoadUs or SaveUs procedure in UsersManager XD Then I updated my players arrays for 4.0 but the real problem was the LoadUs SaveUs stuff.
EDIT: After a long time of searching SRL (5min) I found SaveSettings and LoadSettings in the PlayerForm.scar file in Misc… :duh:

There's stuff like HowManyBooleans and LoadPlayerArray, and I have no clue where to put that.

Also, you have it as
Players[cUser].Integer[0]
When it should be
Players[cUser].Integers[0]

jhildy

2007-08-20 20:53:58

oh you don't even need those procedures because they just save the players they are not completely necisary.

ShowerThoughts

2007-08-21 04:27:49

is it working for srl 4.0 beta /offtopic get on msn/

rotflmfwao

2007-08-21 14:45:23

@Jhildy:
Oh, ok, that helps a lot. I thought they were necessary because they freed up a lot of memory :p

Negaal

2007-09-29 08:09:51

thank you for this, i didnt knew how to do those:D

Naum

2007-12-29 15:31:16

Good tut jhildy!

Pure1993

2008-03-03 07:40:06

great tut, I didn't even know that UsersManager.Scar existed. O.o
But I have a question (sorry if this sound noobish, but I just started with forms, want to add them to my guild miner…): I get the error message unkown Identifier LoadUs (it also doesn't find SaveUs). I would like to know if anything changed which I need to add or get rid of…
Thanks a lot,
Stur Pure

EDIT: well, dunno exactly why and how, but it doesn't seem to need it anymore. works like a charm the way it does. AWSOME TUT! :)

jhildy

2008-03-03 22:14:00

you dont need those and they dont exist any more.

Pure1993

2008-03-04 12:21:10

When I use the Integer[0] example, I always get the error: Unknown identifier 'INTEGER' in script", do I need to define it anywhere, and if so, how and where (I tried defining it as "array of Integer", just for fun, and to see if it worked for ssome weird reason, but it didn't…

ShowerThoughts

2008-03-04 12:27:58

12-29-2007, 09:31 PM

-_-
with old threads like those it's not forbidden to ask somthing just pm the thread starter or the person where you need the info from.

Pure1993

2008-03-04 13:11:14

Well, yeah, I guess, but posting it here is one way I guess (won't kill anyone lol). and heck, I got an answer to the first question, so why not to the second one too? :D

Zezi

2008-04-01 18:59:03

I'm VERY sorry if i'm gravedigging, but does this still work? As loadus doesn't actually exist anymore, i haven't started far into the tutorial, but i would just like to know before i continue doing this :P

Tyvm, hoping for a somewhat fast reply :)