GabrielTDGabrielTD Posts: 8Member
edited September 12 in Tutorials

I'm doing a fighting game, and I wanted the player to be able to do combos in the fight, Ex: punch-punch-kick. I have zero idea how to do this, could anyone help me?

Answers

  • GabrielTDGabrielTD Posts: 8Member
    edited September 12

    And when the player starts the combo, the enemy will not be able to interrogate him.

  • TwistedTwiglegTwistedTwigleg Posts: 851Admin
    Answer ✓

    Having never tried this before, here is what I would do:

    In _Input I would store all keyboard input events into a array, something like this (untested):

    var combo_events = [];
    func _input(event):
        if (event is InputEventKey):
            " We only want press events, not release events or holds events "
            if (event.pressed = true):
                combo_events.append(event)

    Then in _process or _physics_process, make a timer to periodically check and clear the array. Something like this (untested):

    var timer = 0;
    const COMBO_TIMER = 0.2;
    func _process(delta):
        " Add time to the timer "
        timer += delta;
        " Check to see if we need to check for combos... "
        if (timer >= COMBO_TIMER):
            " Reset the timer "
            timer -= COMBO_TIMER;
            " Check to see if anything was pressed during the timer "
            if (combo_events.size() > 0):
                " Create some variables for combo checking... "
                " Note: Here we are assuming checking for two button combos "
                " To check for more buttons, you just need to keep shifting/assigning events to "
                " variables until you have however many you need "
                var combos = [];
                var last_key_event = null
                for (event in combo_events):
                    if last_key_event != null:
                        " Let's assume J -> K is a combo"
                        if (last_key_event.scancode == KEY_J and event.scancode == KEY_K):
                            combos.append("JK_Combo")
                        " Another combo (J -> L )"
                        if (last_key_event.scancode == KEY_J and event.scancode == KEY_L):
                            combos.append("JL_Combo")
                    last_key_event = event
                " Do whatever combo processing you want/need to do here! "
                print ("You entered :", combos.size() + " combos!")
                " Clear/Reset combo_events "
                combo_events = []

    Assuming the code above works, there are several minor things you could do to improve it, like only starting the timer when a input event is pressed, but hopefully the example code above gives you some ideas on how to go about it :smile:

  • GabrielTDGabrielTD Posts: 8Member

    On line 17, you said var last_key_event = null , so it will always continue null, no time you change it. Next on line 19 you say that if last_key_event != Null but it has always been null, understand? Or did I interpret it the wrong way?

  • MegalomaniakMegalomaniak Posts: 949Admin
    edited September 13 Answer ✓

    No. He creates the variable in line 17 and initializes it as null, but the script will run in a loop. The if is there to deal with a situation where the variable is no longer null. Notice that there is a last_key_event = event in the end of encompassing for-loop.

  • TwistedTwiglegTwistedTwigleg Posts: 851Admin
    Answer ✓

    As Megalomaniak said, last_key_event is initialized as null outside of the for loop, and is set at the end of the for loop.

    I’ll do my best to try and explain how the code in the for loop works in regards to last_key_event, which hopefully will help show how it’s working:


    Inside the for loop it first checks to see if last_key_event Is null. Initially last_key_event will be null, since that is its initial value, and no combo checking will happen. Then last_key_event will be set to the event being processed right before moving on to the next event in the combo_events list (that is what last_key_event = event is doing).

    This makes it when the second event is being processed, last_key_event is no longer null, but rather is holding the event prior to the second event. Then the combo checking code will run since last_key_event is no longer null. input_key_event is only null for the first initial run through the for loop, and then after that it is always the previous event in combo_events after the initial run.


    The reason I wrote the example code like that was because I needed to compare two events in combo_events. There are other, probably better, ways you could do get two or more items in a list, but that was the first way that came to mind when I was writing the example. :smile:

  • GabrielTDGabrielTD Posts: 8Member

    Good evening! Thank you very much for the quick response and explanation, I did it and it worked. If you want, I put the code and can close the topic.

Sign In or Register to comment.