Bit of help - Functions

Monkey Forums/Monkey Programming/Bit of help - Functions

Gruk(Posted 2011) [#1]
Hi, could you hlp me out I'm having a little trouble getting my head around the way you code coming from Blitz.

I've got my variables setup:

Global backtiles:background
Global tiles:Int[]

In OnCreate I'm loading up my grx, then in OnRender, I'm pasting them up. But I want to move the map drawing lines of code out of there to make it easier to read.

In blitz I could just create a function, I've tried doing that in Monkey but it cant find the variables?

eg..

function drawmap()
For num=0 To xscreen*yscreen-1
If tiles[num]<>0 DrawImage backtiles.background,x,y,0,1,1,tiles[num]
Endif

Says it cant find num, xscreen etc? but I declared them as global?

Also I want to load in a 'game level data tile map', looking thro the docs I see it says it has to be in .TXT format. So does that mean I'll have to load it in, then parse for the ',' seperators - finally converting the string in INT's and putting them back into an INT ARRAY?


Samah(Posted 2011) [#2]
We really need a bigger code sample to see what you're trying to do.
Also use code tags for small chunks of code:
Example

...and codebox tags for larger code blocks:



Gruk(Posted 2011) [#3]
Ok.. its a bit of mess as it's made up of bits and pieces from everyone's examples



It works.. but I want to move the map drawing routine out of the way.


Goodlookinguy(Posted 2011) [#4]
You declared those variables as global in the scope of a class.

Therefore, num does not exist, but Game.num does.


Jesse(Posted 2011) [#5]
num and num2 are fine as long as they are in a method or a function with in the class they were created in.

anything outside of the class will need a reference to the class before any of it's global variables can be used such as "Game.num" etc..
Anything within the constraints of a class are only accessible to the class and/or to its instance.
to access anything on the class from outside of the class the class name(for globals and constants) or its instance name followed by a period and the name of the variable(global,constants and fields) must be used.


Jesse(Posted 2011) [#6]
double post.


Gruk(Posted 2011) [#7]
Sorry guys.. bit too techie for my understanding, best I grab some C++ books. I'm trying to get my head around classes using nothing but the examples :(


FlameDuck(Posted 2011) [#8]
Why would you grab some C++ books? If you want to read some good books about Object-Oriented Programming, a C++ book is practically useless.

Good books on the other hand are Bruce Eckel's fantastic "Thinking in Java" books, the third edition of which is available in electronic form, from here: http://www.mindviewinc.com/Books/downloads.html

Another good book on the subject is Zed A. Shaw's "Learn Python The Hard Way" which you can also read for free online or purchase through vanity press from here: http://learnpythonthehardway.org/

In either case what Jesse and Goodlookinguy said is basically it. The concept you're looking to study is called either "Encapsulation" or "Information hiding" depending on which book you're reading although to most non-academics the distinction is academic.

The short version is that it protects you from unintentionally breaking your software by accidentally changing the software's state to an "illegal" or "inconsistent" state.

The slightly longer version is that if you ever find yourself needing to break encapsulation, your software design is fundamentally flawed, and you should reevaluate what kind of responsibilities the different parts of your software has, so as to eliminate this as possible cause of rather severe bugs or unexplained (and unintentional) behavior.