How do I Count the number of an instance

Get help using Construct 2

Post » Fri Apr 03, 2015 11:07 pm

Hello all,

Sorry if this has been explained elsewhere but I can't find it. I'm not a programmer, and have only had the program a week so I'm stumped. I have an enemy sprite called "SnailEnemy" and a goal where I want them to get to.

How do I:

1) Start of Layout, count the number of "SnailEnemy" to create "SnailEnemyCount" (Instance Variable on Goal)
2) Subtract 1 from "SnailEnemyCount" whenever it collides with "Goal"
3) If "SnailEnemyCount = 0", restart Layout

I could set the SnailEnemyCount manually to the number each layout starts with, but I want the system to handle it instead. When I run the game, on the first restart I get the message:

"The Instance being inspected ('Goal' UID 18) was destroyed"

I don't have any actions that should destroy the Goal so I don't know why it's breaking. It remains visible even while the system continually tries to restart. Thanks for any and all responses.
B
5
Posts: 9
Reputation: 243

Post » Sat Apr 04, 2015 12:44 am

I would make a global variable called "snailEnemyCount" by right clicking in the event editor and choosing add global variable
then make an event "on system start." set global/local choose "snailEnemyCount" and then type snailEnemy.Count
then as new event snailEnemy - is overlaping and then choose goal.
system set global/local snailEnemyCount = snailEnemyCount-1

finally an event for snailEnemyCount = 0 restart layout

hope I explained it well and hope it helps. Also I have no idea about your bug.
B
6
S
1
G
1
Posts: 31
Reputation: 945

Post » Sat Apr 04, 2015 2:19 am

Thanks for the assistance :D. It works perfect if it's a global variable, but the bug occurs if SnailEnemyCount is a local variable. I will keep it as Global but wish I understood the error :P
B
5
Posts: 9
Reputation: 243

Post » Sat Apr 04, 2015 3:26 am

You should be able to get what you want with NO variables at all just have a System compare to values SnailEneamy.count = 0 it's that simple
B
42
S
17
G
2
Posts: 850
Reputation: 6,209

Post » Sat Apr 04, 2015 12:10 pm

That would only work for what he wants if he plans to destroy the snailEnemy the second it touches the goal. I imagined he wanted the snailEnemy to continue existing maybe do a victory dance or something while the global count of how many are in the race can still tick downwards.
B
6
S
1
G
1
Posts: 31
Reputation: 945

Post » Sat Apr 04, 2015 2:17 pm

There will be other conditions that can destroy the snails without it leading to victory so that suggestion won't work. Thanks for the suggestion though :)

The bug has returned even while using Global Variables. It seems to be fairly random.
B
5
Posts: 9
Reputation: 243


Return to How do I....?

Who is online

Users browsing this forum: Gokee and 2 guests