How to get the index from an array element?
I'm making the targeting system for a turn based RPG and would like to get an element index in integer form, so I could check who's selected and stuff like that. Is it at all possible, or do I need to create an integer and manage it all the way through?
asked Sep 09 '18 at 09:15 PM in Blueprint Scripting
No, because TArray same as raw C++ array is using memory offset as index, making it unreliable id, if you remove element from array it will move all farther items in memory in direction of removed item. You might consider using TMap which use key to value pairing (TPair) to store items and you can use any type as index, at first it might not make sense to use integer as key type as it will work like TArray, but thing in TMap set index will be persistent or else you will change index of the item yourself
answered Sep 09 '18 at 09:46 PM
I'm not sure I follow exactly what your thinking is here, but I have found that using an array of actor object references I am able to set and index to "None" by using "Set Array Element" an connecting the array and index pins and leaving the "item" pin unconnected. You can use the "Find" node to find and Element in an Array and return it's index. You can use "Get (a copy)" on the array and use an index integer to retrieve the Element at that index. You can then use "Is Valid" to check if it is valid (Not Pending Kill and Not "None") or you can use the "Find" Node to find the first element that is "None" by not connecting the "Item" pin. It will return the index number of the first index it finds -- to "None" ad return that index, or if all indexes are set, it will return a -1 for the integer.
Does this Help? You could make an Array of Actor Objects and assign the characters/enemies. You could create multiple Arrays (Selected/Targeted/Wounded etc.) and each index in each array would represent each character/enemy. Your other arrays could be booleans, floats or what ever. As long as you do the same operation (Removing, inserting etc.) the same to each array your indexes should match. Or you could create a custom structure variable to put in an array which contains all the data you need and make an array of that structure corresponding to your array of characters/enemies. Hope this makes sense??
answered Apr 16 '19 at 04:07 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here