Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jojowl80 on Sat 27/03/2021 00:45:28

Title: Using Bools to make a lock
Post by: Jojowl80 on Sat 27/03/2021 00:45:28
trying to make a combination lock without using integers. I am terrible at math.
this is what I have formulated so far. I have no idea if this will even work.
Code (ags) Select

function room_RepExec()
{
if ((((but1 == true && (but2 == true) && (but3 == true) && (but4 == true)))))
lev1 = true;
}


function hbut1_AnyClick()
{
sClick.Play();{
but1 = true;
but2 = false;
but3 = false;
but4 = false;
 
}
if (((but2 == true) && (but4 == true) && (but3 == true))){
sClick.Play();
but1 = true;
cJojo.Say("Coordinates ready");
}}

function hbut2_AnyClick()
{
sClick.Play();
but2 = true;
but1 = false;
but3 = false;
but4 = false;
}

function hbut3_AnyClick()
{
sClick.Play();{
but3 = true;
but1 = false;
but2 = false;
but4 = false;
}
if ((but2 == true) && (but4 == true)){
sClick.Play();
but3 = true;
}}

function hbut4_AnyClick()
{

sClick.Play();{
but4 = true;
but1 = false;
but2 = false;
but3 = false;
}
if (but2 == true){
sClick.Play();
but4 = true;
}}

function hlev1_AnyClick()
{
if (Verbs.UsedAction(eGA_Push)) {
cJojo.Say("I need coordinates first.");
}
if (lev1 == true){
cJojo.Say("Engage!");
sRocket.Play();

}

else if(Verbs.UsedAction(eGA_Pull)) {
   cJojo.Say("I'm already Disengaged.");
}
else if(Verbs.UsedAction(eGA_Open)) {
    cJojo.Say("What would that do?");
   
}
else if(Verbs.UsedAction(eGA_Close)) {
    cJojo.Say("What would that do?");
}
}

Title: Re: Using Bools to make a lock
Post by: Crimson Wizard on Sat 27/03/2021 01:14:41
I'd like to mention that you may simplify conditions with boolean variables.
Because a boolean variable itself translates to "true" or "false" value, you do not need to compare them directly.

Code (ags) Select
if (var == true) may be written as
Code (ags) Select
if (var), and
Code (ags) Select
if (var == false) may be written as
Code (ags) Select
if (!var).

" ! " is a NOT operator, it compares opposite value.

So, for example, you may change
Code (ags) Select

if ((((but1 == true && (but2 == true) && (but3 == true) && (but4 == true))))

To
Code (ags) Select

if (but1 && but2 && but3 && but4)

Title: Re: Using Bools to make a lock
Post by: Jojowl80 on Sat 27/03/2021 01:39:00
okay that's cool to know, but I am still lost :D
Title: Re: Using Bools to make a lock
Post by: arj0n on Sat 27/03/2021 14:20:26
Assuming the lock's order is: 2-4-3-1, you could do something like this:

Code (ags) Select

// room script file
bool but1;
bool but2;
bool but3;
bool but4;
bool lev1;

function Set4ButtonsToInitialValues()
{
  but1 = false;
  but2 = false;
  but3 = false;
  but4 = false;
}

function room_Load()
{
  Set4ButtonsToInitialValues();
}

function hbut1_AnyClick()
{
  if (lev1) cJojo.Say("Coordinates are already ready");
  else if (!but1 && but2 && but3 && but4)
  {
    sClick.Play();
    but1 = true;
    if (lev1 == false)
    {
      cJojo.Say("Coordinates ready");
      lev1 = true;
    }
  }
  else Set4ButtonsToInitialValues();
}

function hbut2_AnyClick()
{
  if (!but1 && !but2 && !but3 && !but4)
  {
    sClick.Play();
    but2 = true;
  }
  else Set4ButtonsToInitialValues();
}

function hbut3_AnyClick()
{
  if (!but1 && but2 && !but3 && but4)
  {
    sClick.Play();
    but3 = true;
  }
  else Set4ButtonsToInitialValues();
}

function hbut4_AnyClick()
{
  if (!but1 && but2 && !but3 && !but4)
  {
    sClick.Play();
    but4 = true;
  }
  else Set4ButtonsToInitialValues();
}

function hlev1_AnyClick()
{
  if (!lev1)
  {
    if (Verbs.UsedAction(eGA_Push)) cJojo.Say("I need coordinates first.");
    else if(Verbs.UsedAction(eGA_Pull)) cJojo.Say("I'm already Disengaged.");
    else if(Verbs.UsedAction(eGA_Open)) cJojo.Say("What would that do?");
    else if(Verbs.UsedAction(eGA_Close)) cJojo.Say("What would that do?");
  }
  else if (lev1)
  {
    cJojo.Say("Engage!");
    sRocket.Play();
  }
}
Title: Re: Using Bools to make a lock
Post by: Jojowl80 on Sat 27/03/2021 19:30:04
that worked great. even though I cannot comprehend some of it. My brain just cant follow the logical path of why its working. anyway. Now when I Pull ,push,open, the lever it says engage on all instances. Not a huge deal. Thanks for your help!
Title: Re: Using Bools to make a lock
Post by: arj0n on Sat 27/03/2021 20:01:18
Yes, I didn't know for sure what you meant with these actions.
Maybe this would improve it, just replace the function 'hlev1_AnyClick' with this:

Code (ags) Select
function hlev1_AnyClick()
{
  //in case the player has not solved the lock code sequence:
  if (!lev1)  cJojo.Say("I need coordinates first.");
  //but, in case the player has solved the lock code sequence:
  else if (lev1) //if code sequence is correct:
  {
    if (Verbs.UsedAction(eGA_Pull)) cJojo.Say("I'm already Disengaged.");
    else if (Verbs.UsedAction(eGA_Open)) cJojo.Say("What would that do?");
    else if (Verbs.UsedAction(eGA_Close)) cJojo.Say("What would that do?");
    else if (Verbs.UsedAction(eGA_Push))
    {
      sRocket.Play();
      cJojo.Say("Engage!");
    }
  }
}
Title: Re: Using Bools to make a lock
Post by: Jojowl80 on Sat 27/03/2021 20:26:53
perfect! I am slowly understanding. thanks so much!
Title: Re: Using Bools to make a lock
Post by: arj0n on Sat 27/03/2021 20:30:13
Good to see it works  :)