How to compare two arrays?
Hello and Howdy!
I have one single question that I would like some small help! I am working on a Fighting game and I need a way to compare two arrays. So far, I am planning a system where every input adds a string to an specific array. Let's name it Player Input Array. And let's say we got another array for an event to trigger, named Heavy Punch. Now in order to trigger Heavy Punch, a player has to press in order, Down - Forward - Punch.
In Player Input Array, I can make the player create strings in order if the player Presses the inputs in order, but that's another story. Let's say the player was successful on doing such inputs in order.
Player Input Array = 1-)Down - 2-)Forward - 3-)Punch (Numbers are the elements of arrays in order) Heavy Punch = 1-)Down - 2-)Forward - 3-)Punch
So I got two arrays and I need a Branch to check and trigger an Event, animations etc.
As far as I checked there is no simple way to compare two arrays but have a foreachloop to check if each element is equal both in string names and the element number they are. So I have to compare two arrays not only by string names matching, but also element numbers has to be equal aswell.
A little starting point would be appreciated! Thanks. ^_^
asked Apr 25 '15 at 06:59 AM in Blueprint Scripting
I would go for another solution.
My suggestion is to create a new actor (let's call it combo) with two members:
moves: an array for the combo moves, storing ints associated with each button
index: integer telling which is the next button in the combo (default to 0, so it points to the first item in the array)
for each character you would define an array of combo class and instantiate all of them into a combo reference array at character construction. (this is useful if you define a bunch of combo derived actors which can be inserted in this array)
each time a button is pressed you check for each element in the combo reference array. if it matches the move in the combo pointed by it's index, you increase index (if index equals the number of moves in the combo, do the combo and set index to 0). if the move doesn't match with the next move in the combo just set index to 0.
this may sound a bit confusing. feel free to contact me for further help on this topic
answered Apr 01 '16 at 02:31 AM
Hello! I thought about it too! Though my concern is, what if the player tries something like 6x Down + Forward + Punch. Then I wouldn't be able to have a contains for such a thing. Or maybe I can figure something out, creating another array for last 3 strings and comparing that one instead. I will give it a thought. Thanks!
answered Apr 25 '15 at 08:07 AM
This is an interesting CS problem. Here's how I would solve it:
There's not really any way to "compare" two arrays quickly without comparing simple things like length or going through every element of the array. Going through every element of every array of every move you want to implement is inefficient, which you understand and are trying to get away from.
Instead, what you need to do is to distill the "essence" of the combination and order of your inputs into a single variable or number. The best way to do this is to use prime numbers.
Recall from math class that every number is made up of a unique combination of prime factors. If you multiply any unique combination of prime factors you will always get the same number. And no matter what, when you look for the smallest possible factors of any number you will always get the same combination of prime factors.
We can use this fact by mapping each of one of your inputs to a different prime number. Say UP is 2, RIGHT is 3, DOWN is 5, LEFT is 7, and etc. We will multiply each of the inputs' prime numbers together to get a unique number that will represent a unique move.
Now how do we include information about the order in which the inputs are pressed? One way to do it is to just multiply that prime orderIndex times. So for example, UP RIGHT DOWN would be (2) * (3 * 3) * (5 * 5 * 5). LEFT DOWN LEFT would be (7) * (5 * 5) * (7 * 7 * 7) This may get you into some trouble with certain move orders (DOWN UP UP DOWN == UP DOWN DOWN UP) but the number of problem moves may be small enough that you can have special checks or you can design around it.
After that, match the result of the multiplication to a move. You will need to assign a special number for every move. You can just check for certain moves in a series of branches/if statements or use a hashmap.
TL;DR do these things:
Map every input to a unique prime number.
Multiply those prime numbers i times per input, with i being the 1 + the order index that the input was pressed.
Match the result to the move you want to do.
answered Apr 26 '15 at 04:48 AM
Man I will let you know what you need to investigate to make this, I hope to not let you down if you were expecting a made solution.
Ok I you need a different structure than an array, you need to investigate state machines or data trees, they Can be represented by arrays too.
First, comparing strings is inefficient, you can map every action button to a number, for example backwards means 1, upwards 2 etc.
Second you need to make a FIFO queue, for example your player inputs down,waits too long, foward, puch, you need to clear the down after a certain time has passed.
Third google state machine to find out if word belongs to language, (in the context of language structures if the first search is unsatisfactory) you need to find if up,down,up,down, high punch belongs to your moves language.
Fourth if you make a move clear your FIFO queue.
answered Apr 26 '15 at 06:40 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here