Author Topic: array problems: ArrayHigh, GetArrayLength  (Read 977 times)

0 Members and 1 Guest are viewing this topic.

Offline yexxle

  • Major(1)
  • Posts: 22
array problems: ArrayHigh, GetArrayLength
« on: June 19, 2007, 04:14:20 am »
Ok, this is beyond frustrating. I've done a clean install of the server to make sure it's not something I've done in my more complex code. I've disabled BattleEye just in case the recent update affected anything. Nothing has fixed this problem. Function calls to get the upper bound of an array are failing. So, I decided to see what works and what doesn't. The following code samples are written on a clean server install on Windows XP.

Working (or at least properly-written) code example, with array lengths defined during variable definition.

Code: [Select]
type
    Player = record
        username: string;
        score: integer;
    end;

var
    sample_str: array[0..9] of string;
    sample_int: array[0..9] of integer;
    sample_bool: array[0..9] of boolean;
    sample_rec: array[0..9] of Player;
    loop: integer;

procedure ActivateServer();
    begin
        { works }
        for loop := 0 to ArrayHigh(sample_str) do
            sample_str[loop] := 'test';

        { fails with run-time error "Could not call proc" }
        for loop := 0 to GetArrayLength(sample_str) - 1 do
            sample_str[loop] := 'test';


        { fails with compile-time error "Type mismatch" }
        for loop := 0 to ArrayHigh(sample_int) do
            sample_int[loop] := 1;

        { fails with run-time error "Could not call proc" }
        for loop := 0 to GetArrayLength(sample_int) - 1 do
            sample_int[loop] := 1;


        { fails with compile-time error "Type mismatch" }
        for loop := 0 to ArrayHigh(sample_bool) do
            sample_bool[loop] := true;

        { fails with run-time error "Could not call proc" }
        for loop := 0 to GetArrayLength(sample_bool) - 1 do
            sample_bool[loop] := true;


        { fails with compile-time error "Type mismatch" }
        for loop := 0 to ArrayHigh(sample_rec) do begin
            sample_rec[loop].username := 'test';
            sample_rec[loop].score := 0;
        end;

        { fails with run-time error "Could not call proc" }
        for loop := 0 to GetArrayLength(sample_rec) - 1 do begin
            sample_rec[loop].username := 'test';
            sample_rec[loop].score := 0;
        end;
    end;


Working (or at least properly-written) code example, with array lengths set after variable definition.

Code: [Select]
type
    Player = record
        username: string;
        score: integer;
    end;

var
    sample_str: array of string;
    sample_int: array of integer;
    sample_bool: array of boolean;
    sample_rec: array of Player;
    loop: integer;

procedure ActivateServer();
    begin
        SetArrayLength(sample_str, 10);

        { works }
        for loop := 0 to ArrayHigh(sample_str) do
            sample_str[loop] := 'test';

        { works }
        for loop := 0 to GetArrayLength(sample_str) - 1 do
            sample_str[loop] := 'test';


        SetArrayLength(sample_int, 10);

        { fails with compile-time error "Type mismatch" }
        for loop := 0 to ArrayHigh(sample_int) do
            sample_int[loop] := 1;

        { works }
        for loop := 0 to GetArrayLength(sample_int) - 1 do
            sample_int[loop] := 1;


        SetArrayLength(sample_bool, 10);

        { fails with compile-time error "Type mismatch" }
        for loop := 0 to ArrayHigh(sample_bool) do
            sample_bool[loop] := true;

        { works }
        for loop := 0 to GetArrayLength(sample_bool) - 1 do
            sample_bool[loop] := true;


        SetArrayLength(sample_rec, 10);

        { fails with run-time error "Type mismatch" }
        for loop := 0 to ArrayHigh(sample_rec) do begin
            sample_rec[loop].username := 'test';
            sample_rec[loop].score := 0;
        end;

        { works }
        for loop := 0 to GetArrayLength(sample_rec) - 1 do begin
            sample_rec[loop].username := 'test';
            sample_rec[loop].score := 0;
        end;
    end;

Summary:
  • ArrayHigh only works on arrays of strings (whether the array length is defined during or set after variable definition). ArrayHigh does not work on arrays of integers, booleans, or records.
  • GetArrayLength does not work on arrays whose dimensions are given during variable definition.

Currently, the ONLY WAY to "properly" manage your arrays with Soldat scripting is to create dynamic-length arrays (no using [1..10] notation when defining your arrays), use SetArrayLength to change the dimensions, and GetArrayLength to fetch the length back. The only exception is you can use ArrayHigh() on an array of strings (and only strings). I can only guess that GetArrayLength is coded to simply spit back the number given with SetArrayLength. So if you provide the dimensions upon variable definition (thereby not calling SetArrayLength), GetArrayLength doesn't know what to give you.

I don't want to sound like a jerk, but seriously. If you guys can't even get something as simple and fundamental to the language as arrays straight, I don't see how this project is going to get very far. Beta or not, this is simply wrong. Some people who want to add scripting to Soldat are going to know a thing or two about programming, and be entirely turned off by the fact that not only have you restricted access to most of the Pascal language, but you have dropped in replacements that don't work.

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: array problems: ArrayHigh, GetArrayLength
« Reply #1 on: June 19, 2007, 06:36:23 am »
ArrayHigh is fixed in the next version of the server.
« Last Edit: June 19, 2007, 06:38:59 am by chrisgbk »

Offline yexxle

  • Major(1)
  • Posts: 22
Re: array problems: ArrayHigh, GetArrayLength
« Reply #2 on: June 19, 2007, 06:42:24 am »
Is it fixed to work with arrays defined with indices, or is it too dependant on SetArrayLength?

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: array problems: ArrayHigh, GetArrayLength
« Reply #3 on: June 19, 2007, 06:54:49 am »
It works exactly as it does with arrays of strings; it was declared wrong is all.

Offline yexxle

  • Major(1)
  • Posts: 22
Re: array problems: ArrayHigh, GetArrayLength
« Reply #4 on: June 19, 2007, 06:56:47 am »
Oh yes yes yes. Goody. When's the next release? Haha, kidding. I can wait, knowing it's in there. :)