How do I trigger individual spawner while player is in range

Get help using Construct 2

Post » Sat Aug 05, 2017 5:15 pm

Hi guys,

I've got a problem with spawning enemies while multiple spawners are in player's range.

I want to spawn one enemy whenever player gets near the spawner (in my example - less than 100px).
Everything works fine when there is only one spawner within the player's 100px range (one enemy is properly spawned), but whenever more than one spawner is in player's range the enemies are spawning endlessly from all spawners except the first one.

I've tried different approaches for measuring distance like:

- System - distance(player.X, player.Y, spawner.X, spawner.Y) less than 100
- above method, but with changing spawner's local variable "lod" to "1" when player is in range
- using "line of sight" behaviour on spawners
but all these methods produce the issue I mentioned earlier.

I've tried also different ways to spawn enemies but without success
- System - create object
- spawner - spawn

my events with all 3 methods of spawning
Image

and illustration of the issue
Image

It seems I'm missing something so any help would be much appreciated
B
10
S
4
Posts: 9
Reputation: 1,062

Post » Sat Aug 05, 2017 6:18 pm

@MrSheet


Maybe something like this??

Capx https://www.dropbox.com/s/xxkdfjvob95zsmg/Spawners.capx?dl=0
B
41
S
25
G
87
Posts: 309
Reputation: 48,096

Post » Sat Aug 05, 2017 6:29 pm

Here are a couple of ways you can do it:

https://www.dropbox.com/s/sp0pa7sbdmv1s ... .capx?dl=0

In method 1 each spawner is constantly checking the distance to the avatar and updating its distance instance variable - if the avatar comes within range it spawns an enemy.

In method 2 the avatar has a range sprite with a radius of 100, and the spawner spawns an enemy on collision.

In both methods I've restricted the spawner to only spawning once by checking their "enemies" instance variable. You can increase this to spawn more enemies per spawner, or eliminate the the condition "spawner.enemies>0" to spawn an enemy every time the avatar comes withing range.
B
22
S
9
G
4
Posts: 476
Reputation: 4,327

Post » Sun Aug 06, 2017 12:05 am

Thanks both of you :)

tarek's method did the trick wonderfully :)

here's an image if anyone else is intersted

Image
B
10
S
4
Posts: 9
Reputation: 1,062

Post » Sun Aug 06, 2017 8:35 am

Personal, i am not that happy with that 'for each'.
Just have to be aware of the different 'timings'. The animation changes when in/not LOS, the bullet fire happens once on approach. As you see, those are totally different 'timings'.

https://www.dropbox.com/s/cjtru92sxgtzz ... .capx?dl=0
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sun Aug 06, 2017 12:17 pm

The animations you don't really need them it was more to give more juice to the example

Anyway, The spawner it should be animation Speed 0 my mistake I did it quickly and I forgot to change that I didn't think you will keep the animations Frames lol

Corrected Capx :

https://www.dropbox.com/s/jhehtdv82ofmcin/Spawners2.capx?dl=0

Image
B
41
S
25
G
87
Posts: 309
Reputation: 48,096

Post » Sun Aug 06, 2017 1:30 pm

@99Instances2Go

your capx is not really working like it should without the Each, at last here on my pc?? I didn't get what you mean about the (For each) problem
B
41
S
25
G
87
Posts: 309
Reputation: 48,096

Post » Sun Aug 06, 2017 2:57 pm

What did i miss ? (not the first and not the last time that i did something wrong)
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sun Aug 06, 2017 3:32 pm

99Instances2Go wrote:What did i miss ? (not the first and not the last time that i did something wrong)


lol :D

I meant for this >>(Personal, i am not that happy with that 'for each'.)

If I understood properly you said that you have a problem with the "for each" right? so because I'm not a Pro I'm just a beginner and I may have done something wrong so I would like to correct it if possible, every day we learn better ways to do things in it, that is the fun part.
so I look up in your capx to learn the correct way of doing it without the "for each", but I see like the spawners don't spawn individually when the player is on the range. maybe I'm wrong but shouldn't each spawner spawn once when the player is in range, basically what the op was asking if I'm not mistaking please correct me if I'm wrong
B
41
S
25
G
87
Posts: 309
Reputation: 48,096

Post » Sun Aug 06, 2017 4:18 pm

Ahhhhh, you are right, that capx was close to rubbish. Allow me to make a correction.
https://www.dropbox.com/s/4mn2e16dwmlzw ... .capx?dl=0

About THAT 'for each'. That is a loop, running trough all (picked) instances.
Personal i do not like a 'for each' as 'first' condition, because it runs trough all instances.
With a lot of instances, that is gonna hurt performance.
Worse, I have seen a lot of projects with several 'for each' loops running trough the same instances.

I try to avoid that.

I have no problem with a 'for each' after pre-picking conditions. Like this:

Player has LOS to Spawners
For each Spawners <---- runs only trough those that are in range (picked)

You see the difference ?

Not that i am that bright to always know how to avoid such a root 'for each'.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Next

Return to How do I....?

Who is online

Users browsing this forum: max2612 and 26 guests