Extender Functions

Started by bauldur_rises, Sun 25/06/2017 06:56:48

Previous topic - Next topic

bauldur_rises

Please forgive my ignorance, but I'm trying to get my head around basic extender functions and having trouble.  I've currently got this going:

Code: ags

//global header
import function FaceDirectionWithoutTurn (this Character*, CharacterDirection, BlockingStyle);

// global script

 function FaceDirectionWithoutTurn (this Character*, CharacterDirection, BlockingStyle)
{
  SetGameOption (OPT_TURNWHENFACING , 0);
  this.FaceDirection(CharacterDirection, BlockingStyle);
  SetGameOption (OPT_TURNWHENFACING , 1);
}
{/code]

But it returns the error "GlobalScript.asc(2146): Error (line 2146): Already referenced name as import; you must define it before using it
"  - line 2146 being " function FaceDirectionWithoutTurn (this Character*, CharacterDirection, BlockingStyle)".

I feel like there's some basic concept I'm missing - can anyone help me?

Thanks

Cassiebsg

Are you using the function in any other line above?

Try moving the function to the top of global scripts and see if that helps.
There are those who believe that life here began out there...

bauldur_rises

Thank you for the advice, I have moved it up the top, but now I get the error "static non-property access: internal error
"

Cassiebsg

#3
Sorry, don't know what that means, you'll have to wait until someone with more knowledge of the engine to see your thread and reply.
Might mean you are using some code wrong, though...
Think it's telling that it can not change a something that is static (as in, not-changeable)...

Check the manual. Or try to comment out the two lines SetGameOption... have a feeling these might be the problem.

EDIT: I see monkey posted in the mean time. (nod)
There are those who believe that life here began out there...

monkey0506

Your code from the first post, with fixed [code][/code] tags:

Code: ags
//global header
import function FaceDirectionWithoutTurn (this Character*, CharacterDirection, BlockingStyle);
 
// global script
 
 function FaceDirectionWithoutTurn (this Character*, CharacterDirection, BlockingStyle)
{
  SetGameOption (OPT_TURNWHENFACING , 0);
  this.FaceDirection(CharacterDirection, BlockingStyle);
  SetGameOption (OPT_TURNWHENFACING , 1);
}


CharacterDirection and BlockingStyle are the names of enums, and are used exactly the same way as you would use int. Namely, you need to give your parameters a name in the function definition:

Code: ags
 function FaceDirectionWithoutTurn (this Character*, CharacterDirection direction, BlockingStyle block)
{
  SetGameOption (OPT_TURNWHENFACING , 0);
  this.FaceDirection(direction, block);
  SetGameOption (OPT_TURNWHENFACING , 1);
}


imports do not need to have named parameters, and for enums it is conventional to not give them names in the import unless their use is ambiguous. However, in the function definition your parameters must have names or you can't access the value passed in.

bauldur_rises

Thank you very much, monkey0506, that seems to have fixed it!

However, another problem is emerging with another extend function I was playing with:

Code: ags

//header
  enum PickupHeight {
    eLow,
    eMid,
    eHigh
  };

 import function Pickup (this Character*, PickupHeight h, AudioClip);

//script
function Pickup (this Character*, PickupHeight h, AudioClip audio)
{
  if (this == cJosephus)
   {
    if (h == eLow)
    {
      this.ChangeView (11);
    }
  
    if (h == eMid)
    {
      this.ChangeView (4);
    }
    
    if (h == eHigh)
    {
      this.ChangeView (12);
    }
   }
   
 Wait (10);
 audio.Play(eAudioPriorityVeryHigh);
 Wait (10);
 
  if (this == cJosephus)
   {
     this.ChangeView (2);
   }
}



When I try and use the function as such:

Code: ags

player.Pickup(eHigh, aBLANKSLUG);


It tells me "Error (line 146): Type mismatch: cannot convert 'AudioClip*' to 'AudioClip'
"

I appreciate any help, as I am just a beginner.

Crimson Wizard

When passing objects as parameters you must add "*" to the type, e.g. "AudioClip *audio" instead of "AudioClip audio", and so on. This means "pointer to object".

bauldur_rises

That works, thank you Crimson Wizard!

SMF spam blocked by CleanTalk