Home   Help Search Login Register  

Author Topic: Function faults  (Read 1186 times)

0 Members and 1 Guest are viewing this topic.

Offline Sparticus76

  • Members
  • *
Function faults
« on: 06 Sep 2008, 03:39:33 »
Hi, haveing problems with this code:

Code: [Select]
Private ["_targets","_Leader","_numtargets","_i","_targets_select","_enemy_variable_list"];

_Leader = _this;
_targets = _Leader Neartargets 500;
_numtargets = count _targets;
if (_numtargets > 0) then
{
for [{_i=0}, {_i<_numtargets}, {_i=_i+1}] do
      {
            _targets_select = _targets select _i;
            if (east in [_targets_select select 2]) then
                     {
                     _enemy_variable_list = [_enemy_variable_list] + [_targets select _i];
         };
      };
};
else
{
_enemy_variable_list = [];
};
_enemy_variable_list



this is the given fault, the damn ;

Quote
Error in expression <[_targets select _i];
};
};
};
else
{
_enemy_variable_list = [];
};
_enemy_v>
  Error position: <{
_enemy_variable_list = [];
};
_enemy_v>
  Error Missing ;

Can anyone help me with what's going on? Looks like all colons are in place to me  ???

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Function faults
« Reply #1 on: 06 Sep 2008, 04:13:56 »
Well, here is a significantly simplified version which should, nevertheless, show you where the errors with your original script were:
Code: [Select]
Private ["_targets","_Leader","_enemy_variable_list"];

_Leader = _this;
_targets = _Leader Neartargets 500;
_enemy_variable_list = []; // Need to initialise an array before you add to it.

{
    if ((_x select 2) == east) then // Use == rather than in for this.
    {
        _enemy_variable_list = _enemy_variable_list + [_x]; // Don't need [] around the array you want to add an element to.
    };
} forEach _targets; // When you are just iterating through an array of objects, forEach is usually simpler to use than for (this was not an error before; just makes things simpler this way).

_enemy_variable_list
« Last Edit: 06 Sep 2008, 04:18:42 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Sparticus76

  • Members
  • *
Re: Function faults
« Reply #2 on: 06 Sep 2008, 07:00:22 »
It worked a treat spooner! As you can see I'm an amature when it comes to this scripting stuff, thanks a million. Also I was still getting an error and changed my call in the editor from [this] call "script.sqf" to this call "script.sqf" and fixed this further problem. Thanks again.

Edit : Oops, they are actually .sqf files, typo. I actually didnt know that it doesnt matter what extension you have on the call.
« Last Edit: 07 Sep 2008, 11:05:25 by Sparticus76 »

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Function faults
« Reply #3 on: 06 Sep 2008, 15:58:47 »
The style of code you are using is SQF, so you should call it "script.sqf", not "script.sqs". ArmA doesn't care about the filename, since it runs scripts assuming a syntax based on what command you use to run them, but it is clearer for the reader if you follow the convention of using the correct extension.

Yes, it is usual to call with:
Code: [Select]
["fish", 27] call _function;
and then "accept" that at the top of the function with:
Code: [Select]
_typeOfAnimal = _this select 0;
_numberOfAnimals = _this select 1;
This format makes sense regardless of the number of parameters you want to pass through.

Some people prefer to use a single-parameter format when they only want to send one parameter (I prefer to stay consistent and always use the above style, regardless of the number of parameters):
Code: [Select]
44 call _function;
and then "accept" that at the top of the function with:
Code: [Select]
_angelsOnTheHeadOfAPin = _this;

EDIT: Split the thread, since original author asked a non-related question.
« Last Edit: 08 Sep 2008, 12:39:33 by Spooner »
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)