Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: mrsix on Sun 19/10/2008 19:46:09

Title: Calling a function to run which is already defined not working
Post by: mrsix on Sun 19/10/2008 19:46:09
I will give an example.

I have this small script:

-----------------------------
function TelDial_OnClick(GUIControl *control, MouseButton button) {
if (PhoneLabel.Text=="5551552")
{
PlaySound (5);
Wait(160);
cEgo.ChangeRoom(3);
gGui3.Visible=false;

}

else
{PlaySound (4);
Display ("Wrong Number");
PhoneLabel.Text=("");
}
}
-------------------------------

Basically, when I press the button, that code works.
But, I want this piece of code to work everytime there is a variable which hits a certain number, but without having to press the button, so in theory you can just call the name of the function and it will run anytime. So I have this peice of code to check it:

--------------
function repeatedly_execute() {
   
  if (TelephoneCounter==7){
TelDial_OnClick;}
  }
-----------

Everytime I try to run it says 'GlobalScript.asc(105): Error (line 105): Undefined token 'TelDial_OnClick'


I check the userguide and it says about importing/exporting functions, but both of these bits of code are in the global script, so my understanding is that this is not necessary.
My first thought is that  '_OnClick' is an actual peice of code, so I tried renaming the function, it still didnt work.

Any ideas? Sorry if this a stupid question, normally its something silly (always the way!)

Thankyou for your help
Title: Re: Calling a function to run which is already defined not working
Post by: Khris on Sun 19/10/2008 19:54:42
A function must be defined before it can be called.
Just move your function to the top of the global script (or above rep_ex).
Title: Re: Calling a function to run which is already defined not working
Post by: mrsix on Sun 19/10/2008 20:00:17
THank you for your help!

I think I got further with it, but now I have this error message:
"GlobalScript.asc(122): Error (line 122): Not enough parameters in call to function"

The Rep_ex code says:
--
function repeatedly_execute() {
   
  if (TelephoneCounter==7){
TelDial_OnClick();
TelephoneCounter=0};}
---

I dont understand the error message really. Does it mean not enough parameters define when the function should be called? It seems straight forward in the code!
Title: Re: Calling a function to run which is already defined not working
Post by: Pumaman on Sun 19/10/2008 23:29:09
The OnClick function is defined like this:

function TelDial_OnClick(GUIControl *control, MouseButton button) {

and that's how it has to be, because it is a built-in event handler. If you want to call it manually, you would need to do:

TelDial_OnClick(TelDial, eMouseLeft);

ie. fill in those two parameters.
Title: Re: Calling a function to run which is already defined not working
Post by: mrsix on Mon 20/10/2008 16:11:54
Thank you, I have got it fixed thanks to your reply.
It works fine! My only problem is that I dont understand what the brackets actually refer to (telDial, Emouseleft), but who cares, it works now!! I shall read up on this and find out.

Thanking you for your assistance  :)
Title: Re: Calling a function to run which is already defined not working
Post by: Trent R on Mon 20/10/2008 18:02:48
The two parameters were needed, and so you had the fill in the what was required in the function, a GUIControl (such as your button, TelDial) and a MouseButton (eMouseLeft just says that you've click with the left mouse button.)


~Trent
Title: Re: Calling a function to run which is already defined not working
Post by: Khris on Mon 20/10/2008 19:05:08
You can click GUIControls with the right mouse button, too, so one might choose not to react/react differently to a right-click.
The clicked GUIControl is passed to the function to react individually if one has many similar buttons (e.g. a keypad) and uses the same OnClick function for all of them.