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. :slight_smile:

You only need one array. Add each input to an WhatPlayerPressed array as a String, then whenever you like you can use Array > Contains : String And String And String , branch. Don’t forget to make the conditions which clear the array too.

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!

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.

1 Like

That’s quite interesting aswell. o.o

Though as you said this might be problematic with a dash, ex; Forward-Forward move etc. I will keep this in mind aswell! Thanks!

Actually with that dash combo, there wouldn’t be any problems. I think it’s only the ABBAC pattern that will get you into trouble.

Also that’s not the only way to get around input orders. Just the simplest :wink:

Thank you. I may use this idea for later, but I believe an array string could be useful for now. Cheers!

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.

Ow I am not expecting a prepared solution. I rather learn on the way, or else I would pay someone to do it for me to respect his work no problem on it.

Though I still would get some advices just like yours, as Quocanh and Tomofnz.

One thing I have to mention is, I can’t code, and coded very little years ago, so using such systems you mentioned (they look like C++ coding) is something beyond me.

I appreciate your help but if there is no way to mimic those systems in blueprints, I am kinda lost. I will still check what are those about though, thanks!

Good luck man, those things sound complicated but are hmm not simple but basic systems, like the building blocks of a lot of stuff, if you learn them they will help you a lot while making your game. They should be doable by blueprints too, as I said it is up to you, if you want to spend the time to understand those concepts or pay someone to do they blueprints for you, they shouldn’t be too expensive thought.

I will try my compare-strings work which seems quite facepalming as I checked other ones being proffesional and I would agree that I would need a better system if my character would have way too much abilities.

Though there are still ways to get around for me, for example, the last string of array being Punch, I could check for other arrays having punch at the end etc.

Ofcourse things could get complicated if I was making a game like Tekken, where the character named King got 128+ moves.

Thank you once again.

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

Thank you for your answer. I asked this exactly a year ago though and I have a solution for this already.

Considering this as an answer to appreciate your time and effort. ;D