Variables Not Changing [Updated]

Started by FinalSin, Wed 26/07/2006 17:34:14

Previous topic - Next topic

FinalSin

Right. The problems are getting simpler and simpler, it's so silly!

When you initiate dialog with a character, AGS runs this script:
Code: ags
character[EGO].Walk(60, 130, eBlock);
if(Fez == 0)
		RunDialog(0);
else
		{
		if(Fez == 1)
			{
			RunDialog(5);
			}
		else
			RunDialog(7);
		}
}


I can't get it to recognise the Fez variable equalling anything except 0. I change the Fez variable to 1 more than once, and I'm sure those triggers are working. But it's not registering with this script. The Fez variable gets changed to 1 twice in another room. Fez is initialised at the top of the global script, and exported. It is imported in the Main Script Header.

I have no idea where the bug is.
The Blue Casket

Alynn

Code: ags
character[EGO].Walk(60, 130, eBlock);
if(Fez == 0) {
  RunDialog(0);
}
else if (Fez == 1){
  RunDialog(5);
}
else {
  RunDialog(7);
}


In this case there is no reason to use nested if's, using else ifs work much better.
However your issue seems to be with your brackets, it looks to me as if you have your code blocks wrong.

FinalSin

You mean the curly brackets? Or the normal ones?
The Blue Casket

Khris

Code: ags
Ã,  character[EGO].Walk(60, 130, eBlock);
Ã,  if(Fez == 0)Ã,  RunDialog(0);
Ã,  else {
Ã, Ã,  Ã, if(Fez == 1) {
Ã,  Ã, Ã,  Ã, RunDialog(5);
Ã,  Ã,  }
Ã, Ã,  Ã, else RunDialog(7);
Ã,  }
}


I've cleaned up your code a bit.
Alynn's is a lot more readable, but they do exactly the same thing, although there's a mysterious closing bracket at the end, but that's probably a copy&paste issue.

Sidenote:
You don't need { and }, if there's only one command inside.
So the best way to code this would be:
Code: ags
if (Fez==0) RunDialog(0);
else if (Fez==1) RunDialog(5);
else RunDialog(7);


Basic debugging:
Insert Display("%d", Fez); commands to check Fez's value.

FinalSin

#4
Thanks both of you, I was unsure whether it accepted 'else if's. But surely that was just readability? I still have the problem? I'll try that debug when I get up tomorrow. Thanks.

UPDATE - I've checked the value of the variable, and it doesn't change at all. It's called in the following places:

Code: ags
// main global script file
   int Fez = 0;
    export Fez;


Code: ags
// Main header script - this will be included into every script in
// the game (local and global). Do not place functions here; rather,
// place import definitions and #define names here to be used by all
// scripts.
import int Fez;


And that long string of elseifs, and one run_dialog script. But I don't think they're the problem. Somehow, the variable Fez is being reset.
The Blue Casket

Maverick

Yup! As far as I know it will be reset to Fez = 0 every time because that is what you are telling it to do.

// main global script file
Ã,  Ã, int Fez = 0;// You are assigning the zero value
Ã,  Ã,  export Fez;

Just do

int Fez;
Export Fez;

