I am experimenting with these projectiles a little bit, but for some reason, when they hit the corners of my player's hitbox specifically, they sometimes hit twice for double damage. The guy in the video should take exactly 10 hits to kill. The first time I kill him, it takes 9. The second time, it correctly takes 10. Any ideas on how to fix this?
#Projectile hitting twice
1 messages · Page 1 of 1 (latest)
Physics bodies have a function called add_collision_exception_with(body).
Could use that to add an exception to the thing that was just hit, would prevent extra collisions with it.
Or if a projectile is meant to only ever hit one thing (not pierce and hit multiple), could also use a boolean.
Something like has_hit = false and have an if statement to only allow that one hit.
I'm not sure why the physics act that way though.
I've tried the boolean method, and concluded it just works the same in this case as my current code
i queue_free() before the damage function even happens
I will try the exception but it might just work the same
I think queue_free happens at end of frame, so things ran right after it migth happen before it does.
Not sure about hitbox.enabled, but it's physics stuff, so it migth happen on physics tic?
I don't knowfor sure, though.
boolean should happen instantly.
alright I popped the collision exception right after the if statement, still collides twice sometimes
welp, i am now adding an exception, disabling the hitbox, and desroying the object all before the damage function even happens, and it still hits twice sometimes :|
I even tried putting the collision exception before the if statement, still no
Try putting this right before you deal damage await get_tree().process_frame
ah does this wait until the frame is finished? very smart lemmie try it
This won't stop it from getting 2 calls btw. Not sure if that is the current issue
It will just make both calls happen a frame later
yeah the area 2d is entered twice
Are you 100% sure that both times it is entered, it is the same body?
You can use breakpoints to confirm
where would i put breakpoints to confirm?
Oh right, I suppose the wait it would have just deleted both of them before damage happens.
huh, weird.
Maybe some other code is also messing with things.
But if it works, don't fix it :p
i think what's happening at this point is that it enters the area, waits a frame, deals the damage and destroys itself while entering the area a 2nd time, but then it has to wait a frame before dealing damage a 2nd time, and at that point it's already destroyed by the first queue_free
oh wait we have another problem, but this might be unrelated
when many projectiles hit at once, the damage is inconsistent
I fired 9 projectiles, and it only needed 7 to kill, when it should take 10
actually i think it's janky from a distance in general
also i printed the body that it hit, seems to be the same body each time
I dunno, I sometimes make things wait a physics frame because of Godot's timings yeah.
But it doesn't really explain why things are getting hit multiple times in the first place.
I think nodepaths are basically calling get_node repeatedly since you aren't storing them into a variable, but I dunno if that really affects anything here.
i mean I might as well try to change that and see if it works
I've ran into one collision related issue in 4.4, too, but it was in 3D so I doubt that has anything to do with this, either.
that didn't do it
and I tested to see if it was a problem with the fact that multiple projectiles are out at once, but it can infact happen with only 1 projectile on screen
I should have asked earlier, but are your bullets moving in _physics_process?
If you are doing it in _process that can cause issues.
Could try move_and_collide() instead of move_and_slide()?
I don't know though.
what's the difference?
Move and slide is supposed to simulate like... sliding on angled surfaces and stuff I think?
And the collide version just moves and collides with things
alright and what do I need to enter for its argument?
im using delta * velocity for now
and the bug still happens
Yeah it just takes velocity and delta.
I have to admit I really don't know, then. Very odd. I don't know that much about 2D side unfortunately.
unfortunate indeed, i guess i will attempt to call in assistance from #❓┃quick-help again
for any newcomers, this function is being called twice for some reason, even though it runs queue_free() in the first call
That part is normal.
queue_free()
print("lol")```
This on a Node will print "lol" before it's actually deleted.
queue free queues a deletion, at the end of frame.
The double hit I can't explain though.
Come to think of it, there's ``free()`` that instantly frees, but it can lead to errors if anything tries to access it.
I suppose you could try it, too. But ye probably would throw errors due to movement code being on it.
yuh it says the object is locked
queue_free() won't stop it from being called again on the same physics process
yeah so how do i stop it
oo lemmie try that
i'm not 100% sure that's exactly what the function is called
if it's being called twice it's because it collided twice
If the body collides with your body while colliding with the enemies doesn't that add more damage?
yeah but it doesn't make any sense that it collided twice
are you using the new physics engine?
i don't belive that would do anything. I check to see if the shooter isn't the victim, and if they are, it doesn't run damage
uhh i think so? idk how to check
Can I see you damage player function?
That's can be it we must check first
i don't believe anything is wrong with it
and it's notable that the double hit only happens sometimes, seemingly at random
did that change anything?
what is air?
unfortunely no
are you sure it's colliding twice then?
if you mean the function kbPlayer, it's knockback. it won't mess with the health and won't run in the first place because the type is colorless
yes, i printed the body that triggered the collision, and it is, infact, running the entire collision function twice
are there 2 bullets maybe?
maybe, ill try printing something on a bullet creation
yeah
can you do print(self, body) in that function
oh, that is the problem!
it creates a buncha extra bullets sometimes seemingly randomly
10 clicks, 13 bullets
well i don't think anything is wrong with this
and here is the input
well i'm lost again
oh and it seems to just be calling the shoot function more than intended
so i don't think it's a problem with that, but the input handler seems innocent
is it ever a problem with your mouse?
my mouse is fine, almost always
I wonder if you using Input. instead of the event causes that.
Given that you don't have a cooldown for firing bullets, double input would mean double bullets.
And yea could also be that your mouse sometimes doubleclicks.
Though you'd have probably noticed that elsewhere.
actually i do plan on adding a cooldown, that would fix it wouldn't it
you really should understand why it's not working
or the problem might mainfest in a different way later
that is a good point
and i think im figuring it out
it only really happens when im pressing some other input
like walking around
yeah i figured it out...
if you do 2 inputs on the same frame it will go twice
it feels like holding down any input cues for an additional bullet to fire when I press the correct input
you should check if the evenet.is_action_just_pressed(...)
like if i hold down forward, then backward, then shoot, it fires 3 bullets
Yeah I just mentioned that, haha
I remember having issues with it. But dunno if it's still an issue in current versions or not.
actually it's event.is_action_pressed()
uhh wouldn't that run every frame that im holding it down
'cos Input is meant for process or physics process and event is meant for _input.
i mean every time i press any input whilst it's held down
_input() runs whenever you input anything, and Input.is_action_just_pressed() checks if you clicked since last frame, so it will pass the check for any input if at least one is a click
ahh i see
but is there a way to check if a scene is loaded?
so i can fix my menu screen
but if you check it on the event it will only check for the one event that caused the _input() call
yess i see i see
get_tree().current_scene gives you the topmost scene
you can then check against a class_name or group
but only if it's the main scene
yeah that didn't seem to work
you are instantiating it then
it saying that in_action_just_pressed is a nonexistant function in inputeventmousemotion
which i suppose is an input
also i corrected it here
oh yeah this works perfectly
i thought that is would just check every time an input happens that it was held down if i used that function
as in i start holding it, move left and right a few times, it'll fire when i move left and right
but that doesn't seem to happen
no _input() only runs when it starts or ends
ohhhhh gotcha
and you're checking it's the start with is_action_pressed()
as well as checking that it's a click
mm i see
well thank you everyone for your help, i feel kinda stupid for not troubleshooting correctly enough to find out it was my input handler lol!
also i prob shoulda put this in #1235175950241628273 ngl
yeah, ill actually go through each involved process with prints next time
i will eat the cat
the cat is just so edible
did you fix the menu thing?
yeah changing the method to is_action_pressed() fixed it
it was a different scene yeah
so did you fix that then?
oh it should be the same scene? it works fine as different scenes
what did you wanted to check if a scene is loaded for?
because i thought using is_action_just_pressed() was fine, and the reason it broke was that my character didn't exist yet so there was no shoot method to call when i try to click "play"
but it was a problem with using is_action_just_pressed() itself

_ _