How do you use if else statements in functions that return a value?

My code breaks and returns the error: “This function returns a value of type ?creative_prop, but the function body’s result is an incompatible value of type void.” This error only occurs when I use an else if statement. It works perfectly fine with an else statement.

Working Code:

SpawnPlatform2(Position:vector3)<transacts>: ?creative_prop =
        var RandomAmount: int= 0
        # places the pinball devices randomly
        set RandomAmount = GetRandomInt(low:int = 1, high:int = 3)
        if (RandomAmount = 1):
            var SpawnedProp1: tuple(?creative_prop,spawn_prop_result) = SpawnProp(PinballTrap, vector3{X := GetRandomFloat(min:float = -200.0, max:float = 200.0), Y := GetRandomFloat(small:float = -200.0, big:float = 200.0), Z := 50.0}, MakeRotationFromYawPitchRollDegrees(Pitch := 90.0, Yaw := 0.0, Roll := 0.0))
            return SpawnedProp1(0)
        else:
            var SpawnedProp2: tuple(?creative_prop,spawn_prop_result) = SpawnProp(PinballTrap, vector3{X := GetRandomFloat(min:float = -200.0, max:float = 200.0), Y := GetRandomFloat(small:float = -200.0, big:float = 200.0), Z := 50.0}, MakeRotationFromYawPitchRollDegrees(Pitch := 90.0, Yaw := 0.0, Roll := 0.0))
            set SpawnedProp2 = SpawnProp(PinballTrap, vector3{X := GetRandomFloat(min:float = -200.0, max:float = 200.0), Y := GetRandomFloat(small:float = -200.0, big:float = 200.0), Z := 50.0}, MakeRotationFromYawPitchRollDegrees(pitch := 90.0, yaw := 0.0, roll := 0.0))
            return SpawnedProp2(0)

Error Code:

SpawnPlatform2(Position:vector3)<transacts>: ?creative_prop =
        var RandomAmount: int= 0
        # places the pinball devices randomly
        set RandomAmount = GetRandomInt(low:int = 1, high:int = 3)
        if (RandomAmount = 1):
            var SpawnedProp1: tuple(?creative_prop,spawn_prop_result) = SpawnProp(PinballTrap, vector3{X := GetRandomFloat(min:float = -200.0, max:float = 200.0), Y := GetRandomFloat(small:float = -200.0, big:float = 200.0), Z := 50.0}, MakeRotationFromYawPitchRollDegrees(Pitch := 90.0, Yaw := 0.0, Roll := 0.0))
            return SpawnedProp1(0)
        else if (RandomAmount = 2):
            var SpawnedProp2: tuple(?creative_prop,spawn_prop_result) = SpawnProp(PinballTrap, vector3{X := GetRandomFloat(min:float = -200.0, max:float = 200.0), Y := GetRandomFloat(small:float = -200.0, big:float = 200.0), Z := 50.0}, MakeRotationFromYawPitchRollDegrees(Pitch := 90.0, Yaw := 0.0, Roll := 0.0))
            set SpawnedProp2 = SpawnProp(PinballTrap, vector3{X := GetRandomFloat(min:float = -200.0, max:float = 200.0), Y := GetRandomFloat(small:float = -200.0, big:float = 200.0), Z := 50.0}, MakeRotationFromYawPitchRollDegrees(pitch := 90.0, yaw := 0.0, roll := 0.0))
            return SpawnedProp2(0)

Does anyone know how to correctly use else if statements in functions that return a value? Thank You!

for a function that returns an optional, in the else statements you can return false

Thanks for the suggestion, but I want to use an else if statement inside of a function that returns a value.

I’m a little confused, you can return out of any if/else/else if statement as long as you have the data to return. In the case you are showing, you don’t have a prop to return outside of those statements. You’re already returning something from the else if statement. Also, it’s kind of hard to follow because you didn’t post the error you’re getting, you just pasted the code twice.

I posted the error as well. Here it is again, “This function returns a value of type ?creative_prop, but the function body’s result is an incompatible value of type void.” And the code is also different in each post. The first one contains no else if statements and works fine. While the second one contains else if statements and causes the error above to be displayed.

My apologies for misunderstanding- that being said my original suggestion is your solution. In order to return something in a function, all conditions must return that type. Your second code doesn’t work because if and else if can both fail, resulting in nothing being returned. You have to return false after those statements in order to make it work.

1 Like

Ok, thank you so much!!!