#Projectile hitting twice

1 messages · Page 1 of 1 (latest)

grand laurel
#

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?

cinder hamlet
#

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.

grand laurel
#

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

cinder hamlet
#

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.

grand laurel
#

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

cinder hamlet
grand laurel
#

ah does this wait until the frame is finished? very smart lemmie try it

supple needle
#

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

grand laurel
#

yeah the area 2d is entered twice

supple needle
#

Are you 100% sure that both times it is entered, it is the same body?

#

You can use breakpoints to confirm

grand laurel
#

where would i put breakpoints to confirm?

supple needle
#

Anywhere within the entered function.

#

The idea is to see what node is body

cinder hamlet
#

Oh right, I suppose the wait it would have just deleted both of them before damage happens.

grand laurel
#

actually the wait seems to have worked

#

i just moved the queue free after it

supple needle
#

huh, weird.
Maybe some other code is also messing with things.

#

But if it works, don't fix it :p

grand laurel
#

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

cinder hamlet
#

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.

grand laurel
#

i mean I might as well try to change that and see if it works

cinder hamlet
#

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.

grand laurel
#

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

cinder hamlet
#

I should have asked earlier, but are your bullets moving in _physics_process?
If you are doing it in _process that can cause issues.

grand laurel
#

yeah it's in physics process

cinder hamlet
#

Could try move_and_collide() instead of move_and_slide()?
I don't know though.

grand laurel
#

what's the difference?

cinder hamlet
#

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

grand laurel
#

alright and what do I need to enter for its argument?

#

im using delta * velocity for now

#

and the bug still happens

cinder hamlet
#

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.

grand laurel
#

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

cinder hamlet
#

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.
grand laurel
#

yuh it says the object is locked

cold mulch
grand laurel
#

yeah so how do i stop it

cold mulch
#

you can check if it is_queued_for_deletion()

#

and then return out of the function

grand laurel
#

oo lemmie try that

cold mulch
#

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

clever reef
grand laurel
#

yeah but it doesn't make any sense that it collided twice

cold mulch
#

are you using the new physics engine?

grand laurel
grand laurel
cold mulch
#

i think you have to enable it so probably not

#

yeah idk why it's colliding twice

clever reef
grand laurel
#

in this case the type is colorless

clever reef
#

That's can be it we must check first

grand laurel
#

i don't believe anything is wrong with it

#

and it's notable that the double hit only happens sometimes, seemingly at random

cold mulch
clever reef
#

what is air?

grand laurel
cold mulch
#

are you sure it's colliding twice then?

grand laurel
# clever reef what is air?

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

grand laurel
cold mulch
#

are there 2 bullets maybe?

grand laurel
#

maybe, ill try printing something on a bullet creation

clever reef
#

yeah

cold mulch
#

can you do print(self, body) in that function

grand laurel
#

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

cold mulch
#

is it ever a problem with your mouse?

grand laurel
#

my mouse is fine, almost always

cinder hamlet
#

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.

grand laurel
#

actually i do plan on adding a cooldown, that would fix it wouldn't it

cold mulch
#

you really should understand why it's not working

#

or the problem might mainfest in a different way later

grand laurel
#

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

cold mulch
#

._.

#

yeah

grand laurel
#

yeah i figured it out...

cold mulch
#

if you do 2 inputs on the same frame it will go twice

grand laurel
#

it feels like holding down any input cues for an additional bullet to fire when I press the correct input

cold mulch
#

you should check if the evenet.is_action_just_pressed(...)

grand laurel
#

like if i hold down forward, then backward, then shoot, it fires 3 bullets

cinder hamlet
#

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.

grand laurel
#

well that broke my title screen

#

is there a way to check if a scene is loaded?

cold mulch
#

actually it's event.is_action_pressed()

grand laurel
#

uhh wouldn't that run every frame that im holding it down

cinder hamlet
#

'cos Input is meant for process or physics process and event is meant for _input.

grand laurel
#

i mean every time i press any input whilst it's held down

cold mulch
#

_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

grand laurel
#

ahh i see

grand laurel
#

so i can fix my menu screen

cold mulch
#

but if you check it on the event it will only check for the one event that caused the _input() call

grand laurel
#

yess i see i see

cold mulch
#

you can then check against a class_name or group

#

but only if it's the main scene

grand laurel
#

yeah that didn't seem to work

cold mulch
#

you are instantiating it then

grand laurel
#

it saying that in_action_just_pressed is a nonexistant function in inputeventmousemotion

#

which i suppose is an input

cold mulch
#

it's is_action_pressed()

#

i missremembered

cold mulch
grand laurel
#

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

cold mulch
#

no _input() only runs when it starts or ends

grand laurel
#

ohhhhh gotcha

cold mulch
#

and you're checking it's the start with is_action_pressed()

#

as well as checking that it's a click

grand laurel
#

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

cold mulch
#

yeah ._.

#

it's a skill like any other

#

you get better by being bad at coding

grand laurel
#

yeah, ill actually go through each involved process with prints next time

cold mulch
#

i will eat the cat

grand laurel
#

😿 is crazy LOL

cold mulch
#

the cat is just so edible

cold mulch
grand laurel
#

yeah changing the method to is_action_pressed() fixed it

cold mulch
#

i thought you wanted to check when the menu was loaded

#

or was it a different scene?

grand laurel
#

it was a different scene yeah

cold mulch
#

so did you fix that then?

grand laurel
#

oh it should be the same scene? it works fine as different scenes

cold mulch
#

what did you wanted to check if a scene is loaded for?

grand laurel
#

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

cold mulch
#

gdthumbsup_ _

#

but did you read the error message?

grand laurel
#

yeah that's how i figured it out, plus your reminder

#

i failed to read the error message before consulting discord, another flaw with my debugging

cold mulch
#

some error messages aren't too easy to understand so you can end up getting used to skipping that step

#

ok, we're done here, have a cookie: 🍪