Help With Basic Custom Functions

Started by Avirosb, Mon 27/03/2017 16:31:20

Previous topic - Next topic

Avirosb

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

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


and then in the room script say
Code: ags

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.


Cassiebsg

As you said, you need to import the function.  ;)

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

import function nope();


Save, try it.  (nod)
There are those who believe that life here began out there...

Slasher

#2
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

// 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.\"");    
}



dayowlron

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.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Slasher

#4
yeah..It was a typo (laugh) of course it was meant to read 2.... corrected.

cheers

Avirosb

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.


Khris

#6
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.

Also, regarding what your ultimate goal is here, take a look at unhandled_event (scroll down)

SMF spam blocked by CleanTalk