isolate identical sprites by variables for testing

Get help using Construct 2

Post » Fri Mar 23, 2012 2:26 am

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.
B
20
S
5
G
3
Posts: 65
Reputation: 3,764

Post » Fri Mar 23, 2012 2:44 am

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?Zetar2012-03-23 02:47:00
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

Post » Fri Mar 23, 2012 2:51 am

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
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

Post » Fri Mar 23, 2012 2:53 am

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.
B
28
S
9
G
6
Posts: 30
Reputation: 6,609

Post » Fri Mar 23, 2012 9:31 pm

@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.

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.
B
20
S
5
G
3
Posts: 65
Reputation: 3,764

Post » Sat Mar 24, 2012 2:04 am

@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 :)
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

Post » Sat Mar 24, 2012 3:26 am

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.
Danijerry2012-03-24 03:33:56
B
28
S
9
G
6
Posts: 30
Reputation: 6,609

Post » Sat Mar 24, 2012 3:53 am

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 :)
B
192
S
6
G
7
Posts: 213
Reputation: 10,178

Post » Sun Mar 25, 2012 4:20 pm

@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!
B
20
S
5
G
3
Posts: 65
Reputation: 3,764

Post » Sat Nov 28, 2015 4:12 pm

How to I find path to a object UID ?
B
5
Posts: 28
Reputation: 305

Next

Return to How do I....?

Who is online

Users browsing this forum: spcsmrf and 26 guests