How to set an event and action with a textbox input

Started by mrsix, Wed 08/10/2008 18:26:17

Previous topic - Next topic

mrsix

Hello there, new to this program and am having some problems.

I have some experience using Pascal Programming and Visual Basic, but am having problems working out how to do something.

In my game, the character goes up to a computer, and there is a text box which he types in. I can make him type in the textbox but havent got as far with the code.
Depending on what he types in, different things come back.

Now, how would you set up a basic script so that when enter is pressed, it checks to see what is typed in the textbox, and then does the action statement.
Im guessing I will need a variable of sorts.


For instance "if textbox says 'murders', show newspaper picture. etc..

Sorry, I know this is probabl very basic preliminary coding, but it has been a long time, and I have never used this program before! The manual is very helpful but obviously doesnt cater for complete noobs in programming ;o)

I would be so grateful for any examples ;o)

Thanking you (and hi!)

TwinMoon

The variable is already supplied by AGS:

select the text box, then go to the 'events' tab (it's the button in the right bottom with the lightning bolt on it) then click the button behind the OnActivate.
OnActivate gets called after the player pressed enter.

you should go to the global script, and see this:
Code: ags

function TextBox1_OnActivate(GUIControl *control) {

}


then you can put this code between the brackets:
Code: ags

if (TextBox1.Text == "hello") {
  //insert code here
}

and so on.

If you changed the TextBox's name, replace the word TextBox1 with the name you gave the Textbox (the property 'name' in the properties tab)

mrsix

Thanks ever so much for your help, it's starting to make alot more sense now ;o)

I've stumbled across a further problem though!


Here is my script so far:

function TextBox1_OnActivate(GUIControl *control) {
if (TextBox1.Text == "murder"){
  Newsclip1.Visible = true;
  }

}

however, it cannot find my newsclip sprite, even though I have named it exactly as it appears on the script. It keeps saying 'undefined token Newsclip1', even though it is Named exactly that.
Is there a reason for this?

Sorry to keep asking so many questions

Khris

Assuming Newsclip1 is an object: you can't access those by name from the global script, since they're room-specific.
You'd use the object array instead:
Code: ags
  if (player.Room == 5) object[3].Visible = true;       // access object 3 in room 5

mrsix

Thankyou! That makes sense that objects are only available room-specific.

Now the code debugs and compiles which is good (i am not using the array as I think it might confuse things in this instance).

HOwever... the object does not appear! I have it set to invisible on properties, so in theory it should appear.

Im sure its something very silly!

Note - I have copied and pasted the code into the Room Script instead if that is ok

Gilbert

Can you post your current codes again?
Quote from: mrsix on Thu 09/10/2008 08:57:17
(i am not using the array as I think it might confuse things in this instance)
I don't quite understand what this means, but if you need to refer to the object in the Global script you need to use the array. If you just put "object.Visible" it would not work (in case it slips through the compiler and does not generate an error).

Khris

Quote from: mrsix on Thu 09/10/2008 08:57:17
Note - I have copied and pasted the code into the Room Script instead if that is ok

No, it isn't.
Unlike e.g. objects, GUI stuff is global and corresponding functions thus need to be in the global script.
If you're afraid of confusing things, comment your code (like I did in the example).

mrsix

Thank you so much all of you, you are all genius! (or is that geni??)

I was not aware of the object array, it works perfectly, thanks again  ;D

Here is what I did in the end :

function TextBox1_OnActivate(GUIControl *control) {
String s=control.AsTextBox.Text;
if (s=="murder") object[0].Visible=true;
 
}

Dualnames

Quote from: mrsix on Sun 12/10/2008 11:50:13
function TextBox1_OnActivate(GUIControl *control) {
String s=control.AsTextBox.Text;
if (s=="murder") object[0].Visible=true;
 
}

Not really sure about what I'm about to suggest but well in my parsers I usually use the following code, which actually allows player to type MuRdeR or more commonly Murder and the parser to accept it as murder, which in your case doesn;t(at least I think so).

Anyway so here's the code if you want it:

function TextBox1_OnActivate(GUIControl *control) {
String s=control.AsTextBox.Text;
if (s.Contains("murder")!=-1)  {
object[0].Visible=true;
TextBox1.Text="";//this clears the text
return;
}
if (s.Contains("murder")==-1)  {
//have something like no you sort of messed up message)
TextBox1.Text=""//this clears the text
return;
}
}

I think that's not of much help but anyway.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

mrsix

It is of extreme help, I haven't done programming for many years so everything helps (i didnt think of using "" to clear the text!!)

Thankyou  :)

Dualnames

Quote from: mrsix on Sun 12/10/2008 13:27:01
It is of extreme help, I haven't done programming for many years so everything helps (i didnt think of using "" to clear the text!!)

Thankyou  :)

Glad to hear that.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

SMF spam blocked by CleanTalk