Suggestion about characters turning

Started by BlackBaron, Tue 06/05/2003 22:23:20

Previous topic - Next topic

BlackBaron

The option "Characters turn before walking" is really cool, unfortunately it only works when you walk. It would be nice to have a similar option for the functions FaceLocation and FaceCharacter, maybe 2 new functions: TurnToLocation and TurnToCharacter.


Thanks a lot.
"Do you thirst for knowledge no matter the cost?"
            -Watrik, master glassblower

FenderQ

#1
That's funny that you mention that.  In my latest game I decided to quickly make a custom function sort of like what you mentioned.  The function is called TurnCharacter(CHARID, int direction) where you pass it the CHARID and a number from 1 to 4.  I guess I could have used words for directions, but I myself didn't have a problem using numbers.  Hope some of this helps you!

Note: FenderQ is left handed so he turns to the left when trying to reach his direction, this code could be modified for the right handed person.  You could use random() for some real mind boggling activity!   ;D

1 = Up
2 = Right
3 = Down
4 = Left

//  I placed this in my Global Script

function TurnCharacter(int name, int direction) {
 if ((character[name].loop == 0) && (direction == 1)) {
   FaceLocation(name,character[name].x+1,character[name].y);
   Wait(5);
   FaceLocation(name,character[name].x,character[name].y-1);
 }
 else if ((character[name].loop == 1) && (direction == 2)) {
   FaceLocation(name,character[name].x,character[name].y+1);
   Wait(5);
   FaceLocation(name,character[name].x+1,character[name].y);
 }
 else if ((character[name].loop == 3) && (direction == 3)) {
   FaceLocation(name,character[name].x-1,character[name].y);
   Wait(5);
   FaceLocation(name,character[name].x,character[name].y+1);
 }
 else if ((character[name].loop == 2) && (direction == 4)) {
   FaceLocation(name,character[name].x,character[name].y-1);
   Wait(5);
   FaceLocation(name,character[name].x-1,character[name].y);
 }
 else if (direction == 1) { FaceLocation(name,character[name].x,character[name].y-1); }
 else if (direction == 2) { FaceLocation(name,character[name].x+1,character[name].y); }
 else if (direction == 3) { FaceLocation(name,character[name].x,character[name].y+1); }
 else if (direction == 4) { FaceLocation(name,character[name].x-1,character[name].y); }
 else {
   FaceLocation(name,character[name].x,character[name].y+1);
   DisplaySpeech(name,"You want me to turn using a directional parameter of %d?",direction);
   DisplaySpeech(name,"I think you better have another look at your programming code.");
 }    
}

//  And I placed this in my Script Header

import function TurnCharacter(int, int);

BlackBaron

Nice, really nice...  :D

Thanks a lot  ;D
"Do you thirst for knowledge no matter the cost?"
            -Watrik, master glassblower

scotch

shouldn't that be

import function TurnCharacter(int, int);

in the script header?
Don't know if it makes a difference..

Nice script btw ;)

FenderQ

#4
scotch: I just finished testing my code above and it worked ok for me with the Script Header import definition like I did.  It might work like you did it maybe too.  The big part which took me a while was figuring out about passing the CHARID as an int which RickJ pointed out a while back.

BlackBaron

It worked really well, thanks, again.
I'll just add the following in case somebody else needs this.
You may define constanst for the directions: add the following to the script header:

#define D_UP 1
#define D_RIGHT 2
#define D_DOWN 3
#define D_LEFT 4

(those or any names you like, I just added the D_ because it seems there's already a constant RIGHT, but you could simply don't define RIGHT  again and use it with the value it already has).

I also added the folowing functions, I guess somebody may find them useful too:

//Function that turns character 'name' to a certain location
function TurnToLocation(int name1, int x, int y) {
 int dx = character[name1].x - x;
 int dy = character[name1].y - y;
 int direction;

 //Chooses direction
 if ((dx*dx)>(dy*dy)) {
   if (dx>=0) direction = D_LEFT;
   else direction = D_RIGHT;
 }
 else {
   if (dy>=0) direction = D_UP;
   else direction = D_DOWN;
 }
 
 TurnCharacter(name1, direction);
}

//Function that turns character 'name1' to character 'name2'
function TurnToCharacter(int name1, int name2) {
 int x = character[name2].x;
 int y = character[name2].y;
 
 TurnToLocation(name1, x, y);
}

with the appropiate imports in the header script:

import function TurnToLocation(int, int, int);
import function TurnToCharacter(int, int);

They seem to work quite well but the way direction is choosen in the function TurnToLocation could be improved a bit (as soon as I get some extra time I'll do it and post it here).
"Do you thirst for knowledge no matter the cost?"
            -Watrik, master glassblower

FenderQ

Right on! using those defines is a really good idea.   :D

I'm surprised my import definitions in the Script Header worked ok.  Do you guys normally just specify the variable types for your custom functions when placed in the Script Header?  For example import function MyFunc(int, int); without actually giving it the variables names too?

BlackBaron

Yep, the manual actually tells that's the way to do it, but since you haven't got any problems with your way, it seems to me that it's equally right.
(Just note that not adding the names of the variables only works for the imports).
"Do you thirst for knowledge no matter the cost?"
            -Watrik, master glassblower

FenderQ

#8
Oh ya, your right.  In the section Calling global functions from local scripts.  I think I might change my code to reflect what the documentation says, just to be on the safe side  ;)

Pumaman

You can put variable names in there if you want (it helps you to remember which argument means what) - in an import statement they are simply ignored if you do put them in.

For reference, this suggestion is already in the tracker:
http://www.agsforums.com/tracker.php?action=detail&id=18
to be added to the engine at some point - although FenderQ has found a good scripting workaround. Won't work for 8-directional characters of course :)

SMF spam blocked by CleanTalk