If ... Then func() Else ...

Archives Forums/Blitz3D Bug Reports/If ... Then func() Else ...

octothorpe(Posted 2006) [#1]
Here's a fun one for the BRL team! :D

If blah Then Print Else foo() ; fine, as expected
If blah Then foo() Else Print ; SYNTAX ERROR: Expecting expression?!

Function foo()
End Function

I can't believe I haven't run into this until now!

Matty(Posted 2006) [#2]
I have noticed something similar, but have not tried to pin it down yet. Next time I come across it I will post some code, but it usually results in me having to split the if.then.else statement into multiple lines with if...else...endif.

Matty(Posted 2006) [#3]
Here is an example taken directly from the code of my current game:

this code works:
If OtherUnit\IsPlayer Then 
AddSoundToPlayList(OtherUnit.UnitObject,"body impact")

this code does not:
If OtherUnit\IsPlayer Then AddSoundToPlayList(OtherUnit.UnitObject,"body impact") Else AddSoundToPlayList(OtherUnit.UnitObject,"impact")

does it in both blitzplus and blitz3d (haven't tried it in blitzbasic but probably not much point testing that one..)

octothorpe(Posted 2006) [#4]
I've simplified your test case. Your code is causing a problem because you're calling a function with two arguments.

If blah Then bar() Else Print ; Expecting Expression
If blah Then bar(1) Else Print ; works fine!
If blah Then bar(1,2) Else Print ; Expecting ')'
If blah Then bar(1,2,3) Else Print ; Expecting ')'
If blah Then bar(1,2,3,4) Else Print ; Expecting ')'
If blah Then bar(1,2,3,4,5) Else Print ; Expecting ')'

Function bar(x1=0,x2=0,x3=0,x4=0,x5=0)
End Function

As illustrated above, the Blitz3d parser chokes on single-line IF..THEN..ELSE statements where the THEN substatement is a function call with (a) any number of parameters other than one and (b) an ignored return value.

big10p(Posted 2006) [#5]
Weird. Is this a problem specific to the 1.93 update? I probably haven't hit it before because I hardly ever use If/Then/Else on a single line.

Teddyfles(Posted 2006) [#6]
I think it's an old problem. I remember having the same problem long ago when I tried:

If blah Then Color(255, 255, 255) Else Color(0, 0, 0)

octothorpe(Posted 2006) [#7]
I know 1.91 also suffers from this issue because I upgraded from that to see if it'd go away.

Regardless of whether it's an old problem this parser bug should be fixed. :)

JazzieB(Posted 2006) [#8]
If you have a function that doesn't return a value, then you can call it by taking out the brackets. I know some people prefer to use the brackets all the time, but I prefer to have these sort of functions 'look' like any other command.

If a=10 Then Color(255,255,255) Else Color(200,200,200)
produces the error, but
If a=10 Then Color 255,255,255 Else Color 200,200,200
does not.

octothorpe(Posted 2006) [#9]
Nice catch.

Personally, I use parenthesis even with builtin commands.

jfk EO-11110(Posted 2006) [#10]
To be on the save side you may use the dedicated command delimiters:
if condition: action():else:actions2():endif
The structure is about the same.