Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: AndreasBlack on Tue 17/05/2022 10:42:00

Title: Lose the Key after using the Doors syntax in Thumbleweed
Post by: AndreasBlack on Tue 17/05/2022 10:42:00
Can't see anything about it in the manual, but i've been known to be blind before so. Maybe it's there, just not written as an actual example.

How do i lose the key from the inventory forever after it's been unlocked? I've managed to do it with a weird solution, but there's gotta be a proper way to do it inside a normal function and not repetedly execute like i'm using.

Weird solution so far, a simply check with some synthax like "doors.getdoorstate==0")  ???

Code (ags) Select
function hBedroom_Door_AnyClick()
{

 
if (Doors.AnyClickSpecial(10, oBedroom_Door.ID,  41,  116, eDirectionLeft, 1, 200, 145, eDirectionLeft, aDoor_Opened, aDoor_Closed, 10, 0) == 0)
Verbs.Unhandled() || cRain.ActiveInventory==iBedroom_Key;



function hBedroom_Door_UseInv()
{


if (cRain.ActiveInventory==iBedroom_Key)

{
  Doors.SetDoorState(10, 2);

  Key_Unlocked=1;
 
  if (Key_Unlocked==1 && !cRain.Walk(41, 121, eBlock))
 
{
      aDoor_Unlocked.Play();

}
}

}

//and finally........

function room_RepExec()
{
if (Key_Unlocked==1)

{
 
  if (cRain.HasInventory(iBedroom_Key))
  {
  cRain.LoseInventory(iBedroom_Key);
  } 

}


}




Title: Re: Lose the Key after using the Doors syntax in Thumbleweed
Post by: Khris on Tue 17/05/2022 11:50:26
With the Tumbleweed template, you're supposed to use the AnyClick event for all hotspot interactions.

Try this:
Code (ags) Select
function hBedroom_Door_AnyClick() {
  // standard behavior
  if (Doors.AnyClickSpecial(10, oBedroom_Door.ID,  41,  116, eDirectionLeft, 1, 200, 145, eDirectionLeft, aDoor_Opened, aDoor_Closed, iBedroom_Key.ID, 0) == 0) Verbs.Unhandled();
  // if key was used, lose it
  if (Verbs.UsedAction(eGA_UseInv) && player.ActiveInventory == iBedroom_Key) player.LoseInventory(iBedroom_Key);
}


If the door is initially locked, using the key on it will unlock it, so we can safely assume that happened.
Title: Re: Lose the Key after using the Doors syntax in Thumbleweed
Post by: abstauber on Tue 17/05/2022 12:06:33
To be 100% safe, you can also check, if the door has actually been unlocked.
Doors.GetDoorState(door_id) == 0
Code (ags) Select

function hBedroom_Door_AnyClick() {
  // standard behavior
  if (Doors.AnyClickSpecial(10, oBedroom_Door.ID,  41,  116, eDirectionLeft, 1, 200, 145, eDirectionLeft, aDoor_Opened, aDoor_Closed, iBedroom_Key.ID, 0) == 0) Verbs.Unhandled();
  // if key was used, lose it
  if (Verbs.UsedAction(eGA_UseInv) && player.ActiveInventory == iBedroom_Key && Doors.GetDoorState( oBedroom_Door.ID) == 0 ) player.LoseInventory(iBedroom_Key);
}
Title: Re: Lose the Key after using the Doors syntax in Thumbleweed
Post by: AndreasBlack on Tue 17/05/2022 12:18:57
intermittent fasting isn't to recommend either when doing this, i suppose........ :-X

edit: HAHA!!!!  (laugh) What a smart ***, added the KEYS in the ROOM LOAD. ROFL, fantastic logic!!! What did i expect? Nothing wrong with the code, like always!  (laugh) (roll)


Title: Re: Lose the Key after using the Doors syntax in Thumbleweed
Post by: Nahuel on Wed 31/08/2022 09:51:39
Hi there guys, I was just testing some stuff and playing around and the double click button can bypass this action and the door will remain locked and the inventory will be removed.

I've just bypassed as "double click action" adding the new state

Code (ags) Select

function hBedroom_Door_AnyClick() {
  // standard behavior
  if (Doors.AnyClickSpecial(10, oBedroom_Door.ID,  41,  116, eDirectionLeft, 1, 200, 145, eDirectionLeft, aDoor_Opened, aDoor_Closed, iBedroom_Key.ID, 0) == 0) Verbs.Unhandled();
  // if key was used, lose it
  if (Verbs.UsedAction(eGA_UseInv) && player.ActiveInventory == iBedroom_Key && Doors.GetDoorState( oBedroom_Door.ID) == 0 ) {
    player.LoseInventory(iBedroom_Key);
    Doors.SetDoorState(10, 0);  // You force the door to be set closed (the action was not to open, just to unlock
  }
}


Is there anything I'm missing that I can "lock" the action of the character?

Thanks and hope it helps.
Title: Re: Lose the Key after using the Doors syntax in Thumbleweed
Post by: Khris on Wed 31/08/2022 09:57:22
You're passing the (arbitrary) object ID to  Doors.GetDoorState()  instead of the door id (10)
Title: Re: Lose the Key after using the Doors syntax in Thumbleweed
Post by: Nahuel on Wed 31/08/2022 11:21:37
Quote from: Khris on Wed 31/08/2022 09:57:22
You're passing the (arbitrary) object ID to  Doors.GetDoorState()  instead of the door id (10)

I see, then the example code is wrong, thanks Khris.

Code (ags) Select
if (Verbs.UsedAction(eGA_UseInv) && player.ActiveInventory == iBedroom_Key && Doors.GetDoorState( 10 ) == 0 ) player.LoseInventory(iBedroom_Key);
Title: Re: Lose the Key after using the Doors syntax in Thumbleweed
Post by: Khris on Wed 31/08/2022 15:22:04
Indeed, here's the fixed version for reference:

Code (ags) Select

function hBedroom_Door_AnyClick() {

  int door_id = 10;

  // standard behavior
  if (Doors.AnyClickSpecial(door_id, oBedroom_Door.ID,  41,  116, eDirectionLeft, 1, 200, 145, eDirectionLeft, aDoor_Opened, aDoor_Closed, iBedroom_Key.ID, 0) == 0) Verbs.Unhandled();
  // if key was used, lose it
  if (Verbs.UsedAction(eGA_UseInv) && player.ActiveInventory == iBedroom_Key && Doors.GetDoorState(door_id) == 0) player.LoseInventory(iBedroom_Key);
}