Hi folks having an issue that’s driving me crazy.
I’m working on a game mechanic in which you type a sequence of buttons and if that sequence matches with an existing one then you spawn a specific game object, kinda like a combo but not exaclty.
Here’s the code with further explanation:
using System.Collections;
using System.Collections.Generic;
using System.Linq; // allows to compare contents of two arraws with a built-in function (SequenceEqual)
using UnityEngine;
public class BuilderMode : MonoBehaviour
{
public PlayerMovement playerMovement;
public bool onBuilderMode;
public bool arraysAreEquals;
public int[] testComboArray = { 1, 2, 3 };
public int[] testComboInputArray = new int[3];
//combo Inputs:
public KeyCode metal = KeyCode.Q;
public KeyCode spark = KeyCode.W;
public KeyCode ammo = KeyCode.E;
private int q_metalCode = 1;
private int w_sparkCode = 2;
private int e_ammoCode = 3;
public int comboSlotValue;
void Start()
{
onBuilderMode = false;
arraysAreEquals = false;
for (int i = 0; i < testComboInputArray.Length; i++)
{
testComboInputArray *= 0;*
}
//for each (int i in testComboInputArray) print (i);
}
void Update()
{
StartBuilderMode();
CancelBuilderMode();
FillInputArray();
printComboKeysValues();
if (onBuilderMode == true && Input.GetButtonDown(“Fire1”))
{
CheckArrays();
}
}
void StartBuilderMode()
{
if (Input.GetButtonDown(“BuilderMode”))
{
onBuilderMode = true;
playerMovement.movementSpeed_magnitude = 0;
}
}
void CancelBuilderMode()
{
if (onBuilderMode == true && Input.GetButtonDown(“Fire2”))
{
onBuilderMode = false;
playerMovement.movementSpeed_magnitude = playerMovement.walkSpeed;
comboSlotValue = 0;
for (int i = 0; i < testComboInputArray.Length; i++)
{
testComboInputArray = 0;
}
}
}
void printComboKeysValues()
{
if (onBuilderMode == true)
{
if (Input.GetKeyDown(metal))
{
comboSlotValue = q_metalCode;
}
if (Input.GetKeyDown(spark))
{
comboSlotValue = w_sparkCode;
}
if (Input.GetKeyDown(ammo))
{
comboSlotValue = e_ammoCode;
}
}
}
void CheckArrays()
{
if (testComboInputArray.SequenceEqual(testComboArray))
{
arraysAreEquals = true;
}
else { arraysAreEquals = false; }
}
void FillInputArray()
{
if (onBuilderMode == true)
{
int i = 0;
int range = testComboInputArray.Length;
////
if (i < range)
{
if (testComboInputArray > 0)
{
i++;
}
if (i < range)
{
testComboInputArray = comboSlotValue;
}
}
////
print("ArrayInout Length is " + range);
print("filling Array Slot " + i);
}
}
They way I’m trying to solve it is by creating two arrays:
public int[] testComboArray = { 1, 2, 3 }; this is the array that has already a sequence.
public int[] testComboInputArray = new int[3]; and this is the empty array that the player is supposed to fill.
----------
What I did was asign int values to the QWE keys in the printComboKeysValues() basically if you press the Q key the variable comboSlotValue changes to 1, If you hit W it becomes 2 and E is 3.
The real problem starts in the FillInputArray() method basically what I’m trying to do is the player assings the first array slot (testComboInputArray[0]) to the comboSlotValue variable (which can be 1,2 or 3) after the slot is filled then I check if the value of the slot is greater than zero, meaning is no “empty” anymore then increase i by 1 to move into the [1] slot and finally [2] but it never makes it past [1]. the i++ stops adding and never reaches [2]
the two solutions I tried were:
void FillInputArray()
{
if (onBuilderMode == true)
{
int i = 0;
int range = testComboInputArray.Length;
////
while (i < range)
{
testComboInputArray = comboSlotValue;
if(testComboInputArray > 0)
{
i++;
}
}
////
print("ArrayInout Length is " + range);
print("filling Array Slot " + i);
}
}
this one “crashes” Unity because it creates an infinite loop.
and:
void FillInputArray()
{
if (onBuilderMode == true)
{
int i = 0;
int range = testComboInputArray.Length;
////
if (i < range) //check if i is smaller than the array range
{
int currentSlot = testComboInputArray*;//get slot to modify*
currentSlot = comboSlotValue; //the current slot will be added the value of the player input
if (currentSlot > 0) // if the current slot is not zero then move to next slot
{
i++;
}
}
////
print("ArrayInout Length is " + range);
print("filling Array Slot " + i);
}
}
this one never adds up i, any ideas of to what Im doing wrong or not doing?