Help with VARIABLES

Non-platform specific questions.

Help with VARIABLES

Postby CoFFiN6 » Wed Sep 21, 2011 9:56 pm

Hey all!
I just need some help with variables.I have been going through other posts and tutorials but for some reason I'm not understanding how variables work and what their main purpose is.

I know that u can choose to declare one in the global code or to just wright
"Int myVar;"

If someone can please explain me a short way of declaring one.

Also I'd like to know why is there variables like Float or Double when you can just use Int .

One last thing. I've tried the var in global code.

char myWord[255]="Hello people";

And then it shows me a char * error popup..I obviously don't understand these things but I realy need to learn them.
Thanx
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score

Re: Help with VARIABLES

Postby skydereign » Wed Sep 21, 2011 10:40 pm

Okay, first your declaration wouldn't work. It has to be lower case i. With that kind of thing, you have to be picky, because it won't cut you any slack.
Code: Select all
int myVar;


If someone can please explain me a short way of declaring one.

You already explained a short way of declaring one. All you do is declare it in global code, and add the script. You can declare multiple variables in a single script. I usually have all my main variables in a global script called vars.

Also I'd like to know why is there variables like Float or Double when you can just use Int .

An int, is an integer. float and double can have decimal values. So, while an int can hold whole numbers, you won't always be able to use a whole number. A good example is if you need to store an angle in radians, using an int simply won't work. doubles can also hold larger numbers.

One last thing. I've tried the var in global code.

char myWord[255]="Hello people";

And then it shows me a char * error popup..I obviously don't understand these things but I realy need to learn them.

The reason that doesn't work, is you are using a char array. You would need to use a char* if you want to declare it like that. The only problem with using a char*, is that if you want to change the text, you'll pretty much have to keep the size of the text to equal or less than the original string. You could however set it like an array (by setting each sub [remembering of course these are chars so you need to use single quotes]).
Code: Select all
char myWord[255] = {'H', 'e', 'l', 'l', 'o', ' ', 'p', 'e', 'o', 'p', 'l', 'e'};


-Edit
I didn't actually address why they are useful. In short, they allow you to hold information, and change how the game works, based off of it. So, you can flip a variable, and change gravity. You can hold what the player's score is. With variables, you can make a game extremely dynamic. It is also required since x and y are variables, among many other useful parts of an Actor.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with VARIABLES

Postby CoFFiN6 » Wed Sep 21, 2011 11:29 pm

Wow thanx! I understand it better now.So a "int" would be single numbers like "1" "332" and the double and float "1.25" .

Ohk so if I make a global group to put in all my variables ,would I be able to access them from another group?
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score

Re: Help with VARIABLES

Postby skydereign » Wed Sep 21, 2011 11:32 pm

I assume by group, you are referring to the individual scripts? Any variables declared in global code can be accessed anywhere, with one exception. In the global script itself, if you write a function, or similar, that uses a variable declared later in the script it won't work.
Code: Select all
void
function ()
{
    textNumber = my_var;
}

int my_var;

So, the above can't, but if you separated them into different scripts, then yes.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with VARIABLES

Postby CoFFiN6 » Thu Sep 22, 2011 10:35 am

Ohk I see now.Thanx a lot and happy scriptin! :D
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest