Track distance between two AI characters?

ProjectorXProjectorX Member Posts: 45
edited March 2013 in Working with GS (Mac)
I've got it down with tracking the distance between my main character and my AI's, because I just keep two game attributes "mainCharX" and "mainCharY" constantly updated.

My AI characters are always spawning so there are always many instances of the same actors in the scene.
I obviously can't keep attributes for every one of the hundreds of AI's on the map at one time, so how would I accomplish, for example:

A situation where the "good guy" AI characters are running away from the "bad guy" characters and those "bad guys" are trying to run toward and attack the good guys? Also, the good guys need to be able to run towards tele porters which are spawned by the main char.

But how do I let the AI know where to run to?

I need the ability to do something like this: When (actor with tag: ______ ) is less than ____ px away, execute behaviors.

Thanks for any input on this.

Answers

  • 3itg3itg Member, PRO Posts: 382
    constrain self.Distance to magnitude( self.Position.X - game.TELEPORTERX , self.Position.Y - game.TELEPORTERY )
    When self.Distance is less than whatever...
    do behaviors here.

    Using self.attributes will only apply to the instance that triggers the condition.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    This is quite difficult to achieve. I'm working on a demo but I don't know if it's really feasible (especially without tons of constraints).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • ProjectorXProjectorX Member Posts: 45
    Thanks, @3itg . That code is helpful but becomes unfortunately inneffectual once I plan teleporters to be spawned in increasing amounts based on the player's ability. I realize I could have ten pre-coded teleporters off-screen and ready to go, but it doesn't seem like an efficient practice to me. I'm already using the code you shared to have "bad guys" chase after the main character. It would be literally impossible, though, to code 200 instances of bad guys chasing after the 200 good guys and 200 instances of good guys running away from the bad guys. That would be 80,000 instances of the same code, plus another 80,000 for the running away and chasing parts. and then there are special characters.....

    If there were just a way to make game salad account for individual actors' specific attributes only once they were present...
  • ProjectorXProjectorX Member Posts: 45
    @tatiang , you're right. Even if you were to manually set up each instance, all of the constraints would run performance into the ground and the game'd be completely unscalable.
  • 3itg3itg Member, PRO Posts: 382
    edited March 2013
    @ProjectorX
    How fast is your gameplay?
    You could drop constraints and have them update a table with coords.
    Basically attempt to make a finite state machine.
    On the AI, make an integer.
    Have it start @ zero.
    When integer = 0
    Do generic behaviors, instead of constrained, move towards nearest waypoint(just a static x&y)
    When magnitude < 300
    Integer +1
    When integer = 1
    Make behavior more refined here, with 2 outs. So use an otherwise to set back to 0 if your conditions aren't met. When conditions are met, like magnitude > 300
    integer +1
    Do even further refined conditions here.
    Chase player... Yada yada

    Can your main character run around the whole screen? Place teleporters any where on screen?
    Is just left to right across the bottom, up and down?
    Because I'm tempted to tell you to get REALLY generic.
    Instead of setting coords for the AI to travel to, you could just set an angle, and have them move in that direction until other conditions are met.

    You could also set up grid system in a table, use booleans to tell if a teleporter is present in that cell location. When a player places a teleporter, have it use the closest cells locations for it.
    Unless your actors are tiny, it shouldnt be very noticeable if the distance is only a small amount pixels.

    I'll crack open some gamesalad here shortly. Everything I've suggested has been off the top of head, but I can't see any reason why one of these solutions wouldnt fix your issue.
    And if there is a maximum number of teleporters (you mentioned 10) code them, place them offscreen. Make rules to move them.
    For what you are describing I imagine the performance will be much better than spawning them.
    For some reason, I read your project description and imagine Braveheart in space... With William Wallace riding around on horse back yelling FREEDOM! every time he drops a teleporter
    @tatiang
    You are good source of info, any thoughts on this suggestion?
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    @3itg lol William Wallace! You know, I couldn't get a demo working but if you can, more power to you.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • ProjectorXProjectorX Member Posts: 45
    @3itg Thanks a lot for your suggestion. I'm currently still trying to get my head wrapped around it. If you could possibly explain it a little more thoroughly then that would be awesome. I'm still not quite sure how to keep down / eliminate the need for game variables on each actor instance.

    FYI: My actors are very small, about 10x10px. They can run around a map filled with obstacles and interactive items (such as houses that bad guys spawn from when close enough) and these maps are mostly at least 1000x1000px It is essentially a multi-directional-shooter where the player can move on both axes and shoot in 360 degrees. (Like Geometry Wars on Xbox). The gameplay is quite fast as it's not a turn-by-turn strategy type game but a shooter. So performance has to be as fast as possible otherwise there will be lag. (Reason global variables and magnitude functions will not work). The actors are tiny but I'm willing to make some sacrifices if I can continue development. This problem has led me to drop development on this game (and gamesalad) completely for over a month and I was pretty excited about it before that! :(

    So I seriously hope I can figure this one out with your guys' help. Thanks!

    PS: I've resolved to simply use a larger and invisible "hit area" actor as an alternative to magnitude. The actor would be stuck to the teleporter and good guy actors so that the good guys run to the teles and the bad guys run to the good guys.

    In summary: all I need to know is how to get the actors to know where to run. They're already going to know when they're close enough. Thanks again!
  • 3itg3itg Member, PRO Posts: 382
    @ProjectorX
    performance definitely takes a hit, and it is a lot to program...
    I was messing with it GS back whenever we were chatting before, and I don't remember exactly how far I got.
    If you want I'll post what I did get accomplished.
  • 3itg3itg Member, PRO Posts: 382
    Yea... I looked at it again, and Im posting what I did do.
    But with that said... I have no idea how to help, sorry.
Sign In or Register to comment.