problem with array, again (solved)

Started by riseryn, Wed 19/12/2007 22:32:43

Previous topic - Next topic

riseryn

Hi
in global script i have this code :
Code: ags

int city[4];
city[0]=1;
city[1]=0;
city[2]=0;
city[3]=0;
export city;

when i run the programm i got a parse error unexpected city (at the first line city[0]=1;)

I don't understand why and i found nothing on the forums about this error.
Someone can explain me?
Thanks


Ubel

I think you can't define the value of a variable outside of a function. This means that you should put lines 2-5 of that piece of code into game_start.

Ashen

Just as Pablo says.
You can set an individual int at declaration outside of functions (int city = 1; would work, but not int city; city = 1;), but members of an array - and other variable types, e.g. String - have to be set in a function (e.g. game_start). Check the BFAQ for more details on how/where you can set variables - although it doesn't mention arrays.

Also ints default to 0, so you'd be able to skip lines 3-5 altogether in that example.
I know what you're thinking ... Don't think that.

riseryn

#3
Both, thanks for your answers.

I have created 2 functions in global script
first one to set the default parameters
Code: ags

function create_city()
{
int city[4];
city[0]=1;}


The second to modify the parameters
Code: ags

function set_city(int city_number, int city_status)
{city[city_number]=city_status}// where city status= 1 city visible

In game_start
Code: ags

create_city();


But when I run testgame I have an error undefined token city for function set_city.
I have read the Bfaq but find (or understand?) nothing about that problem.
i really don't understand the way AGS handle this :(

Gilbert

If you declare a variable (that includes any variables, int, String, arrays, etc.) inside a function the variable is local to that specific function only, so it will be created whenever the function is called and be destroyed when it ends.

You need to move the declaration lines outside of functions, preferrably on the top part of the script.

Khris

Like this:

Code: ags
int city[4];
export city;

function game_start() {
  city[0]=1;
}


Getting accustomed to writing your own functions is always a good thing, but using one to set a city is overkill here.
Why "set_city(2, 1);" instead of "city[2]=1;" ?

riseryn


SMF spam blocked by CleanTalk