Globar var vs type

Blitz3D Forums/Blitz3D Beginners Area/Globar var vs type

Mike0101(Posted 2006) [#1]
Is better using type instead of global variables?
More efficiency or only better structured?
I see the Mario sample and the writer (Mark) uses almost always types. (Camera, player with one element )
When are more uniform elements must use type this is true.


semar(Posted 2006) [#2]
Is better using type instead of global variables?

It really depends on the context.

If you need a global variable that is specialized to a single purpose, for example game status, then go for global.

If you instead need to track a bunch of (global) variables that describe a list of properties which belong to an entity, for example the camera in that Mario sample, then go for types.

Type variables - merely type fields - have also an advantage compared with Globals; not only they are already globals, they work also ala 'Option Explicit' in VisualBasic, because they force you to type the correct variable name, otherwise the compiler refuses to compile, and brings the pointer to the offending line.

With normal global variables, you may type a wrong variable name, and the compiler does not complain at all !

Example:
'declare a global variable
Global Mylongnamedvariablewhichcanbewrongspellt = 5

'declare a type and use its fields like global variables
Type T_Glob
Field Mylongnamedvariablewhichcanbewrongspellt 
end type
'create an instance
Glob.T_glob = new T_glob

'initialize the field variable
Glob\Mylongnamedvariablewhichcanbewrongspellt = 5

'Now let's show a classic typo
print Mylongnamedvariablewhichcanbewromgspellt '<-- there's a typo here, can you see it ?
print Glob\Mylongnamedvariablewhichcanbewrongspellt 

'Output: 
'0 ---> because the variable was wrong spellt (wroMg instead 'of wroNg)
'5 ---> right !

'And of course, this statement:
Print Glob\Mylongnamedvariablewhichcanbewromgspellt
'won't compile !


Sergio.


Mike0101(Posted 2006) [#3]
Thank you, it's clear