(Ruby) Deleting values from an array at a dynamic index

Hi everyone!



I figured that despite the fact the system is evented the question was closer to programming than to eventing so i should post it in the scripting boards (i figured that in the normal support boards almost no one would know what an array is...). I apologize if i was wrong to do so and kindly ask you to point me in the right forum (or to tell me to go look at a solution at stackoverflow.com 😀 ).


Context: I'm creating an ATB battle system using events and it has states (ie poison, paralysis, atk buff, ...).
I display pictures close to the actor portrait to show what states he has (ie show picture "Poison Icon" if actor has state "Poison"). This again works.


The problem i have is to delete those pictures.


How the system works:
I define the states of an actor using a game variable as an array: V[state array]=[0,0,0,0,0,0,0,0,0]



Then i have another variable to tell my dysplaying event what icon it needs to draw: V[icon array]=[0,0,0,0] (it will only draw 4 icons no matter how many states)


Right now i add a state this way: 


     V[state array][state_id]=Duration For example if i want to apply the poison state to actor 1 for 3 turns i put V[95][0]=3


     Then i modify the icon array: I check for the first 0 and modify it to the number of the icon i want to display : V[icon array][first zero found]=Picture_id.


To delete a state, i check if the duration (V[sa][s_id]) is <= 0, if it is i remove the state via event commands and then starts the problem:



Right now i use V[ia].delete(Picture_id) and then V[ia].push(0)
This ensures that it deletes the value in the icon array that corresponds to the icon of the removed state. (Ie it will remove value 1 from the icon array if the state poison is removed.)


Then it adds 0 at the end of the icon array so that it remains the same size.


Right now it works, as expected, but i bet you can see where i'm going: Array.delete(x) will delete all "x" values it finds, so if several states share the same icon, i now have a problem.


Lets say V[ia]=[1,2,3,4]
it will display icons 1,2,3,4 as it should
then state 2 is removed, as i explained above i remove 2 from this array
V[ia] is now [1,3,4]
I then add 0
V[ia] is now [1,3,4,0]
My system will correctly display icons 1,3,4 in order and if i were to add state 2 again, it would display icons 1,3,4,2 in that order.


But, if V[ia]=[1,2,2,3] and i remove state 2 using delete(), V[ia] will now be equal to [1,3] and adding 0 to the end would make it [1,3,0]. So i lost the information that a second state using icon 2 was to be displayed.


How could i make it so that i remove only the icon that corresponds to the state that was removed, at the position it has to?



I thought about using V[ia].delete_at(index) but then i need to track the index at which the icon has to be removed for each state that was applied (and keep it updated when i remove a state...), and i don't know how i can do that.



Could anyone point me towards a solution? Maybe using arrays is not the best system and i could do it with a hash? (but then how? i'm entirely unfamiliar with hashes)
I could also simply add a different number for each state and then add the same icon but with a different picture name... But that would be admitting defeat and i don't like it 🙂


(Is it wrong to ask if i have a working (albeit ugly) solution?)


Thanks in advance, sorry for the long post.
I think you could use the index function to return the location of the state that is being deleted, and then use the delete_at function to remove the icon at that location.


See Ruby documentation for arrays at:


http://docs.ruby-lang.org/en/2.0.0/Array.html


While I think that's probably the best solution, one other thing you could do instead is remake the Icons array every time you change the States array.
Thank you very much for the quick answer!


I found a solution. I had to use another set of arrays corresponding to the state arrays.


Like you said i used the index function so now when i set a state i also attribute the value of the position in the icon array in the new arrays. (If it doesn't find a zero, which means 4 states are already displayed, it sets it to an arbitrary number, i chose 31)


And when i remove a state, i now use delete_at() to the value at the position corresponding to the state being removed in the new arrays.


(It had all kind of weird interactions before i put 31 in and made the system basically ignore it because it would delete at postion 0 which was the default... Took me 1 hour to figure that...)


Anyway, the system is now working and i can use multiple of the same icons. Thank you very much!

Original thread: https://forums.rpgmakerweb.com/threads/ruby-deleting-values-from-an-array-at-a-dynamic-index.55733/