game_start function not working, please help

Started by beomoud, Mon 28/01/2008 08:17:00

Previous topic - Next topic

beomoud

Can someone help me out. It looks like my game_start function suddentlly stoped to work, it has been working for the past few months and i haven't changed anythying in that section. Other games can use this function so it can't be a programme malfunction. Do you know what could have caused this. It seems that every line i put in this function is ignored even the simpliest once like Display ("there is no problem"); All i did i think was add a couple of variable declarations above this function, in the place where i had my own functions and an export statement in the end of this. All the other built in functions seem to work, now, has anyone else had this problem? What could i do? Is there an easy way to tranfer most of my game into a new game file without taking the problem with me? Could it be that my ags (editor settings) file has somehow bben corrupted?

Gilbert

Actually as you mentioned functions like Display(), I'm rather surprised that it worked before.

The game_start() function is executed at a VERY early stage just after launching the game, during which no rooms, sprites, etc. data are loaded yet, and the resolution of the game is not setup either, so it should only be used for simple initializations, like setting up variables, etc., anything more complicated should not work (either ignored without notice or in better cases, crashes the game).

beomoud

It didn't display anything before, i just used it as a debugging tool. But it used to define variables. These variables are now changed from everywhere else in my script but not by the game_start function, and the thing is that it used to. Now these variables start with 0 as there is nothing else defined in the script header. I should say that i have declared these variablse in the script header as i wanted all my scripts to use them, and they did, until now that is

beomoud

#3
Never mind i finally know what it was. As soon as i removed the last two variables: hour, day from my script it worked again, but i don't understand what i did wrong. This is my script if anyone could bother to look into it i'd appreciate it guys. Keep in mind that these two variables hour and day worked smoothly but then the game_start didn't, i don't get it i just went by the book, so here it goes:

Script Header:

int Stealing;
int Lockpicking;
int Speechcraft;
int Intimidation;

import int hour;
import int day;

bool chase;
int Bounty;

int Gold;

struct Things{
int Price;
String Item_description;
int Item_damage;
int Item_defense;
};

struct NPC{
bool dead;
int awareness;
int money;
int enemy_attack;
int enemy_defense;
int enemy_health;
int enemy_weapon;
int enemy_shield;
int enemy_damage;
};

import function lockpicking (int door_defense);

import function stealing (int awareness, int money);

import function hitting (int Item_damage, int enemy_shield, int enemy_defense, int enemy_health);

import function enemy_hitting (int enemy_attack, int enemy_damege, int enemy_weapon);

int foe;

// main global script file
int hour;
int day;

function lockpicking (int door_defense) {
 (my function)
}

NPC person[10];
Things item[10];

function stealing (int awareness, int money) {
  (my function again)
}

function hitting (int Item_damage, int enemy_shield, int enemy_defense, int enemy_health) {
  (another function working fine)
}
function enemy_hitting (int enemy_attack, int enemy_damege, int enemy_weapon) {
  (same here)
}

export hour, day;
#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
player.z -= 15;

hour = 9;
SetTimer (1, 2400);
return hour;

mouse.Mode = eModeInteract;

Health = Constitution*4;
Mana = Intelligence*4;
Defense = Nimbleness + 10;

mouse.DisableMode(eModeWalkto);
mouse.DisableMode(eModeLookat);
mouse.DisableMode(eModeTalkto);
mouse.DisableMode(eModeUseinv);

Gold = 40;

(and this is where my structs begin but they too long a list to display here)

  // called when the game starts, before the first room is loaded
}
#sectionend game_start  // DO NOT EDIT OR REMOVE THIS LINE

paolo

game_start is only for doing things like initialising variables. You should not be declaring anything in there (that is, writing things like "int x"). When you say "and this is where my structs begin", do you mean you are initialising your structs? You certainly shouldn't be declaring structs in game_start.

So

Code: ags

struct MyStruct
{
	int a;
	bool b;
};


and

Code: ags

MyStruct m;


do not belong in game_start, but

Code: ags

m.a = 10;
m.b = false;


