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

What's the best way to know an array index?

Status
Not open for further replies.
Level 20
Joined
Jul 14, 2011
Messages
3,213
If i have 100 items in an array (Item[1, 2, 3, 4]) and I want to detect the index of the picked item, what should I do?

The "brute-force" way I can think of is looping From 0 to 100, but, isn't there a best way?
 
Level 5
Joined
Dec 12, 2011
Messages
116
You can work with Custom Values of items...
Whenever you set an item to your array, immeadetly add to that item's custom value the index. So everything will be straightforward then.
 
Level 15
Joined
Jul 6, 2009
Messages
889
What hamsterpellet said, probably easiest way is to work with the custom value of the items. Assign the position of the array that item sits in as the item's custom value. Another way would be loop through the array and if the item at the loop position is equal to the picked item, then that is the item's index.
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Well.. I already know the loop way, and Item Level, and ItemLevel and all the OE common item Values. Having an Item Indexing System requires more work.

There must be some way to directly detec the index of an item on an array =/
 
It is not much of an issue to loop through 100 values. Actually, it is very light on the CPU, as it is only comparing values. If that is the handiest way for you, then go with it, it won't cause problems in an item system. This poses a problem in systems that require rapid movement and 0.01 refresh rates. Item systems do not suffer from such things, remember when item systems used gamecache and were still pretty fast?
 
@Spartipilo: Well, think of it this way. Hashtables are O(1), looping is worst-case scenario O(n). I'm pretty sure hashtables will pass the loop in speed by a bit in a good amount of scenarios (although it is negligible). Hashtables aren't that slow, they just are considered slow when being compared to arrays, but that is a bit unfair for obvious reasons. Compared to standard functions, they are pretty good.

The best method would be SetItemUserData() though. Just assign it on the looping, it does not have to be a fully fledged indexing system unless the items are constantly being swapped around as far as indices go in the array.
 
Level 9
Joined
Jul 10, 2011
Messages
562
remember that the item array index has to be bound to the item or it will be lost i used the item life for an array index once worked fine and now with textmacro you can quickly make 100 items, all with 1 more hp than the previous .

now i feel screwed -.- i already told to use the HP of the items as dummy array as first answer....and now after 20 other answer mine is told again Oo
 
Level 17
Joined
Jul 17, 2011
Messages
1,864
now i feel screwed -.- i already told to use the HP of the items as dummy array as first answer....and now after 20 other answer mine is told again Oo

yes i was just clarifying your answer because i think its best to use item life as an array index.

Because the hp of items cannot change ingame.[/irony]

irony? :goblin_yeah::goblin_sleep:

there is a setitemlifebj
 
Status
Not open for further replies.
Top