Problems with variable scope
BlitzMax Forums/BlitzMax Programming/Problems with variable scope
| ||
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. |
| ||
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. |
| ||
I was getting this recently. Ant wouldn't believe me though :P Grey; thats ugly coding. :-) If it works, I suppose! :D |
| ||
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... |
| ||
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) |
| ||
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! :) |
| ||
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! |
| ||
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... |
| ||
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. |
| ||
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. |
| ||
any chance that the editor type has a field / global called "game" itself as well? |
| ||
... or the global variable is defined within a function in the type rather than in the type itself? |