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

Astfgl66

Veteran
Veteran
Joined
Jan 5, 2016
Messages
734
Reaction score
605
First Language
French
Primarily Uses
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 :D ).


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.
 

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
6,114
Reaction score
5,879
First Language
English
Primarily Uses
RMVXA
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.
 

Astfgl66

Veteran
Veteran
Joined
Jan 5, 2016
Messages
734
Reaction score
605
First Language
French
Primarily Uses
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!
 

Latest Threads

Latest Profile Posts

After three years, it was awesome to be back at Gen Con!
Words of Wisdom: You can be honest and alone. Or dishonest and together.
Working on mapping using RPGTools to create parallax maps using PVgames tiles/assets. It's super cool... but mapping is still something I drag my feet on getting done. I hate this but love the math parts of design, which is the opposite of a lot of dev's around, I think. It really do be different strokes for different folks.
Spend $5,000 on a new gaming PC. Didn't expect it to be like 1 foot wide, 2 feet tall, and weigh like 100+ pounds. The heck did I get myself into?

Forum statistics

Threads
124,379
Messages
1,163,210
Members
163,179
Latest member
LeuzyGamer
Top