is fine, provided m has already been declared earlier on, either in the global script or in the script header.

But you probably realise this. I think the problem could be that you have put a return statement in game_start:

Code: ags

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
player.z -= 15;

hour = 9;
SetTimer (1, 2400);
return hour; 

//lots more initialisations snipped
}


which means all those initialisations after the "return" never happen.

Khris

paolo: He doesn't open game_start until the end of the script and he isn't declaring anything inside it.

But yes, return will exit the function immediately; that's why it "stopped working".
In fact, even returning a value at the end is pointless since game_start is called by AGS and thus there's no way to get the return value. (Unless you manually call it again at some point, using e.g. "int temp = game_start();".)

And just fyi, declaring a variable in the header creates a variable for the global script and separate ones for every room script.
Although it won't matter as long as you directly access these vars in the global script only.
But it's better practice to declare them in the global script, then im- and export them (as you did with some).

beomoud

The thing is that if i don't return the value for hour it doesn't get the change in my roomscript. Altough in my GUI's (which are handled by the global script) it displays that hour = 9, in my room script it used to display hour = 0. That was until it occured to me to use the return keyword ( although i admitt i haven't figured out what's the whole deal with this yet, i mean how are we supposed to use return??). If i don't use return how else do i say to my room script that by the time that the game starts the hour is 9?

paolo

#7
Quote from: beomoud on Mon 28/01/2008 18:01:35
If i don't use return how else do i say to my room script that by the time that the game starts the hour is 9?

Remove the "return" statement from the script header, then declare "hour" in the global script instead:

Code: ags

int hour;


Define it in game_start, as you have already done.

Alternatively, you can define it at the same time as you declare it at the top of the global script:

Code: ags

int hour = 9;


Whichever way you do it, you then need to export it at the end of the global script:

Code: ags

export hour;


Import it at the top of each of the room scripts that use it:

Code: ags

import int hour;


You should then find that "hour" is equal to 9 in each of the room scripts that uses it.

If "hour" is used only in one room script, then it's tidier and simpler to declare and define it at the top of that room script instead - no exports or imports will be needed.

I would suggest you do the same with all of your other variables. Declare any that are used in more than one room at the top of the global script, define them in game_start and export them at the bottom of the script; import them into the room scripts that need them. Declare and define any variables that are used in one room only at the top of that room script. Any variables that are used in one function only can be declared and defined within that function.

Khris

beomoud:
The return value is used to get a result off a function.
To do so, use the type of variable instead of the "function" keyword (which is really the same as "int"):
Code: ags
int sum(int a, int b) {
  return a+b;
}

This code declares a function called "sum" which returns an integer.
You can now use it in your code like this:
Code: ags
int result = sum(3, 5);

result now has a value of 8.

You can use strings, too:
Code: ags
String fullname(String firstname, String surname, bool list) {
  if (list) return String.Format("%s, %s", surname, firstname);
  return String.Format("%s %s", firstname, surname);
}

fullname("Jack", "Smith", false) will return "Jack Smith".
fullname("Jack", "Smith", true) will return "Smith, Jack".
Calling this function by itself won't do anything, you'd load the result into a String variable or use it directly, e.g. as parameter of a Display command.

beomoud

#9
Ok now that i've removed the return keywork and moved the import keyword from my script header to my room script it works great. But why didn't it before, i thought that you should put the import keyword on the script header if you want all your scripts to have access to it, it says so in the manual. I need this variable in all my rooms, does this mean i have to import int hour in every room i've got?

(By the way i've moved my game to AGS 3.0, it's great, the only problem i noticed is that sometimes it doesn't cut/paste a part of your script if you use right click and that i couldn't paste a code i had stored in a text folder like i used to do, anyway)

Khris

As explained before:
Code: ags
// global script

int variable;
export variable;   // export line doesn't need to be at the end, but must be after declaration

// header

import int variable;


If you did that and had the impression it didn't work, something else must've been wrong because that's the way to do it and it's gonna work 100%.

SMF spam blocked by CleanTalk