Need help with simple AI for enemies

FatalCrestFatalCrest Member Posts: 113
edited November -1 in Working with GS (Mac)
Hello all,

So, my problem is how do I make a simple AI? I've already created an updater rule that gets my main characters x and y coordinates its just that how would I get the enemy to follow him? I've tried interpolating, and moving and sometimes they would fly in the air, I need a simple Ai for my enemy that follows my main character on the ground after my character gets near the enemy.

Comments

  • design219design219 Member Posts: 2,273
    I've never done that, but you could try just a timer on the enemy that accelerates (or moves to) toward the main character X and Y every 1 second (or however often).
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    Also, there's an entry in the Support section: "How do I make one actor follow or chase another?" which looks to be quite comprehensive.

    http://gamesalad.com/wiki/how_tos:gsc_follow_another_actor

    :-)

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • ORBZORBZ Member Posts: 1,304
    google finite state machines
  • FatalCrestFatalCrest Member Posts: 113
    I really like design219's idea. Thanks for the wiki gyroscope I needed that point to function thats in there also. Thanks guys! xD
  • design219design219 Member Posts: 2,273
    ORBZ said:
    google finite state machines

    God, I wish I knew what you know ORBZ.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    It's fairly easy to have a bunch of enemies keep track and chase a player...
    I would do it like this:

    First, you need to keep track of the Player's X and Y coordinates in global attributes.
    Make two global game attributes, integer type, called PlayerX and PlayerY.
    Then, in the Player Actor, add two Constrain Attribute behaviors:

    Constrain Attribute: game.PlayerX to: self.Position.X
    Constrain Attribute: game.PlayerY to: self.Position.Y

    Now, everything in the game has access to the Player's current position, and can act on it.

    The next thing I would do is to have a 'range of vision' for the enemies. That way, the enemies only chase the Player is the Player gets close enough to them.

    So:

    In the Enemy Actor, create a new attribute (integer) called something like 'visionRange' (without the quotes). Set it to 100.

    Now we are going to use the magnitude function to determine the distance between the player and the enemy.

    Magnitude is a distance calculation between two points. It comes in very handy!

    Add this Rule to the Enemy:

    Rule
    When all conditions are valid:
    self.visionRange > magnitude(abs(self.Position.X - game.PlayerX),abs(self.Position.Y - game.PlayerY))
    -----Move To: x: PlayerX, y: PlayerY, speed: [whatever]

    Now, whenever the Player is inside the Enemy's range of vision, the Enemy will chase the Player.

    Hope this helps!

    Joe
  • FatalCrestFatalCrest Member Posts: 113
    Wow thats exactly what I need, Thanks firemaplegames! xD
  • FatalCrestFatalCrest Member Posts: 113
    firemaplegames, I just tried out your magnitude thing and I've gotten an empty screen if I drop my enemy actor into the scene, something is wrong. I followed what you had exactly and what I have is exactly the same. Dx
  • firemaplegamesfiremaplegames Member Posts: 3,211
    You have a syntax error somewhere...
    Either a missing/extra parenthesis, or a 'dangling' attribute.

    Make sure you choose ALL attributes from the drop-down menu. Do not just type them in.
  • FatalCrestFatalCrest Member Posts: 113
    I checked firemaplegames the formula is correct and I do pick my attributes from the menu.
    I did notice if I set the relative to settings to actor, the games loads then freezes right after a sec.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    hmmm.

    I don't know what is going wrong. It should work fine.

    If you cut out that Rule and place the enemy in the Scene, does it work?
  • FatalCrestFatalCrest Member Posts: 113
    Yeah it works fine if I remove that rule.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Did you set the visionRange attribute to 100?

    Do you have both of the abs() in the expression as well?

    Please check your parentheses again, GS only freezes when there is a syntax error.
    Make sure that comma is in there as well...

    Copy/Paste what you have in the expression editor in that magnitude field into the forums here, maybe I can see what is going wrong...
  • FatalCrestFatalCrest Member Posts: 113
    Wait commas?
  • FatalCrestFatalCrest Member Posts: 113
    Nevermind firemaplegames I got it to work, I had a extra period near my parenthesis.
    Hard to notice those things. Thanks! xD
  • ORBZORBZ Member Posts: 1,304
    @ firemaplegames

    the abs() functions in the magnitude function are not needed since magnitude is always an absolute value.

    @design219

    years of coding :P I started when I was 15.. i'm 33 now. (If only I could draw!!!)

    finite state machines are just a fancy way of saying that you can change the behavior of an actor based on the conditions of its inputs.

    so say you have an actor that switches "modes" between chase and run

    each of those two modes is a state, and since there is a finite number of them it's called a "finite state machine"

    it allows for very simple creation of semi-dumb AI which changes its behavior based on some in-game critiera... like pack man picking up the power pellet changes the ghosts from "chase" to "run" mode...

    you could have any states you can imagin. Chase, run, sleep, eat, fly, explode, invisible, etc..

    you just create a self variable called "state" (or whatever you want) then create a rule for each state

    Rule: state = "chase"

    (firemaple's code here to do the chase stuff)

    Rule: state = "run"

    (invert firemaple's code and make the value negative to make it run away from the player)

    Rule: state "sleep"

    (do nothing)

    ... etc...

    that's a very basic finite state machine.

    As you can see, this creates the illusion of the AI having a sort of "brain" as it changes its behavior based on the game world's current condition.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Glad you got it to work!

    @ORBZ: does the magnitude function force a positive? If so, that's great. I didn't know that. I put the abs() in there just in case.
  • FatalCrestFatalCrest Member Posts: 113
    Off topic, ORBZ try sketching, it took me 2 weeks to learn how to draw anime, shapes, landscapes(eh I'm noob at it), and simple junk. Just spend like maybe 30min a day it should work. :D
  • ORBZORBZ Member Posts: 1,304
    @ firemaplegames

    yes magnitude always returns a positive number, or 0. vectorToAngle though can return any real number.

    @ FatalCrest

    Interesting suggestion... I'd like to learn how to draw simple gfx though in the computer. The problem is, how can I translate paper art to computer art? I have a drawing tablet but what's in my head just does not show up on paper :( I wish I knew a talented digital artist...
  • IntelligentDesignerIntelligentDesigner Member Posts: 517
    @ORBZ, there is a guy here named FireMapleGames who is a phenomenal digital artist...

    Oh, wait you knew that!
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    and Ian at debudesign
Sign In or Register to comment.