Beginner with two advanced questions

Started by Jordanowen42, Tue 15/08/2023 06:00:37

Previous topic - Next topic

Jordanowen42

Hello-

First may I say that I really appreciate the guidance I've gotten so far in making my first game- this has been a very helpful resource. You have all been great about checking my code, holding my hand through my confusion, etc.

Now I have two bigger questions (or at least they seem that way to me the layman.) I'm designing a puzzle that requires me to learn two new programming mechanics.

1. The puzzle involves a keypad with a 3x3 button grid. Pushing one button causes other buttons to light up and some buttons to go off if already lit. The goal is to find the sequence of buttons that will cause all the buttons to become lit. To accomplish this I made a "lit" version of the buttons that serves as an object within the game. All 9 "lit button" objects start out as invisible and are made visible when clicked on. So the code, in essence, says "When button five is pushed, cause objects 2, 4, 6, and 8 to become visible."

So my question is this: how do I write code which causes the game to become aware when all of the buttons are lit so as to trigger the result of the puzzle?

2. The reward for the puzzle is to open a door that is on a different screen. The open door is an object that becomes visible. How do I trigger a result on a different room from the one I'm on?

Thank you so much for you help!

Snarky

1. Whether the button is lit in this case is equivalent to "this object is visible." So you would write a check that is something like

Code: ags
  if(   oButton1.Visible
     && oButton2.Visible
     && oButton3.Visible
     && oButton4.Visible
     && oButton5.Visible
     && oButton6.Visible
     && oButton7.Visible
     && oButton8.Visible
     && oButton9.Visible)
  {
    // All buttons lit, so open door
    doorIsOpen = true;
  }

(I've split the test across multiple lines to make it easier to read—with this formatting you can see at a glance that it tests 9 buttons; you can do that but you don't have to.)

A good way to run this code would be to make a function that you call after each button is pressed.

2. In that case, you would make a global variable (for example using the Global Variables pane), for example bool doorIsOpen that is set to false at game start. As you can see, the code above sets it to true. Then in the "enter room" event of the other room, you check that variable and set the open door object to visible or not depending on whether it's true.

(A variable used in this way is often called a "flag.")

Khris

Regarding the puzzle you can probably shorten your code considerably if you use an array of bools, a single rectangular hotspot covering the entire keypad and nine non-clickable objects.

Code: ags
bool lit[9];

function hKeypad_Interact() {
  int xo = 123, yo = 45; // coordinates of top left corner of top left button
  int w = 20, h = 15; // width and height of a button
  int xg = 5, yg = 5; // gap between buttons

  int x = (mouse.x - xo);
  int y = (mouse.y - yo);
  if (x % (w + xg) > w || y % (h + yg) > h) return; // clicked in between buttons
  int bi = (y / (h + yg)) * 3 + (x / (x + xg)); // should be 0-8
  
  // flip clicked and neighboring bools
  for (int i = 0; i < 9; i++) {
    if (i == bi || i + 3 == bi || i - 3 == bi || i % 3 == 0 && i + 1 == bi || i % 3 == 2 && i - 1 == bi) lit[i] == !lit[i];
  }

  // update button objects visibility
  for (int i = 0; i < 9; i++) object[i + 2].Visible == lit[i]; // first button object has ID 2, etc.
}

Jordanowen42

Awesome thanks- I'll start working with that! :)

SMF spam blocked by CleanTalk