Local variable outside a function/method
BlitzMax Forums/BlitzMax Beginners Area/Local variable outside a function/method
| ||
Hi all, first post in the forums. I recently bought BlitzMax since I believe it is a pretty powerful tool. Now my question is this: I am not unfamiliar with programming, particularly object-oriented programming. I do understand the concept of types/methods, and single functions quite well. However - for example in C or Objective-C (and probably other languages), I can define a variable in a Header, so that it is sort of restricted to a certain part of the code. Of course, I can also define global variables accessible anywhere. How can I do this in BlitzMax? Or - in which way do Local variables work? Consider this: Function myFunction:Int() Local myCoolInt:Int = 5 Print myCoolInt Return 0 EndFunction Fairly obvious. But now, what happens if I have a Local variable that is inside a BMX file but outside of a Function? Will this variable be accessible to other BMX files? Example: Local myCoolInt:Int = 5 Function myFunction:Int() Print myCoolInt Return 0 EndFunction If the Local can now be accessed from other BMX files, it's the same as a Global - no? I'm just trying to get my head around this as a newbie. Please be gentle, I'm a newcomer :D Cheers Marcus Last edited 2012 |
| ||
Will this variable be accessible to other BMX files? Yes, it will. If the Local can now be accessed from other BMX files, it's the same as a Global - no? No, it won't be accessible from within functions or class methods, whereas a Global would be. |
| ||
Thanks very much GfK :) |
| ||
Hiya, Welcome to the forums :) Just to add to Gfks reply... Depending on how you are connecting your various .bmx files together, you can choose whether some Global and Local variables are accessible to other files. For eg - Say you are using the Include "sprites.bmx" command in your main file then the included file and the 'main' file will be treated as 1 single file, giving you the scope access scenario as Gfk has already said. If you decide to use Import "sprites.bmx", you can actually restrict Global and Local variables/types to that single .bmx file by wrapping them in Private and Public statements. It's always easier with an example :D (assume these 2 files are in the same folder ) [main.bmx] Strict Import "sprites.bmx" Print NumOfSprites 'This IS accessible in this file from the Imported file Local Sprite:MySprite = New MySprite 'NOT accessible in this file because of Private in the Imported file SpriteList = New TList 'NOT accessible in this file because of Private in the Imported file [sprites.bmx] Strict Private Global SpriteList:TList 'Only accessible in this file Type MySprite 'Only accessible in this file Field VariousFields Method VariousMethods() EndMethod EndType Public Global NumOfSprites:Int = 20 'Global access to the WHOLE application. |
| ||
To add a note of caution to the above answers. You dont really want to have locals (or dare I say it even Globals NB) available across different files. Firstly its anoying after a bit to have the deceleration of variables so far away from their use. And you hit the snag that you are committing that name (ie numofsprites) across the entire scope. NB This is not advice to NOT use Globals across files, just advice to consider a "one instance" Class/Object with all the globals you want in it. (ie a singleton). That is GameGlobal.AGlobal |
| ||
Quote col: Depending on how you are connecting your various .bmx files together, you can choose whether some Global and Local variables are accessible to other files. For eg - Say you are using the Include "sprites.bmx" command in your main file then the included file and the 'main' file will be treated as 1 single file, giving you the scope access scenario as Gfk has already said. If you decide to use Import "sprites.bmx", you can actually restrict Global and Local variables/types to that single .bmx file by wrapping them in Private and Public statements. Wow thanks, this really did help me! This is just what I needed to get my head around at, and is pretty much the equivalent of "local" headers (if you know what I mean). This will help my project immensely. Thanks :) |
| ||
@SystemError51 Glad I could help ;-) @H&K Good advice and I totally agree. I've used that very same type of scope arrangement myself. I guess I could have chosen a better example, NumOfSprites wasn't a good one really but it did demonstrate the concept ;-) |