Have a bit of experience programming in Python, and was wondering what the purpose of "match" was. Isn't it a redundant function, since if/elif/else does the same thing? Does it have behind-the-scene things going on which improve its functionality, speed or whatever?

Thank you.

Hello, as the doc explains : A match statement is used to branch execution of a program. It’s the equivalent of the switch statement found in many other languages, but offers some additional features. https://docs.godotengine.org/en/3.1/getting_started/scripting/gdscript/gdscript_basics.html#match

Switch/match is more compact than if else and therefore, and more readable. If you use 'continue' between two match, you can fall through to the next match. Match only accepts some data types.

Read the docs. There isn't anything there that indicates functionality or optimizations that using match provides over if/elif/else, which tells me that the only difference is readability. You can do the exact same thing with if statements by testing if a thing is a member of a data structure, is a certain value, or whatever.

Don't need nested IF statements for checking membership, if the "in" operator has this functionality without nesting anything, right?

For instance...

some_random_var = [list_of_things_you_check_for] IF thing_you_are_checking IN some_random_var: . . do_thing() ELIF thing_you_are_checking NOT IN some_random_var: . . do_other_thing() ELSE: . . more_things()

So if the switch function is more efficient than that, how is it? If behind the scenes the switch function operates exactly like that, why have one?

Just curious and want to know all the things.

if a
  if b
    if c
      ..
    elif d
      ..
  elif e
    if  f
      if g
        ..
else
  ..

^ Might as well use switch/match instead, should be better. But in your example, yeah, ifs and elses should be fine.

Okay, and, thank you. Appreciate it.

Think you could flatten quite a few nested IF statements by using AND/OR conditionals, like so:

IF a AND b AND c AND d: . . do() ELIF a AND b AND c: . . dont() ELIF a AND b: . . warn()

etc.

Unrelated to the match statement, I am not used to declaring variables. Seems optional in some cases from what I read in the docs, in order to speed things up a bit, but still a foreign concept coming from Python. Having trouble grasping the concept of ENUM, too.

4 years later