HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Set (Integer) = Index of (element) in (array)

04-13-2008, 01:27 AM#1
Gorman
Is there anyway to do something like this simply, to find the index of an element in an array?

Or shall i have to do some ifs and elses...

EDIT: i could do it using game cache, is there any draw backs with doing this?
04-13-2008, 01:30 AM#2
Rising_Dusk
You'd have to loop through the array and stop when "MyArray[Index] == KnownUnit", then you'd know the index. Otherwise, there is no easy solution short of some kind of handle indexing so you can retrieve the index at will.
04-13-2008, 01:33 AM#3
Strilanc
If you elaborate more on what you're doing then we can give better suggestions.
04-13-2008, 01:38 AM#4
Gorman
OK then, i have already defined an array, now another function returns an element from that array. What i want to do is find the index for the element.

As far as i know i have to options, loop until Array[Index] == Element or i can save my array into the game cache with the tags being the elements, so when the function returns them i can change them into strings and find the index, then put it into my array.
04-13-2008, 01:50 AM#5
PurgeandFire111
Quote:
Originally Posted by Gorman
EDIT: i could do it using game cache, is there any draw backs with doing this?

Gamecaches are slow, but would probably work.
04-13-2008, 01:54 AM#6
Vexorian
If the number of elements is very high and the elements themselves are not normalized, gamecache would be one of the fastest solutions, a custom hash is "first place".
04-13-2008, 01:56 AM#7
Gorman
i hav no idea how to do that, i gui -.-

How would i do a custom hash?
04-13-2008, 03:02 AM#8
Pyrogasm
Why not just use a struct array with some custom methods?
Collapse JASS:
scope IntStuff initializer Init
    globals
        Int array YourArray
    endglobals

    struct Int
        integer Val = 0
        integer Index = 0

        method create takes integer Index, integer Value returns Int
            local Int I

            set I = YourArray[Index]
            if I == 0 then //I'm not sure if this is the correct way to check for a "null" struct...
                set I = Int.allocate()
            endif

            set I.Index = Index
            set I.Val = Value
            set YourArray[Index] = I

            return I
        endmethod
    endstruct

    private function Init takes nothing returns nothing //Neither this nor the scope would be required if you could set the default value for YourArray members, but I'm not sure if you can do that in vJASS...
        local integer I = -1
        loop
            set I = I+1
            exitwhen I > 8191
            set YourArray[i] = 0
        endloop
    endfunction
endscope

function ExampleCodeCallback takes nothing returns nothing
    local timer T = GetExpiredTimer()

    call BJDebugMsg("The Index is "+I2S(YourArray[GetCSData(T)].Index))

    call DestroyTimer(T)
    set T = null
endfunction

function ExampleCode takes nothing returns nothing
    local timer T = CreateTimer()
    local Int = Int.create(GetRandomInt(0, 200), 16)

    call SetCSData(T, Int)
    call TimerStart(T, 1.00, false, function ExampleCodeCallback)

    set T = null
endfunction
There may be a better way than that because I don't know the full vJASS intricacies; mayhaps with operator overloading you could do something.
04-13-2008, 05:01 AM#9
Strilanc
Why are you returning only the element? If the function is getting it from the array, just return the index and you skip the whole problem.
04-13-2008, 07:55 AM#10
Gorman
Quote:
Originally Posted by Pyrogasm
Why not just use a struct array with some custom methods?

Coz i never knew such a thing existed ^^

Ty, ill try figure out wat it all means lol, more incentive to learn Jass!

Quote:
Originally Posted by Strilanc
Why are you returning only the element? If the function is getting it from the array, just return the index and you skip the whole problem.

The function isnt getting it from the array, it is giving an element that is in the array. I see why there was confusion, i should have wrote "that is in that same array" instead of "from that array".

What im actualy doing is using it to setup a custom mana/spell casting system, so i need to know infomation about the spell. The only way i know of to do that is to have an array of all the spells/infomation and then just recall it when the spell is cast.

It seems i have found something that can help me greatly, yay for search function! abilities hashtable!