Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Kamileon on Wed 04/06/2014 10:48:55

Title: Opening and Closing a Door
Post by: Kamileon on Wed 04/06/2014 10:48:55
Hey guys, I am currently trying to enable a cupboard to be opened when the player presses on the object. However, I keep getting the error below:
room2.asc(35): Error (line 35): 'ob1' is a global var; cannot use as name for local

If anyone could help me fix this issue that would be really good :grin:

Title: Re: Opening and Closing a Door
Post by: Kamileon on Wed 04/06/2014 10:50:28

//Code in GlobalScript.asc
function VisibleSwitch(Object* oCupboardClosed, Object* oCupboardOpen)
{if ((ob1.Visible == true) && (ob2.Visible == false)) {
  ob1.Visible = false;
    ob2.Visible = true;
  } else if ((ob1.Visible == false) && (ob2.Visible == true)) {
    ob1.Visible = true;
    ob2.Visible = false;
  }
}
Title: Re: Opening and Closing a Door
Post by: Slasher on Wed 04/06/2014 12:17:50
Objects used in the Global must use their Object ID NOT what you have called them.

Eg:
Code (ags) Select

ob1.Visible = true; // Will bring up an error in Global.

object[1].Visible=true; // Acceptable in Global.



Title: Re: Opening and Closing a Door
Post by: Khris on Wed 04/06/2014 12:19:20
1. Something else is also called "ob1", however you don't need to refer to that in your code.
2. This function is in the global script, which means you can only refer to room objects using the object[] array.
3. In line 2 you're supposed to use general names for the parameters; the specific objects are used when the function is called.

Code (ags) Select
// Globalscript.ash
import function VisibleSwitch(Object *a, Object *b);

// GlobalScript.asc

function VisibleSwitch(Object *a, Object *b) {
  a.Visible = !a.Visible;
  b.Visible = !b.Visible;
}

// Room script
function cupboard_Interact() {
  VisibleSwitch(oCupboardClosed, oCupboardOpen);
}


Now enter "cupboard_Interact" for both objects next to "interact with object".

Edit: fixed last function
Title: Re: Opening and Closing a Door
Post by: Kamileon on Wed 04/06/2014 12:54:57
@Khris; I entered the code into their respective scripts but when doing the room script it came up with the following error below.
Failed to save room room2.crm; details below
room2.asc(37): Error (line 37): Unexpected '{'

Code (ags) Select

// Room script
function cupboard_Interact;
{
  VisibleSwitch(oCupboardClosed, oCupboardOpen);
}
Title: Re: Opening and Closing a Door
Post by: Khris on Wed 04/06/2014 13:18:43
You have a semi-colon at the end of line 2, remove it.
Title: Re: Opening and Closing a Door
Post by: Gilbert on Wed 04/06/2014 16:11:48
Not only that, but also the missing parentheses:
function cupboard_Interact()
Title: Re: Opening and Closing a Door
Post by: Khris on Wed 04/06/2014 19:09:53
Whoops, sorry, thanks. Fixed the code.
Title: Re: Opening and Closing a Door
Post by: Kamileon on Wed 04/06/2014 22:29:28
The good news is that no more errors are popping up :grin:
However, the bad news is that when I interact with either door they both disappear and don't come back.
Title: Re: Opening and Closing a Door
Post by: Khris on Wed 04/06/2014 23:28:59
You have to turn one of them invisible at the start of the game obviously, all my code does is toggle both objects' visibility. (Yours wouldn't have worked either though.)
Just add the room's before fadein event and in there set oCupboardOpen.Visible to false.
Title: Re: Opening and Closing a Door
Post by: Kamileon on Thu 05/06/2014 09:20:43
It's working great now, thanks for the help Khris, Slasher and Iceboty V7000a. :-D