Author Topic: Arrays  (Read 2048 times)

0 Members and 1 Guest are viewing this topic.

DarkCrusade

  • Guest
Arrays
« on: May 21, 2010, 08:27:24 am »
I'm working on a ZS right now and try to reduce the amount of copy pasting. It'd be a lot easier if I was able to use something like

Code: (pascal) [Select]
var
  S1Price: Array[1..5] of Integer;

Procedure ActivateServer();
  begin
    S1Price := [1000,2000,3000,4000,5000];
  end;

It compiles but returns me a strange error:
Code: [Select]
10-05-21 15:16:17  [*] [Error] ZombieSurvival 0.01 -> (ActivateServer): Could not call proc

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Arrays
« Reply #1 on: May 21, 2010, 08:32:58 am »
You can only assign dynamic arrays in such way in scriptcore (afaik)

and btw, index * 1000 is a good enough way to get the value you want.
« Last Edit: May 21, 2010, 08:36:00 am by tk »

DarkCrusade

  • Guest
Re: Arrays
« Reply #2 on: May 21, 2010, 09:01:32 am »
But then I've got the problem that 'SetLenght(Array,Indexlenght)' doesn't work in Soldat..

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Arrays
« Reply #3 on: May 21, 2010, 09:06:46 am »
to hide some long code I sometimes do things like:
Code: (pascal) [Select]
procedure InitializePrices();
begin
  S1Price[1] := 1000;
  S1Price[2] := 2000;
  S1Price[3] := 3000;
  S1Price[4] := 4000;
  S1Price[5] := 5000;
end;
and then I/you never have to see it again / its now out of your way in ActivateServer or wherever it may be

But no, as tk` said, I think that only works for dynamic array. You could (if you REALLY wanted) make a function that does it for you:
Code: (pascal) [Select]
procedure SetPrices(const Prices: array of integer);
var
  i: byte;
begin
  for i := 0 to 4 do
    S1Price[i + 1] := Prices[i];
end;

// your code
begin
  SetPrices([1000, 2000, 3000, 4000, 5000]);
end;
But I don't really advise that.

(and I haven't actually tested either piece of code, but you get the idea)


From: May 21, 2010, 09:07:25 am
But then I've got the problem that 'SetLenght(Array,Indexlenght)' doesn't work in Soldat..
SetLength is for strings
SetArrayLength is for dynamic arrays
« Last Edit: May 21, 2010, 09:08:55 am by DorkeyDear »

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Arrays
« Reply #4 on: May 21, 2010, 09:17:18 am »
curt...

Code: (pascal) [Select]
for i := 1 to 5 do S1Price[i] := i * 1000;
DC: dont use dynamic arrays

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Arrays
« Reply #5 on: May 21, 2010, 09:23:21 am »
I was under the assumption that the prices aren't necessarily intervals of 1000, and was just an example; but I could be wrong. But yeah what you provided works if that isn't true.

Static / constant-length arrays are your friend if you can use them. :D

DarkCrusade

  • Guest
Re: Arrays
« Reply #6 on: May 21, 2010, 01:16:45 pm »
Thank you all :)

Offline LORD KILLA

  • Camper
  • ***
  • Posts: 254
  • Happie
Re: Arrays
« Reply #7 on: May 21, 2010, 02:10:31 pm »
what about hiding arrays in strings? You'd use delimeters then...
You'd have just declared a string, and some modifying functions:
Code: [Select]
foo|bar|lol!

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Arrays
« Reply #8 on: May 21, 2010, 03:19:08 pm »
Couldn't you make up anything more inefficient?

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Arrays
« Reply #9 on: May 22, 2010, 01:20:08 am »
Couldn't you make up anything more inefficient?
storing arrays in consts

DarkCrusade

  • Guest
Re: Arrays
« Reply #10 on: May 22, 2010, 02:54:47 am »
Right now I'm optimizing MountainClimbing to the limit and try to figure how I am able to split the Mapslist.txt into an array. Counts a line break as a space (' ')? If that's so the whole thing would be a lot easier.

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Arrays
« Reply #11 on: May 22, 2010, 03:04:44 am »
const br = #13#10;

// ...
var ml: string; arr: array of string;
// ...
ml := ReadFile('Mapslist.txt');
if ml <> nil then begin
  arr := Explode(ml, br);
 // ...

http://soldatcentral.com/index.php?page=script&f=129

DarkCrusade

  • Guest
Re: Arrays
« Reply #12 on: May 22, 2010, 08:12:03 am »
I removed the hardcoded maps from MountainClimbing. Now the script uses the maplist.txt. But OnFlagScore returns an error when I score, the error lies in line 485 but I cannot seem to find the problem.

Code: [Select]
10-05-22 15:02:33 /S2\|SuxX| DarkCrusade captured the Blue Flag
10-05-22 15:02:43 /S2\|SuxX| DarkCrusade scores for Alpha Team
10-05-22 15:02:43  [*] [Error] Mountain 1.65c -> (OnFlagScore): '9999
' is not a valid integer value


Offline Gizd

  • Flagrunner
  • ****
  • Posts: 586
  • (Re)tired
    • Eat-this! community site
Re: Arrays
« Reply #13 on: May 22, 2010, 09:09:53 am »
Is it so hard to look for StrToInt()'s in your own code?

edit: And to use
Code: [Select]
try
 stuff
except
 writeln('omgerror');
end;
« Last Edit: May 22, 2010, 09:12:52 am by Gizd »

DarkCrusade

  • Guest
Re: Arrays
« Reply #14 on: May 22, 2010, 09:47:48 am »
The problem was that ReadFile always returns more than the file contains. Fixed now and it's working, thanks guys