There can be only one match per function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dumuz

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.

:bust_in_silhouette: Reply From: Zylann

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.

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.

Dumuz | 2020-04-02 22:30

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

Zylann | 2020-04-02 22:32

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.

Dumuz | 2020-04-02 22:39

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

Zylann | 2020-04-02 22:40

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.

:confused:

Dumuz | 2020-04-02 22:50

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.

Dumuz | 2020-04-02 23:17

:bust_in_silhouette: Reply From: imjp94

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”