Types / Globals / etc

BlitzMax Forums/BlitzMax Beginners Area/Types / Globals / etc

Kanati(Posted 2011) [#1]
Getting aggravated. Things I should know seeing as how I've owned max since the day it came out. Even if I haven't looked at it in years.

So...

pseudocode:
Global GameWidth:Int = 1024
Global Pic:TImage = LoadImage("Stuff.png")

Type TObject 
    Global X:Int = GameWidth / 2 - (Pic.Width / 2)

    Function DoStuff()
        'DoingStuffHere...
    EndFunction
End Type


So I needed a variable in a UDT. We'll call it X. I tried Local and Field and DoStuff() couldn't access it for whatever reason so I chose Global (figuring Global in that context was global to the UDT itself). And that appears to be fine but it, for some reason, cannot access GameWidth which is global to the program. My brain hurts.

What's the CORRECT way of doing this because this obviously isn't it. :)


Amon(Posted 2011) [#2]
TObject.x

So if in a type:



Last edited 2011

Last edited 2011


Kanati(Posted 2011) [#3]
Ah. Got it. Thanks.

Method's can access fields without that. I assumed functions SHOULD be able to... I had tried Self.X as well but that wasn't the trick either.

In VB.NET (my normal language of choice) it would be simply Me.X or just X. This is the worst part of another language. The tiny idiosyncracies that you think you SHOULD know but don't fit into the window of knowledge you've built from other languages. :)


H&K(Posted 2011) [#4]
In VB.NET (my normal language of choice) it would be simply Me.X or just X

As it is in Bmax (well self.x or Typename.x or just x)

To be honest I am having trouble seeing what your question was, cos you pseudo code should run.

Type poopoo
   Global Big:Int = 1


   Function go()
        Print Big 
	Print poopoo.big
	Print Self.big
   EndFunction
EndType

poopoo.go

What might be your problem is "When" the image width is available , that is
cos you global field isnt pre-definable, the program needs to run and load an image, so it would need to be told WHEN to define the Global field variable, hence the answer of a create function. It isnt that the syntax was different, just that the Global had a specific creation point

Last edited 2011


Czar Flavius(Posted 2011) [#5]
I don't understand the question.

Fields are contained in every object of that type uniquely. So every alien would have an x and y field. Methods can access these fields just by name (optionally with "self" keyword). Functions cannot access fields because they aren't called on a specific object.

Globals exist only once in the whole program. For example, number of aliens could be a global variable. You only have one count. Methods or functions can access global variables just by name.

Type TAlien
	Global NumAliens:Int = 0
	Field x:Int, y:Int

	Function Create:TAlien(xIn:Int, yIn:Int)
		NumAliens :+ 1
		Local alien:TAlien = New TAlien
		alien.x = xIn
		alien.y = yIn
		Return alien
	End Function

	Method MoveUp()
		y :- 1
	End Method

	Method Destory()
		' code to remove alien from game...
		NumAliens :- 1
	End Method
End Type


To access a function or global variable outside the type..

Local alien:TAlien = TAlien.Create(5, 5)
Print "Number of aliens is " + String(TAlien.NumAliens)


Edit: This is BAD
Global Pic:TImage = LoadImage("Stuff.png")

Type TObject 
    Global X:Int = GameWidth / 2 - (Pic.Width / 2)

What happens if global variable X is set up before global variable Pic has been set up with the image you are loading? It will have an invalid value. Globals should not rely upon other globals for their initial value. If you need to, have a StartUp function which sets the value of global variables that depend on others. The order in which global variables are automatically initialized is undefined. You could make GameWidth a Const, which are set up before globals.

Last edited 2011