Author Topic: How to make code panel?  (Read 4539 times)

How to make code panel?
« on: 02 Jun 2014, 12:22 »
Hi.

How can I make working code panel (example code: 7145, 8411 etc.). If I choose correct code I'll leave room, if incorrect -  won't do anything.

Sth like this:


monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: How to make code panel?
« Reply #1 on: 02 Jun 2014, 12:57 »
Pretty much the easiest way to do it would be to:

*) Create a global String variable (say, "sPanelPassword")
*) Whenever you show the GUI or enter the room where this panel is shown, set your String to empty (e.g., sPanelPassword = "";)
*) Whenever you click on one of the numerical buttons, append it to the String (e.g., sPanelPassword = sPanelPassword.AppendChar('1');)
*) Now, check it against the correct password (e.g., sPanelPassword.EndsWith("7145"))
*) If it's the wrong password but the correct number of digits have been entered (check sPanelPassword.Length), you can reset the String back to empty and display a "Wrong Password" message

monkey424

  • I'm a coffee achiever
    • monkey424 worked on one or more games that was nominated for an AGS Award!
Re: How to make code panel?
« Reply #2 on: 02 Jun 2014, 13:10 »
An alternative method:

I'd use a GUI with buttons for the key pad.

In your script, declare an array with 4 elements.

e.g.

int A[4];

(gives you: A[0], A[1], A[2], A[3])

Note that you don't have to use arrays, but I like them.

You need to assign code to each button to record it's value when clicked (i.e. store value in the last array element A[3]).

You also need to store the values of the last few buttons clicked (using the other array elements, in descending order).

Suggest this code:

// click on button

A[0] = A[1];

A[1] = A[2];

A[2] = A[3];

A[3] = value of last button pressed;

So when you press the four buttons in the correct order, the array variables will reflect this.

e.g.

press 8 9 3 5

will give A[0] = 8,  A[1] = 9,  A[2] = 3,  A[3] = 5
    

monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: How to make code panel?
« Reply #3 on: 05 Jun 2014, 00:06 »
It's arguing semantics, but using four separate slots in an array is more complicated, and error-prone, than using a single String. The String method allows you to compare the result once against a single string-literal, whereas you are suggesting to check the value of four separate slots.

You're not wrong, and the array approach is actually significantly more memory efficient. *shrugs* Whichever is easiest for the end user I suppose. ;)

Gurok

  • Rottwheelers
  • When life hands you lemons, combine them with the mop
    • I can help with AGS tutoring
    • Best Innovation Award Winner 2016, for improving and extending the AGS scripting language
    • I can help with proof reading
    • I can help with scripting
    • Gurok worked on one or more games that won an AGS Award!
    •  
    • Gurok worked on one or more games that was nominated for an AGS Award!
Re: How to make code panel?
« Reply #4 on: 05 Jun 2014, 00:32 »
Ignore arrays v/s strings and memory efficiency, monkey_05_06. They actually behave slightly differently. Consider the situation where the passcode is 7145 and the user enters 37145. As I understand it, yours will reset the string after 3714 and at the end, the string will contain 5. monkey424's will tolerate 3714 and at the end, the array will contain 7145. Remove that check in your pseudo code for "the correct number of digits" and the two suggestions should behave very similarly.
« Last Edit: 05 Jun 2014, 00:44 by Gurok »

Re: How to make code panel?
« Reply #5 on: 05 Jun 2014, 01:40 »
I have one of those in my game. The way I did it was as follows (not sure if it's the best way but it's working):

