Author Topic: Script Improvements (Variables)  (Read 2621 times)

0 Members and 1 Guest are viewing this topic.

Offline ExHunter

  • Inactive Soldat Developer
  • Soldier
  • ******
  • Posts: 154
  • Speedy go!
Script Improvements (Variables)
« on: June 23, 2011, 09:53:52 am »
Well I want to explain some Variables that I use already in my Clan script and my Hold the Base RPG script.

They help to improve the Server stability.

1. The Code:

Code: [Select]
var
  HighID: byte;
  InGame: Array[1..32] of boolean;
  PlayerTeam: Array[1..32] of byte;

procedure ActivateServer();
var i: byte;
begin
  for i := 1 to 32 do if GetPlayerStat(i,'Active') = true then begin
    InGame[i] := true;
    HighID := i;
    PlayerTeam[i] := GetPlayerStat(i, 'Team');
  end else InGame[i] := false;
end;

procedure OnJoinGame(ID, Team: byte);
begin
  InGame[ID] := true;
  PlayerTeam[ID] := Team;
  if ID > HighID then HighID := ID;
end;

procedure OnLeaveGame(ID, Team: byte; Kicked: boolean);
var
  i: byte;
begin
  InGame[ID] := false;
  if HighID = ID then for i := 1 to HighID-1 do if InGame[ID] then HighID := i;
end;

procedure OnJoinTeam(ID, Team: byte);
begin
  PlayerTeam[ID] := Team;
end;

2. Why should I use this?
- It reduces the Chance of a Server crash and reduces also the CPU usage. It lets run your Server all in all run more efficient. Example:
Imagine you have a code where its like for i := 1 to 32 do begin '........' - but there are only 2 players in the game. So the loop should only repeat 2 times - not 32 times. Thats the variable HighID for.
Every Scripter should already read something about "GetPlayerStat". It's really.. crap. How you can see do I need it only if the Server starts (or gets recompiled). Then you could replace GetPlayerStat(ID,'Active') with InGame[ID]. It will help - really. This you could also do with other variables of GetPlayerStat.

Hope this Thread will be useful for some scripters.

ExHunter

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Script Improvements (Variables)
« Reply #1 on: June 23, 2011, 11:18:38 am »
If you are worried about efficiency, this could be changed slightly from
if HighID = ID then for i := 1 to HighID-1 do if InGame[ID] then HighID := i;
to something like
Code: [Select]
if HighID = ID then begin
  HighID := 0;
  for i := HighID-1 downto 1 do
    if InGame[ID] then begin
      HighID := i;
      break;
    end;
end;
but i am in no means an expert on efficiency or stability of a Soldat server.


Offline ExHunter

  • Inactive Soldat Developer
  • Soldier
  • ******
  • Posts: 154
  • Speedy go!
Re: Script Improvements (Variables)
« Reply #2 on: June 23, 2011, 11:30:14 am »
oh well. thx.

didnt know that there is something like downto.

"HighID := 0;" <-- isnt neccessary then ^^ (and would bug it ^^)

Edit:

An short statement: Well I'm not an expert too but i can tell, that I've done my expieriences with such things. I own a Server with really much scripts, and its the question about how efficient u can you them. Before I thought about "how i can improve the stability" my server crashed at least once per day. Now it's less than once per 3 days. Which is really "stable" (in my opinion) for my server. But still i can say: I can improve it much more.
« Last Edit: June 23, 2011, 11:34:53 am by ExHunter »

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: Script Improvements (Variables)
« Reply #3 on: June 23, 2011, 04:19:59 pm »
This way you can also set Alive values (OnPlayerRespawn and OnPlayerKill), Team value (OnJoinGame, OnJoinTeam), weapons (OnWeaponChange, not reliable though), IsHuman (bots doesn't call OnJoinTeam), Kills, Deaths (both OnPlayerKill) and Flagger (OnFlagGrab, OnPlayerKill, OnFlagScore, not completly reliable though as there's no OnFlagDrop/Throw). Also, perhaps rename InGame to Active (to make it "compatible" with GetPlayerStats).

Oh, and also mind to use array of type instead of set of individual arrays. It's more clear then, some also says that arrays makes your script unstable.
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Script Improvements (Variables)
« Reply #4 on: June 23, 2011, 05:11:04 pm »
some also says that arrays makes your script unstable.
just dynamic arrays

Offline Mighty

  • Camper
  • ***
  • Posts: 276
Re: Script Improvements (Variables)
« Reply #5 on: June 24, 2011, 07:32:38 am »
Ive been working with same team saving, but had few problems when i was adding spec-on-join. (wrong number was saved in array) Had to make additional trigger. Try it out and see if it still works properly.
xFire: macmil        e-mail: macekmil@gmail.com
My scripts: Accuracy Script       Flashbang       Punishments GUID
            CatchMe Gamemod       AntiFake
            CW System             AntiFakeGUID