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?
Use a character pointer!
;)
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
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) ...
}
Also, try looking at the "Pointers for Dummies" page in the AGS Wiki.
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.
Why can't you use the function?
Make sure you write it like this:
cWhoever.FaceDirection(Left);
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)?
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..
I'm sorry but how do I do that?
Edit: I have it in the global script.
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);
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!
Beaten Again!
I need to speed up my typing...
Move the enum declaration to the script header.