1) First I knew I needed a way to store all of the four digits, so I made four variables. (could also have used an array but we all do things differently) These variables are: nr1, nr2 etc.
2) I made another variable used to determine what position in the sequence you are currently entering, whether it's the first or the third etc. I called this 'int current'. This variable is (for the purpose of its use) ranged from 1 to 4. That is, the number of digits in the code. I set this to 1 when I defined it.
3) When you press a button, the script will first check 'current' to see what digit in the sequence you are currently placed at, if 'current' == 1, then I change the value of nr1 to whatever the number of the button were. Then I printed it onto a label with String.Format to show the number on a prompt. I then bumped 'current' + 1, so the next button press would then logically be checking 'current == 2'.
4) This repeats for all 4 digits, if it's false, set 'current = 1' and reset all the nr# variables to 0. If it's right, then do whatever is gonna happen when you get the right code.

Oh yeah and I simply compared the label.text with another string with the right code to check if it's true or not.

Re: How to make code panel?
« Reply #6 on: 05 Jun 2014, 01:49 »
Or we could use invisible inventory items to store which button was pressed.

Just kidding, not sure why we're listing progressively worse methods of accomplishing this though.
Fail at Floaty Rog' now!  still having to deal with what games are going through

Re: How to make code panel?
« Reply #7 on: 05 Jun 2014, 02:04 »
I guess I'm thw worst then? :(

Re: How to make code panel?
« Reply #8 on: 05 Jun 2014, 02:54 »
Maybe not the worst, but certainly the most laborious. ;-D
Fail at Floaty Rog' now!  still having to deal with what games are going through

monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: How to make code panel?
« Reply #9 on: 05 Jun 2014, 16:31 »
yours will reset the string...Remove that check in your pseudo code for "the correct number of digits" and the two suggestions should behave very similarly.

you can reset the String

I listed "that check" as an optional step, not a mandatory one. Assuming that the check is omitted, then yes, the two give the same results (which was kind of my point in responding to 424).

For what it's worth, Elias' code is still essentially the same thing, we're just listing more and more complicated (and therefore potentially error-prone) means to the same end. 8-)

Re: How to make code panel?
« Reply #10 on: 09 Jun 2014, 08:23 »
Interesting posts

Would be nice to add a randomizer for the code. Each time the game starts it mixes up the correct sequence for the hotspots or buttons...
is possible to do?

geork

  • measure once, cut twice, say it was already broken
    • I can help with scripting
    • I can help with voice acting
Re: How to make code panel?
« Reply #11 on: 09 Jun 2014, 11:47 »
Sure, just use a 'random' function and insert that into whatever method you choose. 'Random' gives you an integer number between

So for Monkey_05_06's, you could create a separate string called "password", and assign every value to a random number
Code: Adventure Game Studio
  1. String password = String.Format("%d%d%d%d", Random(9), Random(9), Random(9), Random(9));
then you would check the user input string with the 'password' string:
Code: Adventure Game Studio
  1. if(sPanelPassword.EndsWith(password)) //Success!

For Monkey424's method, I'd create another set of arrays, which we'll call 'B', then assign each to a random number. So:
Code: Adventure Game Studio
  1. int B[4];
  2. //probably in game_start or something
  3. B[0] = Random(9);
  4. B[1] = Random(9);
  5. B[2] = Random(9);
  6. B[3] = Random(9);
Then to compare:
Code: Adventure Game Studio
  1. if(A[0] == B[0] && A[1] == B[1] && A[2] == B[2] && A[3] == B[3]) //Success!

 So basically, use Random(int) to achieve the randomizer for the code.

 Hope that helps! :)

Re: How to make code panel?
« Reply #12 on: 09 Jun 2014, 11:48 »
Interesting posts

Would be nice to add a randomizer for the code. Each time the game starts it mixes up the correct sequence for the hotspots or buttons...
is possible to do?

Definitely. Using monkey_05_6's method, wherever you set the password, just do this:

Code: Adventure Game Studio
  1. int ran = Random(2); //Gives you 3 random passwords. 0, 1 and 2.
  2.  
  3. if (ran == 0)
  4. {
  5.   sPanelPassword = "1234";
  6. }
  7. else if (ran == 1)
  8. {
  9.   sPanelPassword = "4321";
  10. }
  11. else if (ran == 2)
  12. {
  13.   sPanelPassword = "9876";
  14. }

