Cannot call function inside match ?

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

So I have the following code, I am not sure what I did wrong.
If I just use print, it would work. But when I used function, then nothing happens.
I remember I was able to call function before using match…am I doing something wrong here??

extends Node2D

func _ready():
	var ran_val = floor(rand_range(0,3))
	match (ran_val):

func fun_1():

func fun_2():
func fun_3():
func fun_4():
:bust_in_silhouette: Reply From: Zylann

Your code doesn’t work either with print.
This is really confusing because the equivalent code using ifs works perfectly.
The reason why match doesn’t work here is because ran_val is a float value, but you check against int values, and somehow, it considers they are different, while an if correctly finds they are equal.
I wonder if it’s a bug in the implementation of match or if it was intented (either way, it’s confusing).
See Implicit type conversion from Real to Int differs between branch statements · Issue #21210 · godotengine/godot · GitHub

You can fix your code by doing this:

var ran_val = int(floor(rand_range(0,3)))

Or this:

var ran_val = randi() % 4

also to note: typecasting to int will automatically floor the value (at least for positive numbers), so you can omit the call to floor() and save yourself a little trouble.

Nobuyuki | 2019-08-08 14:46

Thank you very much. I feel really stupid now…lol. Funny thing is I do remember something about converting a value to an int , I just didn’t remember doing it in code

lowpolygon | 2019-08-08 23:27