Page 1 of 1

Need lots of help on an A.I. Code

PostPosted: Sun Jul 22, 2007 6:49 pm
by Freddy
I need an A.I. code for the computer opponent in a game Im making. I need it to punch or kick when it gets close enough to my player, and move to it only on the x axis, and only when it comes into view. Also, how can I make the opponent change animations occording to the direction it is going in.
Just the thought of making a script for this makes me feel sick! :wink: So if anyone can explain this to me, please make it as simple as possible (I'm new).

PostPosted: Mon Jul 23, 2007 2:04 am
by DilloDude
Here's a simple code. You may want to customize it somewhat.

Make sure the animations on the enemy are in this order: stopleft, stopright, walkleft, walkright, attackleft, attackright. It doesn't matter, except that it will effect the animindex values. So if the anims are in a different order, you will need to modify some values.
First, set up the basic collisions on the enemy actor. Then, add a draw actor event->script editor:
Code: Select all
if (abs(player.xscreen - xscreen) < 200)//range of enemy
{
    if (player.xscreen > xscreen + 20)//adjust the 20, this is distance before attacking
    {
        if (animindex < 3)
        {
            ChangeAnimation("Event Actor", "WalkRight", FORWARD);
            x += 5;
        }
    }
    else if (player.xscreen > xscreen)
    {
        if (animindex != 5)
        {
            ChageAnimation("Event Actor", "AttackRight", FORWARD);
        }
    }
    else if (player.xscreen < xscreen - 20)
    {
        if (animindex < 2 || animindex == 4)
        {
            ChangeAnimation("Event Actor", "WalkLeft", FORWARD);
            x -= 5;
        }
    }
    else
    {
        if (animindex != 4)
        {
            ChangeAnimation("Event Actor", AttackLeft", FORWARD);
        }
    }
}
else
{
    switch(animindex)
    {
        case 3: case 5:
        ChangeAnimation("Event Actor", "StopRight", FORWARD);
        break;

        case 2: case 4:
        ChangeAnimation("Event Actor", StopLeft", FORWARD);
        break;
    }
}

on animation finish->AttackRight->ChangeAnimation->StopRight->FORWARD.
on animation finish->AttackLeft->ChangeAnimation->StopLeft->FORWARD.

PostPosted: Wed Jul 25, 2007 4:26 pm
by Freddy
I tried the script out, and found that the part that makes the enemy move towards me when I get to a certain distance works fine. However, it wont attack me or change its animation at all. Also, when the player moves to the right, a gap forms between the two actors before the opponent starts to follow, but when I move it to the left, there is no gap and the opponent follows with no delay. I tried adjusting the script but it wouldnt work. What should I do?

PostPosted: Thu Jul 26, 2007 3:43 am
by DilloDude
Hmmm... I'll check it out in a bit and see if I can get it to work.

PostPosted: Thu Jul 26, 2007 3:19 pm
by Freddy
Thanks! :D

PostPosted: Sun Jul 29, 2007 8:35 pm
by Freddy
Never Mind! I figured it out. All this time I mixed up the animindex! :oops:
Thanks so much! :D :D :D

PostPosted: Sun Jul 29, 2007 9:28 pm
by pavel329
oooh just what ive needed.thanx dillo dude.

PostPosted: Mon Jul 30, 2007 3:33 am
by arcreamer
hey guys, i have a question... ive been learning about using game editor and have been working on games and making simple codes for them for about a month and a half or something like that and im wondering well. you see... im using little codes that ive been learning about that past month or so and then i get on the forums and see codes VERY long and VERY complicated and they are like... "i just made this code and am wondering if i didnt make a variable rihgt" or somethine like that and then i see people responding" oh u did everything thing right except change the ending "029384malskdjf0934ujaosjdf" to "w3r9sdjflk09q3ujasd" (being sarcastic) but HOW IN THE WORLD AND WHERE DID U LEARN HOW TO WRITE ALL THIS CODE??? i want to learn it! =P so please help me out on code or something please!

PostPosted: Mon Jul 30, 2007 4:04 am
by DilloDude
I was a bit like that when I first started. There were a lot of functions and keywords I did not know. Basically, learning comes from experience. If you have a problem, search the forums to see if its been answered before. If it hasn't, ask it. As I'm sure you are aware, there are lots of really helpful people here to help you. One important thing though - when you have a code that works (or should work) and you don't understand it, then ask. Most likely whoever wrote it knows how it works. If you don't understand how a code works, chances are you won't remeber it as easily, and you won't be able to implement that code into other circumstances.
In general, keep practising with what you know, and find out how things that you don't know work.

PostPosted: Mon Jul 30, 2007 3:39 pm
by arcreamer
ok thanks