Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: bigrd32 on Wed 04/01/2006 01:58:27

Title: on/off switch script problem(SOLVED)
Post by: bigrd32 on Wed 04/01/2006 01:58:27
I'm trying to make it so when a switch is turned on there is a message displayed and a variable changed from 0 to 1.Ã,  I also want the variable to go back to zero when the switch is turned off.Ã,  I've searched the forums for help with this and had little luck finding anything.Ã, 
This is what I have so far (keep in mind this is my first time trying scripting).

// room script file
// script for object: Interact with object
if (IsObjectOn(objectnumber)) ObjectOff(objectnumber);
SetGraphicalVariable(Switch,Ã,  0);
DisplayMessage(int 71);
else ObjectOn(objectnumber);
SetGraphicalVariable(Switch,Ã,  1);
DisplayMessage(int 70);

I test the game and I get an error that says: Parse error unexpected "if."
I'm sure there are more problems than just that but I havn't made it far enough to see them :P

Edit:Ã,  I think we're getting close.Ã,  Now I get an error that says Error line 8 undefined symbol 'IsObjectOn'

// room script file

#sectionstart object8_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE
function object8_a() {
Ã,  // script for Object 8 (light switch): Interact object
// room script file
// script for object: Interact with object
if (IsObjectOn(objectnumber)){
ObjectOff(objectnumber);
SetGraphicalVariable("Switch",Ã,  0);
DisplayMessage(71);
} else {
ObjectOn(objectnumber);
SetGraphicalVariable("Switch",Ã,  1);
DisplayMessage(70);}Ã, 
}
#sectionend object8_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE
Title: Re: on/off switch script problem
Post by: Gilbert on Wed 04/01/2006 02:04:05
// room script file
// script for object: Interact with object
if (IsObjectOn(objectnumber)) {
Ã,  ObjectOff(objectnumber);
Ã,  SetGraphicalVariable("Switch",Ã,  0);
Ã,  DisplayMessage(int 71);
} else {
Ã,  ObjectOn(objectnumber);
Ã,  SetGraphicalVariable("Switch",Ã,  1);
Ã,  DisplayMessage(int 70);
}

The { } were added after assuming the logic you want to archieve, not sure if it fixes everthing though.
Title: Re: on/off switch script problem
Post by: bigrd32 on Wed 04/01/2006 02:08:08
I made the changes you suggested but I don't know if they work because I still get the unexpected "if" and I can't even test the game.  I'm sure it fixed various other problems though.  Thanks.
Title: Re: on/off switch script problem
Post by: strazer on Wed 04/01/2006 02:08:57
Quote
// room script file
// script for object: Interact with object

You can't just write the code into the top of room script. How should AGS know when to execute it? You first have to create the event function that will contain the code with the interaction editor:
Select the object in the room editor, click the "Interaction..." button, select the "Interact with" event and add a "Run script" action from the drop-down list.
Then click the "Edit script..." button and paste the code in there.
Title: Re: on/off switch script problem
Post by: bigrd32 on Wed 04/01/2006 02:58:11
For the error undefined symbol 'IsObjectOn'   Am I getting this error because the game doesn't know if the object is on or not?  If so how would I fix this problem?
Title: Re: on/off switch script problem
Post by: Gilbert on Wed 04/01/2006 03:00:45
Did you define a variable called objectnumber?

Otherwise, put in the exact object number you're refering to, like for example:

IsObjectOn(2)
Title: Re: on/off switch script problem
Post by: bigrd32 on Wed 04/01/2006 03:04:28
Yes I did.Ã,  I didn't put it on here because it looks like this (IsObjectOn(8)).
Title: Re: on/off switch script problem
Post by: monkey0506 on Wed 04/01/2006 03:23:29
Just curious, you haven't mentioned: What version of AGS are you using?  If you are using 2.7 or later, it would appear to me that you have the "Enforce new-style scripting" check box clicked, in which case IsObjectOn would be undefined.  It's been changed to Object.Visible (where Object is the name of the structure, Object, and should be replaced with an actual object of that type (you should set the script o-name and use that)).

But then if you are using 2.62 or earlier...I'm not really sure what you have done here...
Title: Re: on/off switch script problem
Post by: Gilbert on Wed 04/01/2006 03:27:27
(You can disable the smileys using the "additional options...")

If you're using V2.7+, either use:

if (object[8].Visible) ...

or something like:

if (oSwitch.Visible) ...

(if say the object is named "Switch" in the room)
Title: Re: on/off switch script problem
Post by: bigrd32 on Wed 04/01/2006 03:39:19
I've changed my script to this:

// room script file

#sectionstart object8_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE
function object8_a() {
Ã,  // script for Object 8 (light switch): Interact object
// room script file
// script for object: Interact with object
if (Object[8].Visible=true){
Object[8].Visible=false);
SetGraphicalVariable("Switch",Ã,  0);
DisplayMessage(71);
} else {
Object[8].Visible=true;
SetGraphicalVariable("Switch",Ã,  1);
DisplayMessage(70);}Ã, 
}
#sectionend object8_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE

now I get an error saying line 8 object is not in array.
Will this ever end? ;)

Edit: I wonder what's going to happen when I want to do something complicated :o
Title: Re: on/off switch script problem
Post by: Gilbert on Wed 04/01/2006 03:46:23
object[8]

Cases matter, also use == for comparison, you can't use =.

#sectionstart object8_a  // DO NOT EDIT OR REMOVE THIS LINE
function object8_a() {
  // script for Object 8 (light switch): Interact object
if (object[8].Visible==true){
  object[8].Visible=false);
  SetGraphicalVariable("Switch",  0);
  DisplayMessage(71);
} else {
  object[8].Visible=true;
  SetGraphicalVariable("Switch",  1);
  DisplayMessage(70);} 
}
#sectionend object8_a  // DO NOT EDIT OR REMOVE THIS LINE
Title: Re: on/off switch script problem
Post by: bigrd32 on Wed 04/01/2006 03:59:26
Okay this is getting out of hand.  What is a parse error and what is it doing at line 9 'object'?  I should pay you guys.
Title: Re: on/off switch script problem
Post by: bigrd32 on Wed 04/01/2006 04:37:40
The script works now but I have a different problem.  The switch is an object.  When I turn it off (visible=false) it is removed from the room and can't be turned back on.
Title: Re: on/off switch script problem
Post by: strazer on Wed 04/01/2006 05:03:05

  if (GetGraphicalVariable("Switch") == 1) {
    SetGraphicalVariable("Switch", 0);
    DisplayMessage(71);
  }
  else {
    SetGraphicalVariable("Switch", 1);
    DisplayMessage(70);}
  }


So you don't actually need an object for the switch. You could just draw a hotspot onto the background image where the switch is and add this code to its interact event.
Title: Re: on/off switch script problem
Post by: bigrd32 on Wed 04/01/2006 21:24:05
Great!!! Thanks for all of your help and I'm really enjoying learning AGS and script.