A question about the array#index method in RGSS3

Hello community!

It appears I have run into yet another roadblock (already) (yay :headshake:  ). Let me explain what I am trying to do first:

At any point in the game, the player can control (1..5) parties and can cycle through them by the pressing of a key. I am struggling with a particular aspect of the cycling method. I can easily put all my party ids in an array, but to be able to cycle, I must know which party the player actually controls (an information I am also able to find). With that information, I could cycle to the previous or the next party in the array (or go back to the first or last index if need be).

So I needed a method that would return the index of an element in an array. Thanks to the internets, I have found the array#index method. However, for some reason, it won't work in RPG Maker! Let me show you an exemple:

# Let's say I have these:all_parties_array = [1,2,3,4,5]current_party = 1# When I use the index method, unexpected information is returned, as though I hadn't supplied the method with a valid argument/object😛rint all_parties_array.index(current_party)=> #<Enumerator:0x5a17700># Substituting the variable for an integer yields the same kind of result. But when I type the exact same code in my computer's shell, however, it works like a charm! Has array#index been rewritten in RGSS3 or what? Does any of you have an idea of what I might be doing wrong or of a work around?

Thanks!

EDIT:

If you have any insight on why I encountered this issue, feel free to pitch in, but I just ended up defining a new method to mimic the array#index functionnality. So until then, I guess the thread could be closed. Sorry for such a useless topic.  :blink:  

If anyone is interested:

Code:
def find_array_index(array, object)  i = 0  array.each do |val|    if val == object      return i    else      i = i + 1    end  endend
Hmm, why not just use 'rotate!'? 'rotate!(1)' and 'rotate(-1)' should work nicely.


At any rate, when I used the code you supplied above, it works fine (well, it returns the index, anyway). RPG Maker VXA uses Ruby 1.9.3, so there might be a difference between that and Ruby 2.2.0.
It should work in Ace. What exactly happened when you tested that code in RPG Maker?
@Exhydra:


Oh! nice, I didn't know that one! It certainly looks better than the ' array << array.shift ' and ' array.unshift(array.pop) ' I used elsewhere. 😉


Thanks for the info!


@ Source:


Well, it returned an enumerator ( #<Enumerator:0x5a17700> ) instead of the integer I was expecting. If I actually used it in a formula, it produced an error and crashed the game.


EDIT: I just thought maybe it was because ruby wasn't installed on my system (which it wasn't), so I installed it but I still have the same issue...
If I copy the exact code you posted above into the script editor it prints "0" as it should.

Usually, an enumerator should only be returned when no argument is passed. When you used the code in an event script command you might have reached the end of a line, causing the program to add a line break after the term index.
Looks like that was it, huh! Good catch... Thanks a lot, this can definitely be closed now, then!

Original thread: https://forums.rpgmakerweb.com/threads/a-question-about-the-array-index-method-in-rgss3.41710/