Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Ai

  1. #1
    Join Date
    Nov 2010
    Location
    Home
    Posts
    69

    Ai

    Im sure this has been covered in the sidescroller series but can anyone help me out. im trying to make it time based like 1 second, it walks towards or away or stands there or hits you if you're within a certain range( which i am still struggling, my sprite class doesn't have any states, clueless on those atm) and i thought it would go somteing like this, but i got way too many errors. pesudocode went something like
    int time = gametime elapsedtime
    switch ( random (0,4)
    case 0 positionx and y towards player.position
    case 1 position.x and .y away from player
    case 2 position.x and .y = zero??
    case 3 i uh think i still need a sprite class with states to show animations but basically, change animation into attacking frames and if collision, hp loss or something.

    I'm not really asking for code, but a general idea on how to go with this... thanks in advance.
    ,___,
    [o.O] - Moo, I'm a cat.
    /)__)
    -"--"-
    ,___,
    [¬.¬] - Dude, you're an owl.
    /)__)
    -"--"-
    ,___,
    [O.O] - My mother has lied to me!
    /)__)
    -"--"-

  2. #2
    Join Date
    Oct 2006
    Posts
    1,548
    I'd use somekind of state system, as already mentioned. Heres a simple approach. Your NPC's ( Non player characters ) need to each have a "Behaviour" component, which acts as their current behaviour state. Where behaviours could be

    - flee
    - follow
    - sleep
    - group

    etc etc

    Code:
    class NPC
    {
         private Behaviour _Behaviour;
    }
    
    class Behaviour
    {
        virtual update();
    }
    
    class Flee : Behaviour;
    {
         update()
       {
             // locate enemy, and move away
        }
    }
    
    class Follow : Behaviour
    {
         update()
       {
             // locate target, and move towards
        }
    }
    
    class Group : Behaviour
    {
         update()
       {
             // locate average position, and move towards
        }
    }
    This behaviour update() function will need to be updated every frame, as it will *update* what the NPC is currently doing. In this case, which direction its moving in.

    State only needs to be updated every so often though, so as you said, one second would be fine. Just create a timer that fires every 1 second. Which you can respond to, and change all the NPC current behaviour states. In your example, with just a random number, it would look something like this.

    Code:
    void OnOneSecondElapse()
    {
           for( all NPC's )
           {
                int r = random();
       
               case r == 0 : NPCList[i].SetBahaviour( new Sleep );
               case r == 1 : NPCList[i].SetBahaviour( new Group );
               case r == 2 : NPCList[i].SetBahaviour( new Flee  );
            }
    }
    
    void GameLoop()
    {
            for( all NPC's )
            {
                NPCList[i].UpdateBehaviour();
            }
    }
    Last edited by wforl; 12-14-2010 at 02:56 PM.
    [quote][\quote]

  3. #3
    Join Date
    Sep 2008
    Location
    Salisbury, UK
    Posts
    2,812
    It sounds as though you might need to go through some or all of XNA 101 before tackling the sidescroller - Jacquesy codes FAST! And I believe that series assumes you already have the basics down (not certain on that, it's been a while since I went through the sidescroller VTMs)...

    I found
    my current avatar on google, so props to THIS GUY who created the original...

  4. #4
    Join Date
    Dec 2007
    Location
    Austin, Texas
    Posts
    499
    I recommend either a behaviour class type thing, or an enum of stats

    public enum AIState {
    WalkToPlayer,
    AttackPlayer, etc....
    };

    and just do a switch in your update.

    But again, both ways work fine.

  5. #5
    Join Date
    Nov 2010
    Location
    Home
    Posts
    69
    yeah its that enum state thing i'm trying to figure out, but i have no clue how to. my teacher's using learning xna 3.0 by reed as a reference and the book doesn't mention states, only gamestates, which i already incorporated, but sprite states with animations, i am clueless! anyone know of a book that teaches sprite enum states before i try membership here.
    ,___,
    [o.O] - Moo, I'm a cat.
    /)__)
    -"--"-
    ,___,
    [¬.¬] - Dude, you're an owl.
    /)__)
    -"--"-
    ,___,
    [O.O] - My mother has lied to me!
    /)__)
    -"--"-

  6. #6
    Join Date
    Sep 2008
    Location
    Salisbury, UK
    Posts
    2,812
    Quote Originally Posted by antiroxas View Post
    anyone know of a book that teaches sprite enum states before i try membership here.
    Surely, most decent textbooks cost about the same as a month of MS...?

    I found
    my current avatar on google, so props to THIS GUY who created the original...

  7. #7
    Join Date
    Nov 2010
    Location
    Home
    Posts
    69
    yeah they do except im still waiting to hear if they charge the credit card or first the amazon gift card value, so i mean, more is better, right?
    ,___,
    [o.O] - Moo, I'm a cat.
    /)__)
    -"--"-
    ,___,
    [¬.¬] - Dude, you're an owl.
    /)__)
    -"--"-
    ,___,
    [O.O] - My mother has lied to me!
    /)__)
    -"--"-

  8. #8
    Join Date
    Dec 2007
    Location
    Austin, Texas
    Posts
    499
    states are simple.

    public enum state {
    Walk,
    Run,
    Crawl
    };
    private state _curState = state.Crawl;
    // back now in the update function
    switch(_curState) {
    case state.Crawl:
    //Do stuff
    break;
    case state.Walk:
    //Do stuff
    break;
    case state.Run:
    //Do stuff
    break;
    }

    It really is THAT simple... you just plop it into a switch, and do stuff based on the states.

    Also, if you go to my youtube channel (in my signature), and watch my new Advanced AI series (it is using Unity, but the concept here transfers over) and watch episode 1, Finite State Machines. Going through that will help you understand state machines as well...

    Hope I am of help!

  9. #9
    Join Date
    Sep 2008
    Location
    Salisbury, UK
    Posts
    2,812
    Quote Originally Posted by antiroxas View Post
    yeah they do except im still waiting to hear if they charge the credit card or first the amazon gift card value, so i mean, more is better, right?
    Sorry; I completely misunderstood your previous post ...As you were

    Quote Originally Posted by m50 View Post
    you just plop it into a switch, and do stuff based on the states.
    Y'see, that's what I'd do too... but I have a horrible feeling that the mention of using a switch will result in a spanking from Nelson

    I found
    my current avatar on google, so props to THIS GUY who created the original...

  10. #10
    Join Date
    Jun 2008
    Posts
    1,088
    Cool tutorials m50, going to check them out later. I always liked AI programming but still new to it myself. I use the enum method you mentioned above and it seems to work well for my particular project. I am not sure if the scale of the project really matters for that type of switch statement. If you have a very large state tree with 50-100 being ran at a single time, is there a preferred method between using a class system or enum? As far as framerates go.

    -Dane

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •