#Trying to add a combo input for my 2d game

1 messages · Page 1 of 1 (latest)

sour abyss
#

here

#

why is it telling me that it isnt finding "InputCombo" tho?

quick rune
#

It should be the name of a script in your Project

#

make sure the asset name and the class name match

sour abyss
#

but this is exactly the name of my script..

quick rune
#

you wrote playercontrols and not PlayerControls

sour abyss
#

the first 1 is the class

#

so the first one should be PlayerControls and the 2nd one doesnt matter no?

quick rune
#

public List<playercontrols.InputCombo>

sour abyss
#

ok I have no clue why that fixed it but it did

#

now how do I modify it to work for me?

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComboChecker : MonoBehaviour
{
    public PlayerControls playerinput;
    
    public List<PlayerControls.InputCombo> Combos;
    private List<KeyCode> currentKeyCodes = new();

    public void Update()
    {
        int currentIndex = currentKeyCodes.Count;
        foreach (var inputCombo in Combos)
        {
            KeyCode keyCode = inputCombo.keyCodes[currentIndex];
            if (Input.GetKeyDown(keyCode))
            {
                currentKeyCodes.Add(keyCode);
                return;
            }
        }
    }
}
quick rune
#

Well for starters you have to check if any combo is complete. Something like this:

public bool IsComplete(InputCombo combo)
{
    if (combo.keyCodes.Count != currentKeyCodes.Count)
    {
        return false;
    }
    for (int i = 0; i < currentKeyCodes.Count; i++)
    {
        if (currentKeyCodes[i] != combo.keyCodes[i])
        {
            return false;
        }
    }
    return true;
}```
sour abyss
#

im thinking like

private void Serving()
{
    if (Input.GetKeyDown(keyCode))
    {
        InputCombo.Add(keyCode)
    }
}

sour abyss
quick rune
sour abyss
#

oh

quick rune
#

currentKeyCodes.Add(keyCode) adds the players input

sour abyss
#

ah makes sense

quick rune
#

currentKeyCodes is every input the player has made

sour abyss
#

so now I add this script to my player game object

#

was this part supposed to be another seperate script?

public class InputCombo
{
    public List<KeyCode> keyCodes;
    public Vector2 hitDirection;
}
#

I put it in PlayerControls, but the game doesnt seem to find it

quick rune
#

ye it can be separate

#

add [System.Serializable] at the top

#

so the game can find it

sour abyss
#

Ok no errors for now at least

#

Im gonna see try if I can debug.log the inputs

#

if it actually works

quick rune
#

did you assign keyCodes in the inspector?

sour abyss
#

Only assignable thing was the Script

quick rune
#

expand it

sour abyss
#

"List is empty", but thats not what I need right now, I tested it and not even anything is being printed when I pressed a button

#

so its not seeing my keycodes

quick rune
#

ye because it checks if the input matches any combo, but if you don't have any there won't be any matches

#

you should be able to add an item to the list. Make sure your InputCombo class does not inherit from MonoBehaviour

sour abyss
#

its not MonoBehaviour

#

wait

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComboList : MonoBehaviour
{
    [System.Serializable]
    public class InputCombo
    {
        public List<KeyCode> keyCodes;
        public Vector2 hitDirection;
    }
}
quick rune
#

not sure where ComboList is coming from, you don't really need that

sour abyss
#

thats just the extra script I made

#

with that name

quick rune
#

you can have the InputCombo class by itself

sour abyss
#

oh

#

lemme change it

#

okkkk but Im still not seeing anything in the console :c

quick rune
#

you assigned a combo in the Inspector?

sour abyss
# quick rune you assigned a combo in the Inspector?

not yet because this is what Im testing:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComboChecker : MonoBehaviour
{
    public InputCombo inputCombo;
    
    public List<InputCombo> Combos;
    private List<KeyCode> currentKeyCodes = new();

    public void Update()
    {
        int currentIndex = currentKeyCodes.Count;

        foreach (var inputCombo in Combos)
        {
            KeyCode keyCode = inputCombo.keyCodes[currentIndex];

            if (Input.GetKeyDown(keyCode))
            {
                currentKeyCodes.Add(keyCode);
                UnityEngine.Debug.Log("Added" + keyCode); //Here
                return;
            }
        }
    }

    public bool IsComplete(InputCombo combo)
    {
        if (combo.keyCodes.Count != currentKeyCodes.Count)
        {
            return false;
        }

        for (int i = 0; i < currentKeyCodes.Count; i++)
        {
            if (currentKeyCodes[i] != combo.keyCodes[i])
            {
                return false;
            }
        }

        return true;
    }
}

If I press any button, its supposed to write "Added" and then the button no?

quick rune
#

yes, assuming Combos is not an empty list

sour abyss
#

oh

quick rune
#

go back to here and make sure you have some elements in there with hotkeys

sour abyss
#

yep I added something and now I got spammed with this error

#

what index?

quick rune
#

it happens before you press any button?

sour abyss
#

no after

quick rune
#

Because we don't reset the input when we complete a combo.
After adding a key, call

if (IsComplete(inputCombo))
{
    currentKeyCodes.Clear();
    //ballRB.AddForce(inputCombo.hitDirection);
}```
#

you also dont need the first line, public InputCombo inputCombo;

sour abyss
#

Oh awesome, no error anymore

#

im gonna try to make the ball be moved

quick rune
#

ye, all you need should be the rigidbody and then uncomment the line

#

that value will be the force applied to the ball, in this case 0

sour abyss
#

where do I add the force?

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComboChecker : MonoBehaviour
{
    public List<InputCombo> Combos;
    private List<KeyCode> currentKeyCodes = new();

    private Rigidbody2D ballrb;

    public GameObject Ball;

    private void Awake()
    {
        ballrb = Ball.GetComponent<Rigidbody2D>();
    }

    public void Update()
    {
        int currentIndex = currentKeyCodes.Count;

        foreach (var inputCombo in Combos)
        {
            KeyCode keyCode = inputCombo.keyCodes[currentIndex];

            if (Input.GetKeyDown(keyCode))
            {
                currentKeyCodes.Add(keyCode);

                if (IsComplete(inputCombo))
                {
                    currentKeyCodes.Clear();
                }

                return;
            }
        }
    }

    public bool IsComplete(InputCombo combo)
    {
        if (combo.keyCodes.Count != currentKeyCodes.Count)
        {
            return false;
        }

        for (int i = 0; i < currentKeyCodes.Count; i++)
        {
            if (currentKeyCodes[i] != combo.keyCodes[i])
            {
                return false;
            }
        }

        return true;
    }
}
quick rune
#

after you clear currentKeyCodes

#

like I wrote here

sour abyss
#

Awesome

#

last thing before you can finally be free

#

I have a script called "GameState" that checks what the points are, and if its 0-0 a bool called "gamestart" is set to true. I only want to be able to do this serving combo if gamestart is true