type functions and globals

BlitzMax Forums/BlitzMax Programming/type functions and globals

Najdorf(Posted 2005) [#1]
A function in a type is just as a function outside? Who can access a global in a field?


semar(Posted 2005) [#2]
A function in a type is just as a function outside?
Sort of. You refer to it with the syntax:
type_name.function_name

Example:
Type test

'Global
Global myglobal

'Constant
Const speed = 5

'Field
field x,y

'Method
method Move()
x = x + speed
end method

'Function
Function MyFunc()
print "Hello"
end function

End Type


Now, you refer to the function MyFunc in this way:
test.MyFunc()

Who can access a global in a field?

If I get what you mean, you can access any type global variable in the same way you access a type function, so:

print test.myglobal

The main difference between Methods and Functions is that a Method can be fired only when you have an instance of the type, because a Method works with type elements, so:
test.MyFunc() -----> Correct call to a function
t.Move -----> this gives a compile error

'==============================
test.MyFunc() -----> Correct call to a function
t:test = new test ----> New instance created
t.Move -----> Correct call to a method


In the same way are Type Fields and Type Globals or Constants: Fields can be set or accessed only when you have an instance of the type, while Global and Constants can be always accessed - that is, you don't need an instance of the type to access it:
Print test.myglobal
Print test.speed


There's some good tutorial around here, check it out for example:
http://www.blitzbasic.com/Community/posts.php?topic=42519

Hope that has sense for you,
Sergio.


Najdorf(Posted 2005) [#3]
grazie sergio!

Matteo


semar(Posted 2005) [#4]
Non c'e' di che !!

:)