Problems with variable scope

BlitzMax Forums/BlitzMax Programming/Problems with variable scope

Robert Cummings(Posted 2006) [#1]
Hi, I have two includes:

;game.bmx
Type game
	Global score:Int = 0
End Type

;editor.bmx
Type editor
	Function Check()
		If game.score = 0
			Notify "ok"
		EndIf
	End Function
End Type


And then I call editor.Check() from somewhere else. The problem is, editor.Check() cannot see game.score during compilation.

The error being returned during compilation is:


Compile Error
Identifier 'score' not found



I cannot reproduce it with the example above. It tends to happen sparodically and is in my somewhat huge game. But I have boiled it down to basically this situation.

What I am asking for is advice on why this can happen, and why it could happen, and any experiences you may have had with identifiers not found.

This will help me avoid this situation in future.


Grey Alien(Posted 2006) [#2]
hmm, why not make the type called TGame, then change Global Score to Field Score, and then right at the start of your code make a single instance called Game. Then game.score should work 100% of the time. I do this.


Damien Sturdy(Posted 2006) [#3]
I was getting this recently. Ant wouldn't believe me though :P

Grey; thats ugly coding. :-) If it works, I suppose! :D


Robert Cummings(Posted 2006) [#4]
It's ugly coding... The problem here may be occuring as a genuine bug if you have a lot of variables defined within a class (or type)...

Can someone confirm this theory? Or offer an alternative...


Grey Alien(Posted 2006) [#5]
yes it sounds like a bug, but since when is creating a single instance of your game object (like Delphi's Application object) ugly coding? Just because it's not using globals part of a type? OR because your editor type depends on an instance of TGame called Game instead of a type declation called Game? Are you hung up about that? ([edit] hmm that sounds rude, it's not supposed to be, sorry)


Damien Sturdy(Posted 2006) [#6]
Yeah, sorry. Pretty useless post by me above :)


Having an instance of Tgame instead of just using a type with a function just seems messy in my head, but generally, Object Orientated Programming is like that to me. Take no offence! :)


Grey Alien(Posted 2006) [#7]
yeah I know what your mean, I guess I could have just had a type with functions and globals, but I'm not getting problems like One Eyed Jack ;-) To be fair, he shouldn't be getting problems either!


Robert Cummings(Posted 2006) [#8]
tell me about it!

I am still looking for informed guesses and advice from people with experience in defining their constants and globals within types.

I have switched from having a large vars.bmx file to declare them all, to keeping the variables and constants with the program.

I believe this allows far better coding practise, so I am looking for people with knowledge of this to post their own findings incase I am missing the obvious?

I am including the files in the right order as well...


Damien Sturdy(Posted 2006) [#9]
I have a similar problem in a current game,I'll check the cause later but basically, I was trying to access a global from the same source file, but a different type. It did exactly as you mentioned above- and failed to find it.


Grey Alien(Posted 2006) [#10]
Actually I think that either all your true globals (not type globals) should be in a separate file, not with the main program, to avoid clutter.


Dreamora(Posted 2006) [#11]
any chance that the editor type has a field / global called "game" itself as well?


tonyg(Posted 2006) [#12]
... or the global variable is defined within a function in the type rather than in the type itself?