Using Bools to make a lock

Started by Jojowl80, Sat 27/03/2021 00:45:28

Previous topic - Next topic

Jojowl80

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

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?");
}
}


Crimson Wizard

#1
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
if (var == true)
may be written as
Code: ags
if (var)
, and
Code: ags
if (var == false)
may be written as
Code: ags
if (!var)
.

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

So, for example, you may change
Code: ags

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

To
Code: ags

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


Jojowl80

okay that's cool to know, but I am still lost :D

arj0n

#3
Assuming the lock's order is: 2-4-3-1, you could do something like this:

Code: ags

// 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();
  }
}

Jojowl80

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!

arj0n

#5
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
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!");
    }
  }
}

Jojowl80

perfect! I am slowly understanding. thanks so much!

arj0n


SMF spam blocked by CleanTalk