Need lots of help on an A.I. Code

Non-platform specific questions.

Need lots of help on an A.I. Code

Postby Freddy » Sun Jul 22, 2007 6:49 pm

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).
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score

Postby DilloDude » Mon Jul 23, 2007 2:04 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Freddy » Wed Jul 25, 2007 4:26 pm

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?
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score

Postby DilloDude » Thu Jul 26, 2007 3:43 am

Hmmm... I'll check it out in a bit and see if I can get it to work.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Freddy » Thu Jul 26, 2007 3:19 pm

Thanks! :D
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score

Postby Freddy » Sun Jul 29, 2007 8:35 pm

Never Mind! I figured it out. All this time I mixed up the animindex! :oops:
Thanks so much! :D :D :D
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score

Postby pavel329 » Sun Jul 29, 2007 9:28 pm

oooh just what ive needed.thanx dillo dude.
Drugs r bad.
Mgaay?

Image
User avatar
pavel329
 
Posts: 269
Joined: Thu May 25, 2006 2:05 pm
Location: Behind you
Score: 5 Give a positive score

Postby arcreamer » Mon Jul 30, 2007 3:33 am

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!
arcreamer
 
Posts: 398
Joined: Tue Jul 03, 2007 4:08 pm
Score: 9 Give a positive score

Postby DilloDude » Mon Jul 30, 2007 4:04 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby arcreamer » Mon Jul 30, 2007 3:39 pm

ok thanks
arcreamer
 
Posts: 398
Joined: Tue Jul 03, 2007 4:08 pm
Score: 9 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest