Facing direction function

Started by Ethan D, Thu 03/09/2009 01:22:20

Previous topic - Next topic

Ethan D

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?

Wonkyth

"But with a ninja on your face, you live longer!"

Ethan D

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

Khris

#3
Create an extender function.
(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.

Code: ags
function FaceDirection(this Character*, eDir dir) {
  if (dir == eDirLeft) this.FaceLocation(this.x - 1, this.y);
  if (dir == eDirUp) ...
}

Wonkyth

Also, try looking at the "Pointers for Dummies" page in the AGS Wiki.
"But with a ninja on your face, you live longer!"

Ethan D

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.



Matti

Why can't you use the function?

Make sure you write it like this:

cWhoever.FaceDirection(Left);

Ethan D

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)?



Matti

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

Ethan D

I'm sorry but how do I do that?

Edit: I have it in the global script.

Wonkyth

#10
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,
Code: ags
import function functionname (whatever parameters);

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

import function FaceDirection(this Character*, edir dir);
"But with a ninja on your face, you live longer!"

Ethan D

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!

Wonkyth

Beaten Again!
I need to speed up my typing...
"But with a ninja on your face, you live longer!"

Khris

Move the enum declaration to the script header.

SMF spam blocked by CleanTalk