Ho do I make a function with if?

Options
mr_blond97
mr_blond97 Member Posts: 2 Member
edited March 2022 in Scripting Workshop

I make a function that contains if:

on init
	message(Add(1,2))
end on


function Add(x,y) -> output
	if x > 0
		output := x + y
	else 
		output := 0
	end if
end function

And I get the error message:

The definition of function Add needs to consist of a single line (eg. "result := <expr>") in order to be used in this context

Ho do I make a function with if?

Best Answer

  • EvilDragon
    EvilDragon Moderator Posts: 1,023 mod
    Answer ✓
    Options

    You cannot call a function within another command like that, you need to store the output of it to a variable. So:

    var = Add(x, y)
    message(var)
    

Answers

  • EvilDragon
    EvilDragon Moderator Posts: 1,023 mod
    Answer ✓
    Options

    You cannot call a function within another command like that, you need to store the output of it to a variable. So:

    var = Add(x, y)
    message(var)
    
  • mr_blond97
    mr_blond97 Member Posts: 2 Member
    Options

    Thank you!

    on init
        declare $var
        $var := Add(1, 2)
        message(var)
    end on
    
    
    
    function Add(x,y) -> output
    	if x > 0
    		output := x + y
    	else 
    		output := 0
    	end if
    end function
    
    
    
Back To Top