Help with nearest obj

Discussion and feedback on Construct 2

Post » Thu Aug 17, 2017 2:30 am

B
7
S
2
Posts: 11
Reputation: 497

Post » Thu Aug 17, 2017 3:35 am

dwtiger wrote:https://www.dropbox.com/s/itmt0kmsks26jau/RPG.capx?dl=0



YAOOOOO man that was so many events lol I got lost, normally you clean and share the minimum events enough events just to reproduce the problem with destroying enemy

Anyway there is so many things there that need serious optimization and I haven't got time to check it I should be sleeping by now

first, let's talk about the problem>> destroying enemy

on event 13 you have

Sprite10 is overlapping Hitbox
Action> Sprite10 destroy
Action> ad 1 to Enemy health


1-You see the problem here you don't reference to which Enemy belongs the Hitbox so because of that it picks all the enemy's and add 1 to each enemy health, you see that's why you kill all in one go

2-another thing that you need to change is instead of the overlapping use on collision is better why you don't use sprite10 on collision with enemy >> Action sprite 10 destroy, add 1 to enemy health that's it more simple


but if you gonna use overlapping you should use trigger once so it runs only once not every thick

you have the overlapping condition every where without the trigger once that it will cause you a lot of problems, normally the things that they need to run just once add the trigger once but if need to be constantly true then don't add the trigger once like event 73 and 74

3- when you wanna check for the health to destroy enemy's is
enemy health is bla bla
>>For each enemy: destroy <<<<<<<<<you see is for each enemy not >>for each sprite >> because here you
have referenced the player sprite not the enemy


That's a few things that a quick saw I didn't check everything but you should really check everything and optimized
B
48
S
30
G
92
Posts: 363
Reputation: 51,384

Post » Thu Aug 17, 2017 4:02 am

Thanks so much! You should sleep, but if you can I'd really appreciate if you could help me with the hitbox and damaging situation, I'm decently new to construct 2 anyways, either way, thanks a ton.

How would I assign a hitbox to an enemy without having to write infinite lines? so that I can have multiple enemys and they all have their own hitboxes
B
7
S
2
Posts: 11
Reputation: 497

Post » Thu Aug 17, 2017 5:36 am

@dwtiger

No problem man, glad to help

here is a capx that you can open with the c2 version r244

https://www.dropbox.com/s/otapg6806888ltg/Health%20bar%20version%20r244.capx?dl=0

now this capx is for the health bar only but it should be exactly the same process to create the hit box for the enemies.

Just in case that you cannot open the capx, the basic idea will be:


first, put the hit box in a family and lets named Hitbox and to that family add an instance Variable lets named >>BelongsTo >> plus add swell the Pin behavior to that family

second, put your enemy in a family and named example enemies and add an instance Variable lets named MyUID and another variable >> Health

Note: the tree instances Variables (BelongsTo, MyUID and Health) are variables type >> Number

Now let's do the events side: one more Note before we start >>when I say sub event means right click in the top event that we currently working on, and select >>Add>> then >>Add sub-event

let's start, the first Action will be created the hitboxes at the start of the game and Link them to their enemies

1- On the start of the Layout: >>Action :>> chose the family Hitbox >> then destroy
now sub event of this event>> For Each >> choose the family enemy >> now the Actions> goes in this line not in the top event
>>>Action: chose family enemy >> set variable >>MyUID to >> self.UID
>>>Action: system>>create "hitbox family" >>on layer (you choose)>> at> enemy.X, enemy.Y
>>>Another Action>> "hitbox family">> set Variable "BelongsTo" >> to >>enemies.UID
>>>Another Action>> "hitbox family">>Pin to enemies


Now with this, we finished the initial preparation, so how do we refer the enemies and their hitboxes?

2- Create New event: sprite10 >> On collision with >> "Hitbox family" >>Action: destroy sprite10
Sub Event of this event: >>enemies >> compare var >> MyUID >>Equal to >>hitbox family var: BelongsTo
Action: in the same line of the sub : enemies add or substract (Amount ) >>from enemies >> Health



3-Create New Event: >>Enemies family >> Health >>is equal or greater than 10
Sub Event of this event >> For Each >>enemie family>>Action Destroy
Sub event of this Sub event >> hitbox family>> var BelongsTo>> is equal to>>
Enemies Family>>var MyUID: Action >> Hitbox familie >> destroy


That's it Let me know if you have any problems, Make sure you put All the Actions in their correspondent line if is top event actions you put them at the top if is a sub event actions put them in the sub event if is for a sub sub event then the same put them there in the sub sub event other ways you will break the code
B
48
S
30
G
92
Posts: 363
Reputation: 51,384

