Author Topic: A quick question  (Read 1940 times)

0 Members and 1 Guest are viewing this topic.

Offline thegrandmaster

  • Flagrunner
  • ****
  • Posts: 543
    • Grandy on Steam
A quick question
« on: December 26, 2010, 09:35:25 am »
I'm a complete newbie with scripting in general, so please be understanding xD

Code: (pascal) [Select]
//First part of a Player Count script. Used for a lobby of some description.
//The Values where comments have the * can be changed to suit the needs of the Admin
Const
Full = 6; //*Needed Players to begin*

var
Gram1, Gram2, Gram3 : string; //There are/There is/player/players
RP, CP, OL, NP : byte; //Remaining Players, Current Players, One player left to join, Needed Players

begin
NP := 6; //*Needed Players to begin*
OL := NP - 1; //One less than full
CP := NumPlayers; //This is the bit I'm confused about...

if (CP = 1) then begin //Grammar rules for below procedure
Gram1 := 'is ';
Gram2 := '';
Gram3 := 's ';
end;
else if (CP> 1 < OL or CP = NP) then begin
Gram1 := 'are ';
Gram2 := 's';
Gram3 := 's';
end;
else if (CP = OL) then begin
Gram1 := 'are ';
Gram2 := 's';
Gram3 := '';
end;
end.

procedure OnJoinGame(ID, Team: byte);
begin
WriteConsole(ID, 'This server requires ' + NP + 'players to begin', $FFFFFF00);

if (CP<> NP) then begin
RP := NP - CP;
WriteConsole(0, 'There ' Gram1 + CP + 'player' + Gram2 + ' in the server. Waiting for ' + RP + ' player' + Gram3 + ' to join', $FFFFFF00);
Full := False;
end;
else
Full := True;
WriteConsole(0, 'The server is full', $FFFFFF00);
end;

My thought pattern says that I need to get the number of players in the server, assign it to something else to then use it (in this script, to get appropriate grammar for the sentance that says how many remaining).
I realise that this is used within functions/procedures, but I'm confused as to how to get NumPlayers as a value I can manipulate and do calculations with etc.

Thanks for your time.

From: December 26, 2010, 09:43:27 am
I realise that I could just add or remove an 's' or output 'are'/'is' rather than doing the whole words like 'there are' and 'players'... I'll change that...
EDIT: Implemented
« Last Edit: December 26, 2010, 09:51:31 am by thegrandmaster »
ohgodwhydidInamemyselfthis. Just call me Grandy.

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: A quick question
« Reply #1 on: December 26, 2010, 09:52:20 am »
Code: (pascal) [Select]
const FULL = 6;

procedure OnJoinGame(ID, Team: byte);
var diff: smallint; str: string;
begin 
    WriteConsole(ID, 'This server requires ' + IntToStr(FULL)+ ' players to begin', $FFFFFF00); 
    diff := FULL - NumPlayers;
    if diff > 0 then begin
if NumPlayers = 1 then begin
str := 'There is 1 player';
end else
str := 'There are '+IntToStr(NumPlayers)+' players';
str := str + ' in the server, waiting for ' + IntToStr(diff) + ' player' + iif(diff > 1, 's', '') + ' to join';

WriteConsole(0, str, $FFFFFF00); 
    end else 
        WriteConsole(0, 'The server is full', $FFFFFF00); 
end;

Untested

Offline thegrandmaster

  • Flagrunner
  • ****
  • Posts: 543
    • Grandy on Steam
Re: A quick question
« Reply #2 on: December 26, 2010, 09:55:02 am »
Although it's nice of you to do that all for me, it doesn't entirely help me with my question..
ohgodwhydidInamemyselfthis. Just call me Grandy.

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: A quick question
« Reply #3 on: December 26, 2010, 10:03:28 am »
This code is answer for all your questions, showing you how to get the number of players, do calculations on it and how to "get appropriate grammar"

Offline thegrandmaster

  • Flagrunner
  • ****
  • Posts: 543
    • Grandy on Steam
Re: A quick question
« Reply #4 on: December 26, 2010, 10:09:32 am »
I suppose so... you've used several concepts I didn't even realise you could do. Thanks, I'll take a close look.
ohgodwhydidInamemyselfthis. Just call me Grandy.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: A quick question
« Reply #5 on: December 26, 2010, 03:58:02 pm »
One thing I would like to make clear, Soldat scripting is not usual pascal/dephi scripting; actions that you want to be done must be inside a function/procedure or an event (which really is just a function/procedure that the scripting core calls). If you want something to be done right away when the script first starts, use the ActivateServer event.

I like to think there is nothing wrong with long variable names that are descriptive (except that it may be tedious to write over and over), but others may disagree.

NumPlayers is a built-in variable that is automatically set by the scripting core.

Variables in programming and scripting are only modified when you tell them to be what you want them to be. For example, if you do MyVar := NumPlayers; at one point in time, MyVar may not necessarily be equal to NumPlayers at a different moment in time (after NumPlayers is modified by the scripting core).
Another example:
Code: [Select]
B := 1; // A = ? (0 default), B = 1
A := B; // A = 1, B = 1
B := 2; // A = 1, B = 2
(there is one exception I can think of relating to the parameters of a function that I am not going to go into that you probably will not run into [yet/soon])

In regards to if statements:
if condition then line else line;
if condition then begin block end else begin block end;
if condition then begin block end else line;
if condition then line else begin block end;

where line can be something like this:
CallFunc()
or
MyVar := 3
and block is a series of lines, each followed by semi-colons, something like this:
CallFunc();
MyVar := 3;


Refer to tk's response, and try to understand what & why he has done what he has done. Please share any further questions :)
« Last Edit: December 26, 2010, 04:09:16 pm by DorkeyDear »

Offline thegrandmaster

  • Flagrunner
  • ****
  • Posts: 543
    • Grandy on Steam
Re: A quick question
« Reply #6 on: December 26, 2010, 04:55:57 pm »
Thanks man :D I'm sure this'll come in handy. And sure, if I get any other problems I'll post them.

From: December 27, 2010, 05:44:59 am
In regards to if statements:
if condition then line else line;
if condition then begin block end else begin block end;
if condition then begin block end else line;
if condition then line else begin block end;


So... what's wrong with this in that case:
Code: (pascal) [Select]
procedure OnPlayerSpeak(ID: Byte; Text: string);
begin
if(Text = '/test') then begin
if (GetPlayerStat(ID, Team) = 1) then
WriteConsole(ID, 'You are in the Alpha Team', $FFFF0000);
else if (GetPlayerStat(ID, Team) = 2) then
WriteConsole(ID, 'You are in the Bravo Team', $FFFF0000);
else
WriteConsole(ID, 'You are in neither in Alpha or Bravo Team', $FFFF0000);
end;
end;

The compiler reckons there should be a ";" instead of "ELSE" on line 6?
« Last Edit: December 27, 2010, 05:44:59 am by thegrandmaster »
ohgodwhydidInamemyselfthis. Just call me Grandy.

Offline Mighty

  • Camper
  • ***
  • Posts: 276
Re: A quick question
« Reply #7 on: December 27, 2010, 08:22:32 am »
The compiler reckons there should be a ";" instead of "ELSE" on line 6?

if condition then line;

but

if condition then line else line;

";" tells that it's the end of whole IF
xFire: macmil        e-mail: macekmil@gmail.com
My scripts: Accuracy Script       Flashbang       Punishments GUID
            CatchMe Gamemod       AntiFake
            CW System             AntiFakeGUID