Home   Help Search Login Register  

Author Topic: Indexof.sqf problem  (Read 817 times)

0 Members and 1 Guest are viewing this topic.

Offline Blanco

  • Former Staff
  • ****
Indexof.sqf problem
« on: 29 Jul 2005, 06:21:30 »
GB has a nice function that returns the index of where an item is in an array. If the item is not in the array, returns -1.

Now I have one big global array named MAGZ_ARRAY  but the function doesn't work with that array.
First it was an array in array, so I thought that was the problem but after I've changed that it still won't work.

Code: [Select]
_index = [player, MAGZ_ARRAY] call indexOf
hint format ["%1",_index]

I'm sure the player is in that array, but it creates an error... it's too long to read.

I tried :

Code: [Select]
_myarray = format ["%1",MAGZ_ARRAY]
But that returns a string and I need an array  :'(  
What the hell, MAGZ_ARRAY is an array!








« Last Edit: 29 Jul 2005, 06:28:25 by Blanco »
Search or search or search before you ask.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Indexof.sqf problem
« Reply #1 on: 29 Jul 2005, 13:20:51 »
How do you 'fill' the MAGZ_ARRAY?
Do you initialize it before adding anything in it with MAGZ_ARRAY = []?
:(
« Last Edit: 29 Jul 2005, 13:21:11 by HateR_Kint »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Blanco

  • Former Staff
  • ****
Re:Indexof.sqf problem
« Reply #2 on: 29 Jul 2005, 13:38:52 »
Yes I did in the init.sqs

This is how I fill the array.

Quote
_unit = _this select 0
?primaryweapon _unit == "" : hint "unit has no primaryweapon"; exit
_magazines = magazines _unit
_mag = _magazines select 0
_c = count _magazines

_totalmags = 0

_i = 0
#loop
_m = _magazines select _i
? _m == _mag : _totalmags = _totalmags + 1
_i = _i + 1
~0.1
? _I < _c : goto "loop"

player globalchat format ["UNIT = %1     TOTALMAGS = %2",_unit,_totalmags]  
MAGZ_ARRAY = MAGZ_ARRAY + [_unit,_totalmags]

Then I run a fired EH

Quote
_unit = _this select 0
_weapon = _this select 1  
_muzzle = _this select 2
_countammo = _unit ammo _muzzle
?_weapon != primaryweapon _unit : exit
?_countammo > 0 : exit
?!(_unit in MAGZ_ARRAY) : exit

_index = [_unit, MAGZ_ARRAY] call indexOf
<ERROR IS HERE
_index = _index + 1
_Magcount = MAGZ_ARRAY select _index
_magcount = _magcount - 1
?_magcount == 0 : hint format ["%1 IS OUT OF AMMO!",name _unit]





« Last Edit: 29 Jul 2005, 13:47:02 by Blanco »
Search or search or search before you ask.

Offline Spinor

  • Members
  • *
  • I'm a llama!
    • The chain of command
Re:Indexof.sqf problem
« Reply #3 on: 29 Jul 2005, 13:42:21 »
IIRC, all elements in the array you are searching in should be of the same type, i.e. in your case MAGZ_ARRAY should be an array of units only. Arrays within arrays indeed do not work, because the equality operator '==' (which is used to compare each element with the object you are looking for) is not defined for arrays.

EDIT: I just  noticed your second post, and I would guess the above is your problem, as MAGZ_ARRAY also contains numbers. I suggest to put the units and the magazine count into two arrays.

Quote
Code:_myarray = format ["%1",MAGZ_ARRAY]

But that returns a string and I need an array    
What the hell, MAGZ_ARRAY is an array!
Well, yeah, but after you put it in the format command, everything becomes a string  :) . What did you try to achieve with this?
« Last Edit: 29 Jul 2005, 13:45:23 by Spinor »

Offline Blanco

  • Former Staff
  • ****
Re:Indexof.sqf problem
« Reply #4 on: 29 Jul 2005, 13:50:51 »
...I suggest to put the units and the magazine count into two arrays....

Thx, I'll try that.

Quote
Well, yeah, but after you put it in the format command, everything becomes a string  :) . What did you try to achieve with this?

The last resort?  :P


*edit*

Works perfect in 2 arrays, thx
Solved.
 
« Last Edit: 31 Jul 2005, 22:54:06 by Blanco »
Search or search or search before you ask.