How do I pick an single object from a family

Get help using Construct 2

Post » Sat Sep 06, 2014 11:47 pm

So I have 3 objects grouped in a Family. Each object has a variable (Distance) that holds a number.
On mouse click I need to loop through the family inspecting each instance variable and pick the object with the lowest value. In C# this is very simple to do, but I cant wrap my head around it with this no code approach.

As of right now all Cannons are firing... I need only the closest one to fire.

"Code" so far:
Code1.PNG


Project so far:
MissileDefencePartialProgress2.capx
You do not have the required permissions to view the files attached to this post.
B
8
S
3
Posts: 16
Reputation: 702

Post » Sun Sep 07, 2014 12:12 am

I got it "done"... So ugly and ridged! :o

Code2.PNG
You do not have the required permissions to view the files attached to this post.
B
8
S
3
Posts: 16
Reputation: 702

Post » Sun Sep 07, 2014 12:42 am

One method you could try is giving the family the LineOfSight object behaviour, and then set the range to 500 or so. LineOfSight automatically checks range and will apply it to each cannon, so if your cannons were 100 pixels apart, each cannon farther away wouldn't see a target because they're 500 plus the 100 pixels to the next cannon away. You can then replace all those cannon distance checks with a check for Cannons -> Has LOS to object and set the object to be checked to be a family of enemies. You can then spawn Bullet1 off Cannons and it should automatically spawn it at the cannons which actually have range on the target.
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008

Post » Sun Sep 07, 2014 1:16 am

Message: fscopel can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
@inquiesco Hmm I see your point, and I think it could partially work. But the issue I have with this solution is that more than 1 cannon may resolve "Has LOS" to true. In that case 2 or more cannons would fire. Plus I also need to add additional logic to check if the cannons still have bullets left, so the next closest one would take charge and fire instead. Does that make sense?

Check out game mechanics so far.... :)

http://ec2-54-187-14-211.us-west-2.compute.amazonaws.com/missileDefense/
B
8
S
3
Posts: 16
Reputation: 702

Post » Sun Sep 07, 2014 2:10 am

@fscopel To be honest, since you're using exact measurements for checking distance rather than rounding them up, the chances of you getting equal distance between two cannons is very, very low. I tried in your example to get somewhere in the middle of 290.181271743284 (or something equally hard, ha ha) and I couldn't even equalise the .18 part because of mouse sensitivity. If you were rounding up the numbers to whole numbers then yeah, it'd be more of a problem and require solving.

As for bullets per cannon, you can give each cannon an instance variable of bullet_count, then give them 500 or however many you're wanting. The logic of the engine when objects are in a family is that they interpret the objects in them individually, so only objects meeting the required conditions you've put to the family are triggered. Based on that you should be able to then just set fam_cannons -> Subtract 1 from bullet_count. It's a bit different moving from programming languages where we need to explicitly check each object to see if it meets conditions, which is a good thing, I think, :). Though I still get caught out sometimes wanting to write JavaScript to pick out specific objects and have to hunt down the equivalent method of thought, lol.

As for your game mechanics, looks fun, I like those games as I'm heavily nostalgic, :). Hence why I'm making a space shooter, and possibly an RPG after, ha ha. Keep up the good work!
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008

Post » Sun Sep 07, 2014 12:02 pm

You can use 'For each (ordered)':

+System: For each Cannons order by Cannons.distance ascending
-> Cannons: Spawn bullet
-> System: Stop loop
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Sun Sep 07, 2014 9:43 pm

@ramones I see what you did there. The only issue is that I keep the distance in each individual cannon not at the Family level. I am not sure how to use the Family level variables. Could you extend on that really quick?

Thank you
B
8
S
3
Posts: 16
Reputation: 702

Post » Sun Sep 07, 2014 11:18 pm

Message: fscopel can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
@ramones Never mind - I got! Thank you for the info, very helpful.

https://www.scirra.com/manual/142/families
B
8
S
3
Posts: 16
Reputation: 702


Return to How do I....?

Who is online

Users browsing this forum: JDebstup, Leef and 21 guests