"this" keyword in Custom functions [SOLVED]

Started by Recluse, Fri 27/07/2007 02:57:13

Previous topic - Next topic

Recluse

I'm currently working on a function that will clean up my cut scene scripts tremendously. However I'm running into a problem.

In it's simplest form, I want my function to look like this:

Code: ags

function sayHeadLeft(String str) {
	  this.FaceLocation(this.x - 10,  this.y, eBlock);
	  this.Say("%s", str);
	  this.LockViewFrame(this.View, 4, 0);
	}


I want this function to be able to be called BY a character. Like so:

Code: ags

cEgo.sayHeadLeft("Hello, I am speaking to you as my head turns to the left while my body faces forward.");


However, AGS doesn't seem to recognize the "this" keyword. In the script it turns up blue... but when I go to compile, I get an error saying that "this" is an undefined token.

Does this function need to be defined from within a struct? And if so, how do I stick it inside the character struct?

P.S. Doing a search for "this" is generally useless  :-\
All your verbcoin are belong to us.

Gilbert

It's not that simple, you need to do it like this:
Code: ags

struct Blah{
  import static function sayHeadLeft(String str);
};


Code: ags

static function Blah::sayHeadLeft(String str) {
	  this.FaceLocation(this.x - 10,  this.y, eBlock);
	  this.Say("%s", str);
	  this.LockViewFrame(.View, 4, 0);
	}


Moreover, you can't currently extend predefined structs like Character (only the current V2.8 beta supports this feature) so you need to define another struct for it, which is a bit complicated.

I'll just suggest you to do something like this instead at the moment:

Code: ags

function sayHeadLeft(Character* speaker, String str) {
	  speaker.FaceLocation(speaker.x - 10,  speaker.y, eBlock);
	  speaker.Say("%s", str);
	  speaker.LockViewFrame(speaker.View, 4, 0);
	}


Code: ags

sayHeadLeft(cEgo, "Hello, I am speaking to you as my head turns to the left while my body faces forward.");

monkey0506

If you're using one of the 2.8 Beta versions, then you can use an extender method like this:

Code: ags
function sayHeadLeft(this Character*, String str) {
  this.FaceLocation(this.x - 10, this.y, eBlock);
  this.Say("%s", str);
  this.LockViewFrame(this.View, 4, 0);
  }

// and then later use:

cEgo.sayHeadLeft("Hello, I am speaking to you as my head turns to the left while my body faces forward.");


If you're using AGS 2.72 then you'll have to use a parameter for the Character like this:

Code: ags
function sayHeadLeft(Character* whichChar, String str) {
  whichChar.FaceLocation(whichChar.x - 10, whichChar.y, eBlock);
  whichChar.Say("%s", str);
  whichChar.LockViewFrame(whichChar.View, 4, 0);
  }

// and then later use:

sayHeadLeft(cEgo, "Hello, I am speaking to you as my head turns to the left while my body faces forward.");


Beaten to the punch! :P

Monsieur OUXX

Quote from: Gilbot V7000a on Fri 27/07/2007 03:54:02
you can't currently extend predefined structs like Character (only the current V2.8 beta supports this feature)

One more feature that makes AGSH useless. Why did Chris Jones decide to fix all these things at the same time just when i try to workaround them  ::)? I hate him  :)
 

Gilbert

Because the less modules that are used as workarounds, the better. :=

Recluse

#5
Hey! Thanks a lot for the help!

I'm reluctant to use a beta version for a full-scale project. Something might screw up later, so I think I'll go with the hack where you import the character object as suggested.

Now I'm going to go look up what that silly '*' is for...

MOD: This problem is now solved.

P.S. This function REALLY cleans up my dialogue scripts. Thanks everyone for your help!
All your verbcoin are belong to us.

Ashen

If you didn't figure it out, or for future reference, the '*' is how you declare Pointers in AGS.
I know what you're thinking ... Don't think that.

monkey0506

Just in case there's still any confusion after having read the manual entry, I've also written a tutorial on AGS pointers here. ;)

Recluse

#8
Well thank you much! That answers all my questions and more!

MOD: If you feel the need to lock this thread feel free to do so.
All your verbcoin are belong to us.

Ashen

Well, I do enjoy locking threads...
However, we only tend to lock threads that break the rules or don't belong. Ones that're just resolved can be left open, so people with the same or similar problems don't have to start a new one.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk