Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
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 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?
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.