Coding a 3 numbers code lock

Started by Nadarian, Fri 24/07/2020 16:56:48

Previous topic - Next topic

Nadarian

Hi!

My first game is almost finished, but in my nearly zero coding knowledge, I'm stuck in one final puzzle. There's this 3 numbers lock, the usual one with 3 rotating rings with numbers on them, and you need to aling the correct ones.

My first idea (which I don't know if it's the simplest or if it's doable) was to create the 3 numbers as characters with a view with 10 sprites, numbers 0 to 9.
Then, buttons as hotspots with up/down arrows to scroll numbers up and down on each ring. When clicking "up", next sprite on that character should be shown. Same with "down", showing the previous sprite. This is the first step where I got stuck... using this:

Code: ags

function hBoto1_Up_AnyClick()
{
   cBoto1.LockViewFrame(13, 0, 1, eStopMoving);
}


isn't correct. Every "Any click" should keep scrolling and scrolling numbers, but I don't know how to code that it keeps changing the frame, one by one.


Then, there should be a line in the code that recognizes that the correct 3 numbers code has been set, to trigger the next cutscene.



Seems really simple in my mind and I already set the "characters", their sprites and the buttons and their hotspots, but I don't know how to start coding all this. :-\
Teaching film photography in the 21st century.
Now, trying to do it through adventure games.

Cassiebsg

Well, you obviously want it to display the first number and then the next and the next...

Lets assume that you start at 0 and go thru up to 9. Assuming your sprites/frames are in order, then you start at 0, and then add a +1 every time the player clicks the button.
Once you reach 9 then next one will again be 0, so you of course need to reset it.

Then you can add an If check, to check if the combination is now the right one, and if it is, then do whatever you want it to happens. Otherwise nothing happens.

Hope it helps to get the grey cells working.  ;)
There are those who believe that life here began out there...

Nadarian

Yes Cassiebsg! All you said is exactly what I'm aiming for! And though I checked the forum, the manual, and tried many different ways of scripting it... my scripting knowledge is definitely far from letting me get it right. So much so that looking for the right parts of the manual to look for this information, is getting really hard  (laugh)-
And I'm sure what I'm trying to do is not difficult at all, but it's beyond my reach now.

Thanks!! I'll keep trying and learning!
Teaching film photography in the 21st century.
Now, trying to do it through adventure games.

Cassiebsg

Create a variable for the code, and set it at game start.

Then just use if/else if/else kind of coding...

I probably would have just used a GUI, but what you use is kind of irrelevant, and a character will work just as fine as an an object or a GUI will.

code1=1; (use a global variable here)
code2=2; (use a global variable here)
code3=3; (use a global variable here)

note, this could also be code=123, but since I'm a worse coder, I'll go with a simpler code.

Code: ags

function hBoto1_Up_AnyClick()
{
   cBoto1.Frame=cBoto1.Frame+1;
   if (cBoto1.Frame==10)cBoto1.Frame=0; // reseating the counter to 0, since there are only numbers 0 to 9.
   if (code1==cBoto1.Frame) Display ("This is the right key number for code 1");
   if (code1==cBoto1.Frame && code2==cBoto2.Frame && code3==cBoto3.Frame) Display ("This is the correct combination!");
}

function hBoto2_Up_AnyClick()
{
   cBoto2.Frame=cBoto2.Frame+1;
   if (cBoto2.Frame==10)cBoto2.Frame=0; // reseating the counter to 0, since there are only numbers 0 to 9.
   if (code2==cBoto2.Frame) Display ("This is the right key number for code 2");
   if (code1==cBoto1.Frame && code2==cBoto2.Frame && code3==cBoto3.Frame) Display ("This is the correct combination!");
}

function hBoto3_Up_AnyClick()
{
   cBoto3.Frame=cBoto3.Frame+1;
   if (cBoto3.Frame==10)cBoto3.Frame=0; // reseating the counter to 0, since there are only numbers 0 to 9.
   if (code3==cBoto3.Frame) Display ("This is the right key number for code 3");
   if (code1==cBoto1.Frame && code2==cBoto2.Frame && code3==cBoto3.Frame) Display ("This is the correct combination!");
}


See if this works. :)

There are those who believe that life here began out there...

Crimson Wizard

Why characters and not simply room objects? Room objects don't need Views, they may have Graphic property set directly.

Matti

#5
Why not use a GUI? You could have buttons for the digits and the arrows. In my code, bLock1-3 are buttons for the digits of the lock, bButtonUp1-3 and bButtonDown1-3 are the arrow buttons.

Code: ags

int code1 = 0;
int code2 = 0;
int code3 = 0;

function UpdateLock() // function to update the button sprites and to check whether the numbers are correct
{
  bLock1.NormalGraphic = 10 + code1; // graphic slots (e.g. 10-19)
  bLock2.NormalGraphic = 10 + code2;
  bLock3.NormalGraphic = 10 + code3;

  if (code1 == 5 && code2 == 1 && code3 == 8) // right numbers (e.g. 518)
  {
    // do something
  }
}  

function bButtonUp1_OnClick()
{
  code1 ++;
  if (code1 > 9) code1 = 0;
  UpdateLock();
}

function bButtonDown1_OnClick()
{
  code1 --;
  if (code1 < 0) code1 = 9;
  UpdateLock();
}

// do the same for buttons 2 and 3 (using code2 and code3)


Edit: Obviously, you could do the same thing using room objects instead of GUI buttons.

Nadarian

Thank you all!!

Yes, I know using characters wasn't the easiest or most logical way to go, but it made sense to me... (laugh)
And as I could see with your ideas, there were also many different ways to code this, but I simply couldn't guess the right way. I saw it crystal clear when Cassiebgs posted the code.

Now it's done, and working like a charm! Thanks for all your knowledge!
I'll soon be able to post my first game here!
Teaching film photography in the 21st century.
Now, trying to do it through adventure games.

SMF spam blocked by CleanTalk