What is wrong with this code???

Blitz3D Forums/Blitz3D Beginners Area/What is wrong with this code???

DNielsen(Posted 2004) [#1]
Function LoadLogo()

		Logo_Pointer = LoadImage ("logo.png")
		If (Logo_Pointer) <> 0 Then QuickExit 
		Else RuntimeError "Failed to find & load required image file"
		End If
.QuickExit
End Function 


I keep getting "ELSE without IF" ???????


Ash_UK(Posted 2004) [#2]
try this:

Function LoadLogo()

Logo_Pointer = LoadImage ("logo.png")
If (Logo_Pointer) <> 0
goto QuickExit
Else
RuntimeError "Failed to find & load required image file"
End If

.QuickExit
End Function

See if that works :)

DeViL


DNielsen(Posted 2004) [#3]
it does not work ...


Ash_UK(Posted 2004) [#4]
Hmmm, are you still getting that message "Else without EndIf"?


Bremer(Posted 2004) [#5]
Loading an image inside a function will not make it available to the rest of the code, as variables used within functions are local unless set as globals prior to calling the function. I suggest you do something similar to the following code, which will allow you to specify a filename and decide which variable will be the handle of the image.



Also, when making functions, try and make them as flexible as possible so that they can be reused. This will allow you to have less code but maintain usability. Like with the above function, you can use it to load and test as many images you want, but with the code in the top of this thread, you are limited to just loading one image into one variable without any flexibility. Coding with flexibility and reuse in mind will speed up future coding projects as function modules can be reused and doesn't have to be coded from scratch.

[edit: added the above text]


DNielsen(Posted 2004) [#6]
you example above works, @Devil. THANKS
I did not have the "goto" command, I thought Blitz could "jump to" a local label by just having the labelname and no "goto"

you solved my problem. Thanks


DNielsen(Posted 2004) [#7]
And by the way, I always use GLOBAL pointers :-)


soja(Posted 2004) [#8]
From your original code, instead of this:
Then QuickExit

...you could have said:
Then Return



DNielsen(Posted 2004) [#9]
@soja
"RETURN" ??? where does that command come from? and where does it return? I assume it equels exiting the function?

*** EDIT ***
ah, just checked the DOCS for the RETURN command. It all makes sense now. Thanks @Soja


WolRon(Posted 2004) [#10]
This:
Function LoadLogo()

		Logo_Pointer = LoadImage ("logo.png")
		If (Logo_Pointer) <> 0 Then QuickExit 
		Else RuntimeError "Failed to find & load required image file"
		End If
.QuickExit
End Function
can all be simplified to this:
Function LoadLogo()
	Logo_Pointer = LoadImage ("logo.png")
	If Logo_Pointer = 0 Then RuntimeError "Failed to find & load required image file"
End Function



DNielsen(Posted 2004) [#11]
@WolRon
Thanks for your version :-)


Sir Gak(Posted 2004) [#12]
By the way, I think the correct closing of the IF-ELSE- nest is not "End If" (two words) but "EndIf" (one word).


DNielsen(Posted 2004) [#13]
@Sir Gak
According to the documentation, "End If" and "EndIf" are 100% the same. BlitzSupport??


N(Posted 2004) [#14]
EndIf and End If function the same.


DNielsen(Posted 2004) [#15]
@Noel
Thanks for clarifying this