Opening and Closing a Door

Started by Kamileon, Wed 04/06/2014 10:48:55

Previous topic - Next topic

Kamileon

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:

It's not who I am underneath, but what I do that defines me.

Kamileon

Code: ags

//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;
  }
}
It's not who I am underneath, but what I do that defines me.

Slasher

#2
Objects used in the Global must use their Object ID NOT what you have called them.

Eg:
Code: ags

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

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




Khris

#3
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
// 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

Kamileon

@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

// Room script 
function cupboard_Interact; 
{
  VisibleSwitch(oCupboardClosed, oCupboardOpen);
}
It's not who I am underneath, but what I do that defines me.

Khris

You have a semi-colon at the end of line 2, remove it.

Gilbert

Not only that, but also the missing parentheses:
function cupboard_Interact()

Khris

Whoops, sorry, thanks. Fixed the code.

Kamileon

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.
It's not who I am underneath, but what I do that defines me.

Khris

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.

Kamileon

It's working great now, thanks for the help Khris, Slasher and Iceboty V7000a. :-D
It's not who I am underneath, but what I do that defines me.

SMF spam blocked by CleanTalk