You then need to assign a value to it at the beginning of the room script (room you're using it in) and/or use Fez as a counter of sorts to change the response every time you interact with something.


Look at CHANGING VARIABLES in the tutorial. It should help you out with the rest of the code

(I assume that it is in global script because the same dialogue is used in different rooms?)



strazer

If you put
  int Fez = 0;
outside of any functions, I don't think it gets reset. The 0 is just the value the variable gets initialized to. But since 0 is the default value anyway, Maverick's way would work as well.

Have you tried putting some Display commands into your functions and conditional statements to see if they are called at all?
At the same time you can output the value of the variable like this:
  Display("Fez is %d", Fez);
Put it in game_start, right after you change its value, and right before you check its value.

FinalSin

Quote from: strazer on Thu 27/07/2006 13:39:42
If you put
  int Fez = 0;
outside of any functions, I don't think it gets reset. The 0 is just the value the variable gets initialized to. But since 0 is the default value anyway, Maverick's way would work as well.

Have you tried putting some Display commands into your functions and conditional statements to see if they are called at all?
At the same time you can output the value of the variable like this:
  Display("Fez is %d", Fez);
Put it in game_start, right after you change its value, and right before you check its value.

I've tried Maverick's suggestion and it doesn't solve the problem. I tried to debugging value earlier, and Fez stays at 0 all the time.
The Blue Casket

strazer

So it displays 0 even right after you change it? And the function where you change is called? Are you sure? Please check again.

If all else fails, you could upload the game somewhere for us to take a look at.

Khris

Quote from: Maverick on Thu 27/07/2006 13:23:38Yup! As far as I know it will be reset to Fez = 0 every time because that is what you are telling it to do.

// main global script file
Ã,  Ã, int Fez = 0;// You are assigning the zero value
Ã,  Ã,  export Fez;

I think it needs to be emphasized how WRONG this statement was.

FinalSin:
Please post the complete function containig the code that changes Fez to 1.

FinalSin

I'll try posting the code, before the game, though apart from this bug the coding has actually reached completion for episode one.

Code: ags
function dialog_request(int parameter) {
  if (parameter == 1)
		{
    SetGraphicalVariable("Intro",  1);
		}
	else{if (parameter == 2)
		   SetGraphicalVariable("Fez",  1);
		   else{if (parameter == 3)
					SetDialogOption(9, 2, eOptionOn);
					else{if (parameter == 4)
					 {
					  //The Big Script
					  character[EGO].Walk(140, 78);
					  character[WAIT].Say("Hey, Officer!");
					  character[OFF1].FaceCharacter(character[6], eBlock);
					  character[OFF1].Say("Yes,  ma'am?");
					  character[WAIT].Say("I got some important evidence for ya.");
					  character[OFF1].Say("Re-really?");
					  character[OFF1].Walk(239, 84, eNoBlock);
					  SetDialogOption(9, 2, eOptionOffForever);
					  SetDialogOption(3, 3, eOptionOn);
					  SetDialogOption(3, 1, eOptionOffForever);
					  character[OFF1].SayBackground("So,  where's the evidence?");
					  character[EGO].Walk(69, 173);
					  character[WAIT].SayBackground("Let me whisper it.");
					  character[BMAN].Say("Thanks,  officer.");
					  character[OFF1].SayBackground("It's WHERE?");
						character[EGO].Say("Don't mention it.");
						SetGraphicalVariable("Fez",  2);
						RunDialog(7);
						}
						else{if(parameter == 5)
							  SetDialogOption(8, 3, eOptionOn);
								else
								player.ChangeRoom(15, 5, 5);
								}
							}
							
						}
					}
			}


I am aware that this is messy code, and that elseifs are supported, and that curly brackets are not going out of style, but please bear with me, as the dialog_request is a new feature for me. run-script 2 is called during a dialogue sequence, which should set Fez to 1.

However, it's worth noting that my frustration led me to place a "SetGraphicalVariable ("Fez", 1)" command in the script for that room, just to see if I could force Fez to change to 1, in case my coding was wrong, and it still hasn't changed the variable.

Many thanks for your continuing patience with me, guys.
The Blue Casket

Ashen

Fez ISN'T a Graphical Variable, it's a scripted int, so the way to change it would be:
Code: ags

Fez = 1;


(Graphical Variables are the ones created in the Interaction Editor.)

If you do have a Graphical Variable you need to check, you should be using if (GetGraphicalVariable("Fez") == 1), not if(Fez == 1) - but that doesn't sound to be the case here, from your other posts.
I know what you're thinking ... Don't think that.

DoorKnobHandle

Ashen is right, this should work then. However I want to motivate you to write your code in a clear manner. You could for example format it like this:

Code: ags

function dialog_request ( int parameter )
{
	if ( parameter == 1 )
		Intro = 1;

	else if ( parameter == 2 )
		Fez = 1;
		
	else if ( parameter == 3 )
		SetDialogOption ( 9, 2, eOptionOn );
			
	else if ( parameter == 4 )
	{
		// the big script...
		Fez = 2;
		RunDialog ( 7 );
	}
	
	else if ( parameter == 5 )
		SetDialogOption ( 8, 3, eOptionOn );
					
	else
		player.ChangeRoom ( 15, 5, 5 );
}


Which is a million times easier to read!

FinalSin

I can't tell you how invaluable this is to me. I'm beginning a course in Computer Science in October, and this kind of analysis and help is just priceless. Thanks.

As for the layout of my code - it's a Java technique that looks really ugly in AGS. I'll lay off the indentation.

Thankyou all so much.
The Blue Casket

Alynn

That my dear sir, is no java technique I've ever learned. In fact since java and AGS script both have the same syntaxual father they aren't that much different. Main ones being the coding conventions (such as setGameSpeed in Java and SetGameSpeed in AGS).

You seem to have random indentions everywhere, and in other places not a single indentation to even HINT at the structure of your code. As a programmer you tend to start using indentations so that the brackets can largely be ignored, IE nobody has to count brackets to see how deep they are into the code if it's already indented to show them how deep they are.

Khris

My favourite way of indenting is like this:
if (bla) {
Ã,  doThis;
Ã,  doThat;
}
else if {
Ã,  if (yadda) {
Ã,  Ã,  doSomethingElse;
Ã,  }
Ã,  else {
Ã,  Ã,  doAnotherThing;
Ã,  }
}
else {
Ã,  doSomethingCompletelyDifferent;
}


I think this is the most readable way, and AGS does it like that automatically, too! ;)

Ashen

I use pretty much the same way as KhrisMUC. However unless you're posting here, or publishing a Module/Template, mostly the only person who'll see your code is you. Provided YOU can read it OK and are comfortable with the formatting, it doesn't really matter how you indent it - just clean it up before posting to ask for help. That said, the way you've got it now IS kind of unreadably confused.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk