Blitz3D+ Command Reference

Local variable[=value][,variable2[=value2],...]

Parameters

variable - the variable's name, with an optional type tag ($, #) or custom Type

value (optional) - starting value; 0, 0.0, "" or Null (default) depending on the type

Description

Declares a variable belonging to the function or main program it is written in.

Local is the counterpart of Global. The variable exists only inside the Function that declares it, and vanishes when that function returns - which is exactly what you want for loop counters, temporary distances, a scratch string you are building up.

Strictly speaking you rarely have to write it, because any variable you assign that is not global is local anyway. What Local buys you is the initializer and the intent: Local hits = 0, angle# = 90.0 says plainly that these are working values, and the next person reading the function does not have to check whether a global of the same name exists somewhere.

Every call gets its own fresh set of locals, which is what makes recursion work - a function that calls itself does not tread on the copy belonging to the outer call.

A local with the same name as a global hides the global for that whole function, so if a function suddenly cannot see the score any more, look for a stray Local score. And a Local statement inside a loop does not create a new variable each pass; its initializer simply runs again every time execution reaches that line.

See also: Global, Const, Dim, Function.

Example

; Local Example
; -------------

; A Local variable belongs only to the code that declares it
Local lives=3

Print "In the main program, lives is "+lives

ShowFunctionLives()

; The function used its OWN lives variable - ours is untouched
Print "Back in the main program, lives is still "+lives

Print ""
Print "Press any key to close the example"
WaitKey

End

Function ShowFunctionLives()
    ; This lives exists only inside this function
    Local lives=99
    Print "Inside the function, lives is "+lives
End Function

Index