Scripting Help- player type in numbers

Started by The_Creep, Wed 24/03/2004 11:35:38

Previous topic - Next topic

The_Creep

Hi all

I'm working on a room currently that has a safe, the player has previosly aqquired a riddle that will give him the combination to the safe, what I can't figure out how to do is when the player interacts with the safe, I want to bring up a GUI or the parse that asks him for three separate digits and then if all three are correct, play the "open safe" animation and assign him points
If you had nothing left, would you risk it all to change everything?

Radiant

Create a ten-button Gui that looks like a keypad, and pop it up when touching the safe.
Then it's a simple matter of ifs.

Code: ags

function on_gui_click [sp] 
...
if (gui == SAFE_GUI) {
if (button == 1 && sequence == 0) sequence ++;
else if (button == 3 && sequence == 1) sequence ++;
else if (button == 8 && sequence == 2) play_open_safe_stuff ();
else sequence = 0;
}


The_Creep

Hey man, thanks, this looks great! I bashed together a very crude solution to my problem, and was settling for it and found your reply, I think your idea of a Keypad is much more smooth. but here is my code for interests sake
(how did you put your code in that neat little box?)

 // script for Safe: Interact object (inputting the combination for the safe)
string user1,user2,user3;
int code1,code2,code3;
InputBox("Dial the first number:", user1);
code1=StringToInt(user1);
InputBox("Dial the second number:", user2);
code2=StringToInt(user2);
InputBox("Dial the third number:", user3);
code3=StringToInt(user3);
   if (code1==11) {
     if (code2==22) {
       if (code3==33) {
         Display("*Click* The safe opens");
         SetObjectView(4,2);
         AnimateObject(4,1,0,0);
}
}
}
if (code1!=11){
 Display("Your attempt to open the safe failed.");
 }
else if (code2!=22){
 Display("Your attempt to open the safe failed.");
 }
else if (code3!=33){
 Display("Your attempt to open the safe failed.");
 }
If you had nothing left, would you risk it all to change everything?

TerranRich

Optimize your code, man! :P

[size=+0]
 // script for Safe: Interact object (inputting the combination for the safe)
string user1,user2,user3;
int code1,code2,code3;
InputBox("Dial the first number:", user1);
code1=StringToInt(user1);
InputBox("Dial the second number:", user2);
code2=StringToInt(user2);
InputBox("Dial the third number:", user3);
code3=StringToInt(user3);
  if ((code1==11) && (code2==22) && (code3==33)) {
    Display("*Click* The safe opens");
    SetObjectView(4,2);
    AnimateObject(4,1,0,0);
  }
if ((code1!=11) || (code2!=22) || (code3!=33))
  Display("Your attempt to open the safe failed.");
[/size]

&& means "and" and || means "or" :P
Status: Trying to come up with some ideas...

Gilbert

Please, Terran, there's nothing wrong with people writing unoptimised codes. As long as one gets his codes to work, it's a fullfillment already.

You may make suggestions for improvement, but please don't criticise other for writing codes that work but not optimised.

The_Creep

Well I honestly did'nt know the functions of && and ||! so I'm grateful for his nit picking :)
If you had nothing left, would you risk it all to change everything?

Alynn

Kinda off topic,

But optimization of code is somethig that comes with time and experience, I know back when I was first learning basic at the age of 8 I wrote monstrosities of code that did very simple things... In college learning pascal, I found myself getting much better at it (and doing things in my 101 class I wasn't going to learn how to do "officially" until my 102 class, such as recursive calling of functions).

Now I find myself optimizing everything when I code (for the most part) which I actually attribute to being lazy, the more optimized it is, the less typing I have to actually do...

Now that I think about it... I think I'm just lazy.

Pumaman

Quote from: terranRICH on Thu 25/03/2004 04:14:40
Optimize your code, man! :P

If you're going to talk about optimisation, you could at least do it properly:

[size=+0]
 // script for Safe: Interact object (inputting the combination for the safe)
string temp;
int code1,code2,code3;
InputBox("Dial the first number:", temp);
code1=StringToInt(temp);
InputBox("Dial the second number:", temp);
code2=StringToInt(temp);
InputBox("Dial the third number:", temp);
code3=StringToInt(temp);
if ((code1==11) && (code2==22) && (code3==33)) {
    Display("*Click* The safe opens");
    SetObjectView(4,2);
    AnimateObject(4,1,0,0);
  }
else
  Display("Your attempt to open the safe failed.");
[/size]

:P


That aside, Gilbert's point is a good one. Most people here using AGS are not software engineers; besides the fact that any optimisations you make to your scripts are likely to have negligable effects on the game speed (just 5% of the engine time is spent running scripts) so it's not something that people should be overly concerned about.

If you're going to go to the trouble of optimising code, repeatedly_execute is probably the only place it's worthwhile.

As Alynn says though, over time you pick up tricks and better ways of coding, and start scripting more efficiently out of habit.

SMF spam blocked by CleanTalk