Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Avirosb on Mon 27/03/2017 16:31:20

Title: Help With Basic Custom Functions
Post by: Avirosb on Mon 27/03/2017 16:31:20
I really wish I'd avoid having to come begging for help but I'm at my wit's end here.
Basically, I thought It'd be a good idea to have a basic response for my character not wanting to interact with various characters, hotspots, objects, etc.
Something as simple as him saying "No, I won't do that" and nothing more.

But, I seem unable to comprehend very basic instructions, and it doesn't help that I find programming (and math) somewhat daunting.
Also doesn't help that I've found multiple conflicting statements on how to do things,
though I know there are many was to accomplish the same goal. I struggle to find even ONE.

Now, I know that I'm supposed to reference the custom function from the global script, something like
Code (ags) Select

function nope()
{
cCharacter.Say ("Nope.");
}


and then in the room script say
Code (ags) Select

function hThing_Look()
{
nope();
}


Or at least I thought it was that simple, but then I read about having to put 'import/export' or something ahead of the function I'm trying to call.
So to summarzie: Not only do I don't know what I'm doing wrong, I just don't know how to do it, period.
If anyone would be so kind as to attempt to guide me through this little conundrum step-by-step, I will be very thankful and make you a character sprite if you like.
Title: Re: Help With Basic Custom Functions
Post by: Cassiebsg on Mon 27/03/2017 16:36:40
As you said, you need to import the function.  ;)

Open the header of Global Script, and add the following line:
Code (ags) Select

import function nope();


Save, try it.  (nod)
Title: Re: Help With Basic Custom Functions
Post by: Slasher on Mon 27/03/2017 16:44:52
Or you can even make a new script in the script tree (give it a name) and add the function there (header script) and of course you can even add random responses instead of getting the same one. You simply call the function as usual.

Example... Made new script named Pick_Random and added this to script Header:

Code (ags) Select

// new module header

function Pick_Random()
{
int Pickran=Random(2);

if(Pickran==0)
cScuminator.SayAt(10, 40, 300,"\"Why would I want to pick that up.\"");
else if(Pickran==1)
cScuminator.SayAt(10, 40, 300, "\"I'm not picking that up.\"");   
else if(Pickran==2)
cScuminator.SayAt(10, 40, 300, "\"Picking that up would be a waste of time.\"");   
}


Title: Re: Help With Basic Custom Functions
Post by: dayowlron on Mon 27/03/2017 17:23:31
slasher, 1/4 of the time that function won't do anything. Keep in mind Random(3) picks a number between 0 and 3 inclusively so 1/4 of the time that function could pick 3 for Pickran. Other than that I like the solution.
Title: Re: Help With Basic Custom Functions
Post by: Slasher on Mon 27/03/2017 17:32:12
yeah..It was a typo (laugh) of course it was meant to read 2.... corrected.

cheers
Title: Re: Help With Basic Custom Functions
Post by: Avirosb on Tue 28/03/2017 15:56:15
Well I'll be... Thanks y'all, that worked!
There's always "that one thing" that's so easy to overlook, but at least now I know what the header is for  :)

If any of you want me to make you something, don't hesitate to PM.
Title: Re: Help With Basic Custom Functions
Post by: Khris on Wed 29/03/2017 09:50:40
Just for reference, the manual's scripting part explains custom functions in "Scripting tutorial part 2":
QuoteUsing functions from room scripts

You may notice that when you add your own function to your global script, you can call it fine from other places in the global script but attempting to use it in a room script gives a parse error. The manual explains how to solve this using the script header.

The mentioned explanation is closely following, titled Calling global functions from local scripts (http://www.adventuregamestudio.co.uk/manual/ags34.htm#topic41).

Also, regarding what your ultimate goal is here, take a look at unhandled_event (http://www.adventuregamestudio.co.uk/manual/ags42.htm) (scroll down)