That's how I'd do it. Not sure if there's a simpler way.

EDIT - Damn. Ninja'd. gerok's would work with a 100% random code. Mine only does a pre defined set.

EDIR 2 - Oh. You'd have to have a second variable to compare sPanelPassword with for my method, so you'd have to modify monkey_05_06's method/code slightly to fit what I've suggested. For example:

Code: Adventure Game Studio
  1. /*Global strings:
  2. sPanelPassword: What the password is set to.
  3. sPanelPasswordEntry: What the numbers entered on the panel are.
  4.  
  5. Then to check the password: */
  6. if (sPanelPassword == sPanelPasswordEntry)
  7. {
  8.   //whatever happens.
  9. }
« Last Edit: 09 Jun 2014, 12:05 by Creator »

Re: How to make code panel?
« Reply #13 on: 09 Jun 2014, 12:41 »
Ok for a code panel or numbers... the AppendChar seems the easiest function to apply for this.
But what about something like this, I may need it for my main Venice puzzle at the end.


monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: How to make code panel?
« Reply #14 on: 09 Jun 2014, 13:00 »
You could just use the actual sprite slot numbers as the password key. Something like:

Angel Sprite: 72
Catapult Sprite: 43
Chimera Sprite: 115
Correct Password: 4311572

Angel x3 = 727272
Catapult-Chimera-Chimera = 43115115
Chimera-Angel-Chimera = 11572115

You could simply use:

Code: Adventure Game Studio
  1. sPassword.Append(String.Format("%d", clickedPanel.Graphic));

where clickedPanel is an Object* to whichever panel was most recently clicked on. This approach could be problematic if you have, say, a fourth sprite slot number 243, because then the sequence "24311572" would end with "4311572". So, you'd have to watch out for that caveat and maybe change some sprite numbers, but otherwise it's functionally the same.

Re: How to make code panel?
« Reply #15 on: 09 Jun 2014, 13:07 »
The basic idea is to have an array of sprite slots and a variable for each mural:
Code: Adventure Game Studio
  1. // sprite slots of each mural, 0 = angel, 1 = catapult, etc., 6 total
  2. #define murals 6
  3. int mural_slots[murals];
  4.  
  5. enum Mural {
  6.   eMuralAngel,
  7.   eMuralCatapult,
  8.   eMuralDragon,
  9.   ...
  10. };
  11.  
  12. // 0: left mural, 1: center mural, 2: right mural
  13. int mural[3];
  14.  
  15. // in room_Load
  16.   mural_slots[eMuralAngel] = 132; // angel sprite
  17.   mural_slots[eMuralCatapult] = 133; // catapult sprite
  18.   ...
  19.   // set initial configuration
  20.   mural[0] = eMuralAngel; // left mural is showing angel initially
  21.   mural[1] = eMuralCatapult;
  22.   mural[2] = eMuralDragon;
Now implement each mural as an Object.
Code: Adventure Game Studio
  1. function oLeftMural_Interact() {
  2.   // walk to mural, etc.
  3.   // advance to next mural
  4.   mural[0] = (mural[0] + 1) % murals; // 6 wraps to 0
  5.   // update object graphic
  6.   oLeftMural.Graphic = mural_slots[mural[0]];
  7.   // check for combination
  8.   if (check_murals()) open_door();
  9. }

The check is simple:
Code: Adventure Game Studio
  1. bool check_murals() {
  2.   // combination:
  3.   return mural[0] == eMuralWhatever && mural[1] == eMuralGrail && mural[2] == eMuralDragon;
  4. }
Fail at Floaty Rog' now!  still having to deal with what games are going through

Re: How to make code panel?
« Reply #16 on: 09 Jun 2014, 13:08 »
Fantastic :) You are a treasure of coding!