question from a monkey beginner

Monkey Forums/Monkey Beginners/question from a monkey beginner

pit(Posted 2014) [#1]
Hi all,

maybe a stupid question but I try... :-)
- why use GLOBAL decalration rather than FIELD in a class ? Is there a difference which can explain that we have to choose this or that ?
- why use FUNCTION in a class rather than a METHOD ? Is there a difference which can explain that we have to choose this or that ?

thanks

Pit


Supertino(Posted 2014) [#2]
example

Class Whatever
   Global data:String="hello"
End Class

Print Whatever.data ' will print "hello", you don't need an object associated with the class, can can call the class directly.


You could go further and put a function in there

Class Whatever
   Global data:String="hello"
   Function SetData:Void(t:String)
        data = t
   End Function
End Class

Whatever.SetData("World") ' change data (global) to say "World"


I do this when I don't need to create an object but I want group lots of functions and variables. Then I can simply call the Class name and get a handly list of funcs and vars show up via intelliprompt

The Field method way would be

Class Whatever
   Field data:String="hello"
   Method SetData:Void(t:String)
        data = t
   End Method
End Class

myObject: = New Whatever ' I now have to create an object to access the class methods and vars

myObject.SetData("World")

Print myObject.data



You can mix and match to good effect

Class Whatever
   Field data:String="hello"

   Global Red:Int=1
   Global Blue:Int=2

   Method SetData:Void(t:String)
        data = t
   End Method

   Method SetColor:Void(color:Int = Whatever.Red)
      select color
          Case Red; SetColor 255,0,0
          Case Blue; SetColor 0,0,255
          Default; SetColor 255,255,255
   End Method

End Class

myObject: = New Whatever

myObject.SetData("World")
myObject.SetColor(Whatever.Blue)
DrawText myObject.data,0,0 ' draw blue text to the screen



ziggy(Posted 2014) [#3]
Field --> Each instance (each copy) of the class created with New will have it's own value of that field.
Method --> When you call a method, you do it over an instance of that class (a copy of it created by "New"). That allows you to modify that class instance's fields.

Global --> It is not tied to any specific instance of the class. All class instances, share this data.
Funciton --> It is not tied to any specific instance of the class, so you can't access a field directly form the function body.

Let's say you have a class that represents an "Alien".

You coud define it like this:

Class Allien
    Field x:Float = 0
    Field y:Float = 0
    Field image:Image
End


What does it makes? Each instance of an allien will have its own x and y fields.

Example:

    Local myAllien1:=New Allien
    Local anoterAllien:= New Allien
    myAllien1.x = 20
    anoterAllien.x = 40


In this example, myAllien1 and anoterAllien have different values for their "x" field. Say you have have 1000 alliens, each one would have its own position defined by x and y fields.

So, when to use a global?

when you need information to be "shared" in all instances (all different copies) of a given class.

Example:
Class Allien
    Field x:Float = 0
    Field y:Float = 0
    Field image:Image
    Global countCreatedAlliens:Int = 0
    Method New()
        countCreatedAlliens += 1
    End
End

Notice that the "New()" method is automatically called every time a new instance of a class is created.
With this in mind, in this sample, in addition to having a dedicated x and y field for each Allien, we have a global -shared- variable that increases 1 each time a new allien is created. That's possible because ALL allien instances share the countCreatedAlliens variable, because it's a global.

Usually, you prefix global and function calls by the class name, and you have to prefix fields and method calls by the instance name. You'll find people prefixing globals and functions by an instance name. this is allowed in Monkey, but a very bad standard, as it looks like you're accessing a "self" class member, but you're not, you're accessing a shared (global if you preffer) member of the class that ALL instances of that class are using.

hope it's better understood this way.

Now function vs methods.

Methods are called in the class instance. That is, from a method you are running from the class instance. That is, in a method of Allien, you coul modify the allien's x and y fields. On a function, you only have access to the shared (globals, functions and consts) of a class. In other words, a function inside a class is exactly the same as a function outside a class. The only difference is that, when it is inside a class, you "know" it has some kind of usage relation to the class it is put in. It's just a matter of conceptual organization of code. As an example, if you have a function to calculate the distance between 2 Alliens, you could put this function inside the Allien class to organize your code.


Gerry Quinn(Posted 2014) [#4]
" You'll find people prefixing globals and functions by an instance name. this is allowed in Monkey, but a very bad standard, as it looks like you're accessing a "self" class member, but you're not."

I think it's legitimate sometimes for read-only elements. You might not care whether alien.image is shared or instanced, for example.


ziggy(Posted 2014) [#5]
I think it's legitimate sometimes for read-only elements. You might not care whether alien.image is shared or instanced, for example.
That's true. but in my honest opinion, having context dependent code leads to maintenance costs in the long run. I would prefer if Monkey did not allow it in the first place. But I suppose it's more related to coding style or preferences.


pit(Posted 2014) [#6]
Guys,

Thanks A LOT for your explanations !!!
Indeed, it's now crystal clear for me !

Pit


navyRod(Posted 2014) [#7]
Nice Community support --