• 🏆 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!

Array question?

Status
Not open for further replies.
Level 12
Joined
Dec 2, 2016
Messages
733
I want to do something like this


array myArray = {"Player1Name","Player2Name","Player3Name"}

for i = 1,#myArray do
if player(0) == myArray[1] then
//blah
end
end

I'm new to jass so this is written in Lua :p but you get the idea, how would I go about this?

Edit: I got what I need working by creating a array variable, and assigning each index to a player name and then looping through each index and checking if the index = player name and executing the code. Is this the best way to go about this?

Also how can I check array length? Indexes.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Is this the best way to go about this?
Sounds pretty much optimal.
Also how can I check array length? Indexes.
There is no need to as the length is always known and constant.
In the current Warcraft III version...
JASS:
constant integer JASS_MAX_ARRAY_SIZE=32768
In some old Warcraft III versions...
JASS:
constant integer JASS_MAX_ARRAY_SIZE=8192

Warcraft III arrays are dynamic arrays. They will always resize up to hold the largest assigned index. The largest such a dynamic array can grow is JASS_MAX_ARRAY_SIZE. The last few indices have been known to bug with save/load cycles but I am not sure if this is still the case.

If one wants a list then one needs another variable to keep track of the list length.

A decent implementation would look like...
JASS:
// This goes in your data initialization function.
set PlayerNamesLength = 0
set PlayerNamesName[PlayerNamesLength] = "Player1Name"
set PlayerNamesLength = PlayerNamesLength + 1
set PlayerNamesName[PlayerNamesLength] = "Player2Name"
set PlayerNamesLength = PlayerNamesLength + 1
set PlayerNamesName[PlayerNamesLength] = "Player3Name"
set PlayerNamesLength = PlayerNamesLength + 1

// The following locals must be declared before performing the search.
local integer searchIndex
local string playerName = ... //Get player name here...

// The following code does the search.
set searchIndex = 0
loop
    exitwhen searchIndex == PlayerNamesLength
  
    if PlayerNamesName[searchIndex] == playerName then
        // Match found so your actions "blah" go here...
    endif

    set searchIndex = searchIndex + 1
endloop

The advantage of initializing data in such a way is that it forms a repeating pattern of 2 lines that can easily be extended, modified, moved around, etc making the code considerably more maintainable. In object orientated languages like Java or C++ one would use a list data structure to replace the repeating 2 line pattern with single method calls but JASS does not support this with any reasonable efficiency.

Be aware that player names are technically case insensitive. As such you might want to only use a single case for defining the constants in the array and then use StringCase on the input player name to change it to the chosen case for comparison.
 
Last edited:
Status
Not open for further replies.
Top