So, this seems very simple, but it is turning out to be difficult to execute.
I want to make it so that interacting with a hotspot opens a door. Further, I want to make sure that interacting with that hotspot a second time makes the door close, rather than open a second time. So I created a variable that keeps track of whether the door is open or closed:
function hButton_Interact()
{
if (myDoor == 0)
{
cAlban.LockView(2);
cAlban.Walk(171, 124, eBlock);
cAlban.FaceLocation(148, 102);
oDoor.SetView(4, 0, 0);
oDoor.Animate(0, 5, eOnce, eBlock, eForwards);
cAlban.UnlockView();
myDoor ++;
}
if (myDoor == 1)
{
cAlban.LockView(2);
cAlban.Walk(171, 124, eBlock);
cAlban.FaceLocation(148, 102);
oDoor.SetView(4, 0, 0);
oDoor.Animate(0, 5, eOnce, eBlock, eBackwards);
cAlban.UnlockView();
myDoor --;
}
}
But now, when the character interacts with the hotspot, the door opens and then immediately closes. Looking at the code, I understand why this is happening, but I can't figure out a way to script around it. Am I not nesting things correctly?
Thanks in advance for the help.
Change
if (myDoor == 1)
to:
else if (myDoor == 1)
Thanks. I knew it had to be a simple solution.
You can shorten this considerably:
function hButton_Interact() {
cAlban.LockView(2);
cAlban.Walk(171, 124, eBlock);
cAlban.FaceLocation(148, 102);
oDoor.SetView(4, 0, 0);
if (myDoor) oDoor.Animate(0, 5, eOnce, eBlock, eBackwards);
else oDoor.Animate(0, 5, eOnce, eBlock, eForwards);
myDoor = 1-myDoor;
}
A tip I find helpful is when dealing with simple yes / no things is to declare it as a bool instead of an int. That way you can have a variable called 'doorOpen', or whatever, and assign it to 'true' or 'false'. I find this way easier to read when viewing the code, but it's completely up to you.
bool doorOpen;
if(doorOpen == true) {
// Close door
}
else {
// Open door
}
Or just
if (doorOpen) { ...
Works with ints, too. Plus, an int can be switched using
doorOpen = 1-doorOpen;
Well, a bool can be switched with:
doorOpen = !doorOpen;
which is a bit easier to read :P