Post » Thu Aug 17, 2017 5:59 pm

ok I've tried working this out but it seems that I might have messed up somewhere. Here's the capx, and thanks again!
https://www.dropbox.com/s/itmt0kmsks26jau/RPG.capx?dl=0
B
7
S
2
Posts: 11
Reputation: 497

Post » Fri Aug 18, 2017 2:17 am

@dwtiger


First please go to the debug option from c2 button and check at the left panel where it says "Hitboxes" & "Hitbox" watch the numbers now go near to any enemy and stay there and look again what happens with the numbers, you creating hitboxes none stop

75 and 76 are the ones causing the problem, you see I warned you in my first post if you are not careful with overlapping things like this will happen, normally creating 1 object the way you do it using overlaps you always add the >> Trigger once> you can find it in the system.
But there is another problem with this you creating already all the hitboxes on the Start of the layout Event 81 remember? so you really don't need event 75 & 76 that will create extra hit boxes for the enemies and it brake the code, an less you don't wanna create the hitboxes at the start of the layout and you wanna create them only when the player overlaps the enemy, in that case, you have to move the last three actions from event 81 to wherever place you create the
hitbox example event 75 and adding the >> Trigger once>


Event 81 >>replace the system create object by >> Enemies >>Spawn object>> Hitboxes and leave it in the same place order were it was the old one, I'm not sure why but it was creating the two hit boxes in the same place, but the spawn option it should fix that

Event 84 >> you have Action destroying an Enemy but you should be destroying the Hitboxes

Event 17 and 18 you don't need it because you already destroy the Enemies on Event 82

Tips:
-The Pin behavior if enemy doesn't move you don't need it

-Remember that you have the behavior >>Line Of Sight and you can use it instead of checking for overlaps for everything

-you have the bullet behavior for the Bullets swell

-you have the groups to organize the code into groups and name them will help you to find bugs quicker


probably I forgot something if I remember anything I will post it
B
48
S
30
G
92
Posts: 363
Reputation: 51,384

Post » Fri Aug 18, 2017 5:28 am

Thanks again! I've fixed the issue with the multiple hitboxes for 1 enemy, I accidentally put another object in the enemy family. But now that the hitboxes are spawned in at the beginning of the layout, my bullets keep going to the wrong hitbox, is there any way to solve this? (also the enemies are still being killed all in 1)
CapX: https://www.dropbox.com/s/itmt0kmsks26jau/RPG.capx?dl=0
B
7
S
2
Posts: 11
Reputation: 497

Post » Fri Aug 18, 2017 3:46 pm

@dwtiger

Again the same Mistakes Event 13 >> the action to add 1 to the enemy DamageTaken you add it to all of the enemies because you don't specify to which enemy belongs that hit boxes That's why you kill all the enemies in one go.
And Event 14 have no actions in there so whats the point to put a condition without any actions on it??
it should be like this
Picture:
Image


And You did the same thing on Event 84 & 85


Event 87 Again take damage to the enemy but which enemy all of them?? and event 88 empty why you keep leaving empty events with no actions???


Bullets Problem:

Event 11 you overlapping the enemy so it adds that enemy to the pick list but on the action you creating the bullet and then you move the bullet to a HitBox but the hit box is not in the pick list you don't specify to which hitbox belongs to that enemy, therefore, is an Un-Referred Object so C2 it will pick up the first hitbox created wich is Hitbox = 0
So first replace the sprite is overlapping to >> "The Family Enemy"
Then sub event of this event>> "Family hitbox" >>Compare>> belongsTo = To >>Enemies.UID> > Action then you can move the bullet to that angle hitbox
Here is A picture:
Image


Then again you will have the same Problem with the other bullet >> Sprite36 if you don't change it, In Event 83 so do the same as above
B
48
S
30
G
92
Posts: 363
Reputation: 51,384

Post » Fri Aug 18, 2017 6:15 pm

Thanks! I've fixed all of those except one, I can now kill enemies one by one, but my bullets keep going to the first hitbox, how do i specify which hitbox i want my bullets to go to, cuz i want them to go to the nearest enemy hitbox.
B
7
S
2
Posts: 11
Reputation: 497

Post » Fri Aug 18, 2017 6:21 pm

dwtiger wrote:Thanks! I've fixed all of those except one, I can now kill enemies one by one, but my bullets keep going to the first hitbox, how do i specify which hitbox i want my bullets to go to, cuz i want them to go to the nearest enemy hitbox.



????

Did you try the example that I left in my pictures ???
Because is working for me
B
48
S
30
G
92
Posts: 363
Reputation: 51,384

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: R0J0hound and 20 guests