• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Get Array Integer

Status
Not open for further replies.
Level 7
Joined
Jun 16, 2008
Messages
253
I want to be able to grab the integer value of an array, associated with a given unit. I figure GUI isn't up to handle this, as there are no options to GetArrayInteger or whatever it might be called in custom script.

I don't know anything about JASS, but it seems to me that I should be able to do the following with it.


Associate a unit with his array number.
So I could go Set Variable(tempinteger) = arrayinteger of Unit
And even,
Set Variable(integer) = arrayinteger of (random unit within unitvariable, or even unitgroup)

So if a variable array has the unit footman_0001, then the trigger can find the footman and take the value of the particular array integer it is assigned to.

Could this be done with a Where?

I don't know if I've explained that too well because I'm used to scrolling charts, but hopefully you can see what I mean. I just want to be able to get the integer of the array number that unit happens to be put into at the time. Would that be parameter? All I know about JASS is what I just read last night, prior to that I'd never even see a code script.

But Dad does know and he was talking about a Where sort of thing.
Where unit = something, then take action. I have a vague notion of it, but could this Where function find any specific unit associated with any of the variables or variable arrays?

If you know anything about this please let me know. And ideally, provide me with a means or example to do it, to the best of your JASS knowledge, and I'll muddle through the rest somehow.

Appreciated.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Option one: an O(n) search:

JASS:
local integer i = 0
local unit u = //YOUR UNIT
loop
    exitwhen udg_YourUnitArray[i] == u
    set i = i + 1
endloop
//use i

Option two: Store the index on the unit. This option is way better for arrays of any significant size.

For example, with UserData,

JASS:
//somewhere
call SetUnitUserData(u,i)
set udg_YourUnitArray[i] = u

//somewhere else
set i = GetUnitUserData(u)
 
Level 7
Joined
Jun 16, 2008
Messages
253
(I'm not editting this in the first post so that it will bump it up, and I don't lose my reference. And I'm lazy.)

I love this! I can actually understand what you wrote! It's amazing what overnight study of JASS tutorials can do!
Well, that's a lie, there's a part in Option 2 that I don't understand the:
call SetUnitUserData (u, i), is that custom value or something? I can see unit and integer involved there so it seems it could be to me. (Though I don't fully understand the meaning of having your values inside the parenthesis yet).
And the somewhere/somewhere else indicate two separate triggers? Just clarifying because it looks like it is.

But from the looks of it I could be using both.

Is it possible to have the trigger search through a unit variable array for a unit that matches a unit-type? So it'll go through and compare the unit-type of every unit in that array to unit-type footman, and if I have a footman in the array, then it will either do what you showed with Option 1, and give me the integer, or whatever else I want to do with it.

If it would be too much of a hassle to write or figure out, don't bother, it's not a huge concern right now.

I'll just see if I can work out Op 2.

First command gives a unit a certain integer value (that actually looks horribly wrong but oh well)
then it sets my variable MyUnitArray of that integer index to equal that unit.

then somewhere else I can recall the integer value of that unit.

Or something. I think I'll have to understand what SetUnitUserData is first!

If... you're happy and you know it, post an answer! If you're happy and you know it, tell me now! If you're happy and you know it and you really want to show it, if you're happy and you know it, make my map!
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
SetUnitUserData is a function which is, as you guessed, what is called Custom Value in GUI.

Function parameters are enclosed in (), for example:

JASS:
function a takes integer i returns integer
    return i + 5
endfunction

function b takes nothing returns nothing
    local integer i = a(2) //i will be equal to 7
endfunction

As for comparing to a unit-type,

JASS:
local integer i = 0
local integer t = GetUnitTypeId(<YOURUNIT>)
loop
    exitwhen GetUnitTypeId(udg_YourUnitArray[i]) == t
    set i = i + 1
endloop
//use i

Or, if you stored the array as unit types only --

JASS:
local integer i = 0
local integer t = GetUnitTypeId(<YOURUNIT>)
loop
    exitwhen udg_YourUnitTypeArray[i] == t
    set i = i + 1
endloop
//use i
 
Level 7
Joined
Jun 16, 2008
Messages
253
That's amazing!

Cheers dude, I'm just staggered at the freedom of custom script. All those things I always wished I could do... *drools*

And still do really. But thanks, problem solved!
 
Status
Not open for further replies.
Top