Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Ethan D on Thu 03/09/2009 01:22:20

Title: Facing direction function
Post by: Ethan D on Thu 03/09/2009 01:22:20
I'm sure this is relatively simple but it is my first time writing a function.  I'm trying to write a function to make it easier to make someone face another direction.  I know how I could set it up for one character but I can't figure out how to make a single function be able to work for all characters.  Any ideas?
Title: Re: Facing direction function
Post by: Wonkyth on Thu 03/09/2009 01:34:05
Use a character pointer!
;)
Title: Re: Facing direction function
Post by: Ethan D on Thu 03/09/2009 01:58:20
I'm not sure how I got through a whole game without ever reading about pointers.  I got a lot of helpful stuff from it but I still don't know how I could apply pointers to this problem.

I'm also not sure if I'm even setting up the function right so far.

function faceup (char)
{
I have no idea what to put here
}

Ugh... I wish I had learned this earlier
Title: Re: Facing direction function
Post by: Khris on Thu 03/09/2009 01:59:53
Create an extender function (http://www.adventuregamestudio.co.uk/manual/ExtenderFunctions.htm).
(You could always pass the character or their ID to the function via a parameter though.)

To do it properly, create an enum for the four (eight) directions and pass that.
Inside the function, check for the parameters value and make the character face accordingly.

function FaceDirection(this Character*, eDir dir) {
  if (dir == eDirLeft) this.FaceLocation(this.x - 1, this.y);
  if (dir == eDirUp) ...
}
Title: Re: Facing direction function
Post by: Wonkyth on Thu 03/09/2009 02:24:10
Also, try looking at the "Pointers for Dummies" page in the AGS Wiki.
Title: Re: Facing direction function
Post by: Ethan D on Thu 03/09/2009 02:35:11
Here is what I have in the global script.


enum edir { Left, Right, Up, Down};

function FaceDirection(this Character*, edir dir)
{
  if (dir == Left) this.FaceLocation(this.x - 1, this.y);
  if (dir == Up) this.FaceLocation(this.x, this.y - 1);
  if (dir == Down) this.FaceLocation(this.x, this.y + 1);
  if (dir == Right) this.FaceLocation(this.x + 1,  this.y);
}

As near as I can see this is correct and there are no error reports coming up but I can't use the function in AGS.


Title: Re: Facing direction function
Post by: Matti on Thu 03/09/2009 02:39:40
Why can't you use the function?

Make sure you write it like this:

cWhoever.FaceDirection(Left);
Title: Re: Facing direction function
Post by: Ethan D on Thu 03/09/2009 02:41:06
This is the error report that I get...

Error (line 5): '.FaceDirection' is not a public member of 'Character'. Are you sure you spelt it correctly (remember, capital letters are important)?


Title: Re: Facing direction function
Post by: Matti on Thu 03/09/2009 02:46:08
You need to export/import the function if you're using it in some room. Importing it in the global scripts header is sufficient I think..
Title: Re: Facing direction function
Post by: Ethan D on Thu 03/09/2009 02:47:47
I'm sorry but how do I do that?

Edit: I have it in the global script.
Title: Re: Facing direction function
Post by: Wonkyth on Thu 03/09/2009 02:50:23
Have you made sure that FaceDirection is a public member?

I know that sounds like I'm repeating the error, but if it isn't public, then it wont work....

Anyway, check that either:
1. You're calling FaceDirection from the same script that the FaceDirection function is in.
2. You have imported the function in the script header, so it cna be accessed by the script that is calling it.

EDIT: Beaten to it!
To import a function,
import function functionname (whatever parameters);

So in your case, you'd need to put this in the global script header:

import function FaceDirection(this Character*, edir dir);
Title: Re: Facing direction function
Post by: Ethan D on Thu 03/09/2009 02:54:59
I figured it out. I forgot that eNum is a local variable so I had to declare it in the room.  Wow... thanks for all the help, this would have helped greatly with my last game.  Oh well.

Thanks Again!
Title: Re: Facing direction function
Post by: Wonkyth on Thu 03/09/2009 02:57:15
Beaten Again!
I need to speed up my typing...
Title: Re: Facing direction function
Post by: Khris on Thu 03/09/2009 10:28:54
Move the enum declaration to the script header.