First let me explain this is for a video game and the code is nearly identical to C++, it does not support signed, unsigned, short, or long, and I think char but aside from that it pretty much is the same. One thing I find baffling however is assigning values.
Could someone please tell me what exactly does it do for example when I declare int x; at the start of the program? Why can't I just declare x "on the fly" later on in the program when it comes time to use it? Then I don't understand what does it mean if I declare something static, or string, or static string, or static int?!?!?! I have tried to look at the many example programs(scripts as they are called in the game) to see some kind of pattern but for the life of me I can't get it. Sometimes they declare things at the beginning of the script and never even use them anywhere in the script, sometimes they declare something at the start, then declare the exact same thing again later on when it comes time to use it, and sometimes they just declare it "on the fly" as I say right then and there when it comes time to execute that part of the script.
ex:
unit_id = soldier; //then they will run the conditions
if (unit_id < 1) {
create unit_id;
}
Can anyone shed some light on what these commands mean and give me some examples of why I would want to declare a value with int, static, string, etc as opposed to the other options. Thank you.
I don't really know C, but I read that you are forced to declare your local variables at the start of a function. If you have to, then that syntax is probably inherited from C, or perhaps is some limitation in the engine.
A local static variable keeps its value between calls to the function:
1 2 3 4 5 6 7 8 9 10 11 12
int f(){
staticint a=0;
return ++a;
}
f(); //1
f(); //2
f(); //3
f(); //4
/*
Static variables are initialiazed only once, so the function doesn't keep
returning 1 forever.
*/
Strings are probably managed strings (e.g. string s="Hello,"; s+=" World!";). It'd be odd for a high-lever language to used character arrays as strings.
I don't know what to tell you about multiply declared variables. In C++, this is legal:
1 2 3 4 5 6
int a=4;
printf("%d",a); //prints 4
{
int a=8;
printf("%d",a); //prints 8
}
Perhaps you should consult the language reference.
1 2 3 4
unit_id = soldier;
if (unit_id < 1) { //<-- You just lost me.
create unit_id;
}
Yeah, kind of strange...um, maybe "soldier" is like a const int, and 1 is the "first" unit type, if it less then 1 (maybe because soldier is defined differently?) then it creates a new unit_id for it (since it doesn't exist (<1 not defined by the game?))
And maybe the variables that are declared but aren't used ARE actually used (but are just data for the compiler/game/whatever to read and interpet)
OK, my fault for giving such a crude example, just quickly the game is called Rise of Nations, if you've played games like Age of Mythology, Age of Empires, Command and Conquer, it's kinda like that, you build up army's, try to kill the other guy's army's. Now in this game you can write scripts(programms) that can control the behavior of the various military units, buildings, nations, what have you. So you start out with a section that let's you declare variables, you then have a section they call the main script body where you can insert all the various commands you want the game to perform as it runs through your scenario, create units, change alliances, destroy buildings, stuff like that, and a script can be hundreds, even thousands of lines long. So let's say at 20 min into your game you want your team to get 10 tanks at a specific x, y co-ordinate on the map, and your team is team 1, you would set a timer at the beginning of the game for 20 min, you can name it anything you want but I'll just call it Tanks1, then in the main body of the script you would write:
if (timer_expired("Tanks1")) {
create_unit_upgrade(1, 30, 120, "Tank", 10);
}
so that would create the effect of placing 10 Tank units for team 1 at x coord 30, y coord 120 when the timer expires. This is just a small example and ofcourse those commands are specific to the game not C, but I'm just trying to illustrate that is uses the same "if" this happens, "do this" type of structure. But that's not the part I'm having trouble with, my problem is I don't understand what is the difference between declaring things at the start of the program, and also in what instances should I declare them, int, static, string, etc vs the other or just declaring them within the function? Hope this clears it up or maybe I just confused you more, I have consulted the "guide" given for the game and as I say I understand pretty much all of the functions related to the game my problem is just with the whole assigning values to things and when, where, how I should do it. Thanks for trying to help.
Oh. My bad. I read soldier as "soldier" (i.e. a string literal instead of a variable name), for some reason. I didn't see the scope issue.
static is not a data type, as I explained earlier. It's a keyword that defines the scope of the variable. You would use static when you need the variable to not lose its value between calls.
int lets you store whole numbers: 1, 100, -52, etc., but not 47.3.
string lets you store text: "Player 1", "New Game", etc. Some string types let you store Unicode.
There's probably a type float or maybe real or single, to store fractional values, like 47.3, .1, -10.24, etc.
OK, I think I'm kinda getting it now, so let me just ask this then:
At the start if I do this:
int x;
What exactly does that do to x? and what if I say said
static int x;
or what if I just said
x = 5;
or what if I just declare x to be 5 somewhere later on in the runtime of the program?
If I understand you right string is a way to assign a text value so then what does static string do? Sorry for all the questions but I even have books on C++ and have tried to read the sections pertaining to this stuff but I just wish someone could tell me in plain English what int does, what static does, what static int does, and here is a relevant instance where you would declare something using int, or here is a an instance where you would want to use static int, etc, etc. I can usually pick things up pretty well if someone points me in the right direction but I just can't find any tutorial explaining the difference. Thanks again for the help.
int x;
This declares x to be an int. As simple as that. It tells the compiler/interpreter "x is a number. From now on, treat all references to x as numbers".
You would use static when you need the variable to not lose its value between calls [to a function].
I don't know what happens if you use x without declaring it. It depends on the language. If you are forced to declared variables, most likely the line will be ignored or the whole script will be rejected.