0 votes

Need to match two different variables (that will hold various string values)under the same function so they process at the exact same time. (It can't wait a frame).

Whenever I try something like:

_process(delta):
     match a:
          #match and do stuff
     match b:
          #match and do stuff

This seems to ignore the 2nd match function. Is there a work around for this? I've tried;

match a and b:
     #match and do stuff

but doing this would require me to a lot of other code for separate types of matches.

in Engine by (293 points)

2 Answers

+1 vote
Best answer

Using two match one after the other should work fine. Something is probably wrong in the code you hinted in #match and do stuff, would be interesting to see it.

by (27,595 points)
selected by

Hmm, I think I'm seeing the issue in my code, but I don't know the work around.

Essentially it's this:

var a
var b 

_process(delta):
     _myfunction(delta)


_myfunction(delta):
     match a:
          "yes":
               #do something
               if circumstances_met:
                    return
               #do more things
           "no":
               #do something
               if circumstances_met:
                    return
               #do more things
     match b:
          "yes":
               #do something
               if circumstances_met:
                    return
               #do more things
           "no":
               #do something
               if circumstances_met:
                    return
               #do more things

I think the return on the first match is causing it not to run the second match.

Then... don't return? Use an else if you don't want to run the #do more things

I'm using the return to keep _process from accessing those things. Making it so it accesses it only once:

Sample code:

if p1_action1 == true:
                return
p1_action1 = true
yield(get_tree().create_timer(sec),"timeout")
p1_pos = $Player1.transform
p1_cmd1 = "clear"
t = 0.0
p1_action1 = false
if dodge == true:
      dodge = false

Unless there is a better way of doing this?

edit To be clear, I need _process for part of the function and need it to be a one shot for the other.

This?

if p1_action1 == true:
    return
else:
    p1_action1 = true
    yield(get_tree().create_timer(sec),"timeout")
    p1_pos = $Player1.transform
    p1_cmd1 = "clear"
    t = 0.0
    p1_action1 = false
    if dodge == true:
        dodge = false

An alternative is to put each match in a function and call them both in _process, so you can use return to go back to _process. match has little to do with all this, really, you could even have used an if to check a abd b

Adding else: makes the function do the same thing. Because although I'm having the 2nd half called once, the first half I need to remain being called, since it's moving the player. The return part of it, is simply a lock to make sure it only accesses the whole function once on the first go, the yield makes it wait till the movement is done, then it sets all the necessary variables.

I do have them in separate functions right now, but it causes this issue with one playing a frame earlier than the other (I know it's only a frame, but it's making other issues). If I can get them both in the same function, then it'll play at the same time.

:/

Hey Zylann, I'll figure out a work around. Ultimately you solved the initial answer and helped me see that the issue isn't with match, but my own script. Thanks for trying to go above and beyond.

0 votes

Wow, at first I thought it is impossible but after a quick peek of GDScript's documentation on match, yes, you can match multiple patterns and here's how:

match x:
    a, b:
        print("matched both a or b!")

EDIT:
Ops, my answer didn't work.
I misunderstood the question as "matching multiple condition"

by (216 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.