isolate identical sprites by variables for testing

0 favourites
  • 14 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Here's a problem that I'm having trouble even beginning to implement.

    I use 1 sprite for all the enemy ships in my game. That 1 sprite has a variable called "faction" which can be 1-3. Depending on which faction it belongs to it'll change it's color and weapons, etc. If two enemy ships of different factions get close to each other I want them to target each other (in my case that requires a variable "target" to be set to the other ship's UID).

    I'm having trouble testing whether two objects, that are the same sprite- but are from different factions, are within a certain distance of each other. And if they are, changing each of their "target" variable to the other one's UID. This would be fairly simple if the two ships were completely different sprites, but there must be a fairly easy way to do it as I have it set up now.

    It kind of hurts my brain to think of how to make this work and I hope I've explained myself properly.

    Basically I'm trying to find a way to isolate 2 identical sprites that have different "faction" variables, compare their distance and if close enough set another variable, "target" to be the other sprites UID. Keep in mind that there may be as many as 50 of these sprites (of various factions) in the playfield at any time.

    Thanks.

  • Are you willing to share your CAPX, or at least a simplified one?

    So If I understand,

    You are doing something like:

    ShipSprite Pick nearest to Self.x, Self.y

    SubEvent-> ShipSprite Team Variable(NOT EQUAL)to Self.Team

              ->System Compare Distance (Self.x,Self.y,ShipSprite.x, ShipSprite.y)<300

                   ->Set variable Self.AttackUID = ShipSprite.UID

    Something like that?

  • However now that I read your post again, the Self part is actually another AI ship, right?

    So In that cars you would add a Event above all others,

    For Each ShipSprite

    ShipSprite Pick nearest to Self.x, Self.y

       ShipSprite Team Variable(NOT EQUAL)to Self.Team

       System Compare Distance (Self.x,Self.y,ShipSprite.x, ShipSprite.y)<300

         ->Set variable Self.AttackUID = ShipSprite.UID

  • You could do it in a couple a ways:

    1. you could do a nested loop,

    Pick one ship and compare if it is within the distance of another ship of different faction.

    2. You could use an invisible object,

    I prefer this, use a sprite for example a circle of a certain radius.

    You could set to invisible

    Pick a ship and set the position of this sprite to the position of that ship.

    Check if other ships overlap with the sprite used

    For each of those ships check for the faction and the distance

    If the distance is lesser store the UID.

    I am also wondering if Pick nearest System event would work for you.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Zetar

    Your method makes a lot of sense to me but I think there's a problem.

    <font size="2">System Compare Distance (Self.x,Self.y,ShipSprite.x, ShipSprite.y)<300</font>

    That line gives me an error saying you cannot refer to "self" in a system expression.

    The other problem is in line:

    <font size="2"> ->Set variable Self.AttackUID = ShipSprite.UID</font>

    I don't know how to set Self.Variable. I only know how to set a variable value for a specifically named sprite.

    I put together a capx that has a simple setup similar to what I have in my game. There is one ship sprite that can be 1 of 3 factions. When its in mode 0 it moves around randomly and when it's in mode 1 is moves towards the ship who's UID is stored in it's target variable. The ship is also in a family group called "enemy_ship". I'm using that family as a way to make the ship sprite differentiate itself from it's target. If you look at the code, hopefully you'll understand what I mean.

    http://dl.dropbox.com/u/28484936/target_ship.capx

    Danijerry Your method using another sprite sounds interesting as well, but my first attempt at it failed. If you have any other suggestion, or could take a look at my capx I'd appreciate it.

  • @Zetar Your method makes a lot of sense to me but I think there's a problem. System Compare Distance (Self.x,Self.y,ShipSprite.x, ShipSprite.y)<300 That line gives me an error saying you cannot refer to "self" in a system expression. The other problem is in line: ->Set variable Self.AttackUID = ShipSprite.UID I don't know how to set Self.Variable. I only know how to set a variable value for a specifically named sprite.

    You are right. I did not have it up and running at the time. It hates Self in expressions... I will look at your CAPX and see if I can figure anything out if someone else doesn't come through for you :)

  • It was a little involved. I used a few local variables.

    It is an issue of updating the "SOL" each time. "Pick all" would do this. You can try disabling it and disabling or enabling the debug text that I have inserted, you should be able get a fair idea of how this works.

    May be the same could be applied to the other methods if you like them.

    Try this. http://dl.dropbox.com/u/28195143/c2_share/TargetShip_InvisibleSprite.capx

    Things to note: Sprite3 is very small, I am blowing it up to 300x300 so the edges would be fuzzy, you may want to use a better one to get a better overlap.

    Local variable "closestTargetDist" should be greater than the invisbile sprite radius to begin with.

  • I would go with Danijerry's idea. I am running into difficulty wity the distance idea. It would only be a tiny bit less complicated...   Someone needs to make a plugin for this :)

  • Danijerry

    You are my hero. This worked like a charm. And I understand your method completely. I've implemented it in my game and it's working great. Thank you so much.

    Zetar

    Thank you for your help too. I love this forum!

  • How to I find path to a object UID ?

  • How to I find path to a object UID ?

    Why would you bump a 3 year old topic?

    I guess you are looking for something like:

    Pick object by UID

    • find path to object.x,object.y

    But It would probably be much better to create a new topic..

  • Yes I want to avoid having to create 12 new objects with new names.I can see that i can "Find Path" to object name example EnemyPath.X + EnemyPath.Y

    EnemyPath is a object

    I wanted to go something like this

    EnemyPath.UID,4

    So it find UID instance 4 of that object

    ??

  • system pick by uid enemypath with uid = 4

    If there is an enemypath-object with uid = 4, that event should pick it..

  • This didnt work but the syntax worked without warning

    EnemyPath.UID = 80

    EnemyPath.UID =80

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)