Phazorknight / Cogito

Immersive Sim Template Project for GODOT 4
MIT License
902 stars 101 forks source link

Wieldable Get_Camera_Collision function causing framerate drops #86

Closed cmr624 closed 7 months ago

cmr624 commented 7 months ago

Am wondering what the code in Get_Camera_Collision in PlayterInteractionComponent does, and why it's called on every wieldable primary action fire? when overriding the function in my class, it would be great to optionally decide not to call this function if it's not serving the function of the custom wieldable?

COGITO/Components/PlayerInteractionComponent.gd#L242

func Get_Camera_Collision() -> Vector3:
    var viewport = get_viewport().get_size()
    var camera = get_viewport().get_camera_3d()

    var Ray_Origin = camera.project_ray_origin(viewport/2)
    var Ray_End = Ray_Origin + camera.project_ray_normal(viewport/2)*equipped_wieldable_item.wieldable_range

    var New_Intersection = PhysicsRayQueryParameters3D.create(Ray_Origin,Ray_End)
    var Intersection = get_world_3d().direct_space_state.intersect_ray(New_Intersection)

    if not Intersection.is_empty():
        var Col_Point = Intersection.position
        return Col_Point
    else:
        return Ray_End
func attempt_action_primary():
    if equipped_wieldable_node == null:
        print("Nothing equipped, but is_wielding was true. This shouldn't happen!")
        return
    if equipped_wieldable_item.charge_current == 0:
        send_hint(null, equipped_wieldable_item.name + " is out of ammo.")
    else:
        if equipped_wieldable_node.animation_player != null:
            if !equipped_wieldable_node.animation_player.is_playing(): # Enforces fire rate.
                equipped_wieldable_item.subtract(1)

                # why is this called in primary_action?
        equipped_wieldable_node.action_primary(Get_Camera_Collision(), equipped_wieldable_item)

thank you!

cmr624 commented 7 months ago

Ah, I didn't describe the issue I mentioned in the title, which is that repeatedly firing a wieldable causes framerate dips

Phazorknight commented 7 months ago

Get_Camera_Collusion() gets a world space collision point of whatever the camera is pointed at, depending on the equipped wieldable range.

You are correct that technically it shouldn't be called for every wieldable per default, so I'll look into moving this out of the attempt_action_primary() function. It's mostly needed for projectile weapons as it is used to calculate which direction the projectiles need to be fired at in a way that lines up with the player crosshair, while also respecting any potential collisions.

How many times are you calling this? If you're creating a very high fire-rate weapon, making it hitscan based is porbably more performant.

cmr624 commented 7 months ago

understood, thank you for the explanation!

in my case, its just a watering can with a simple action, and was trying to change to a press and hold.

I noticed the framerate drops when I was spamming the primary action button, I can also look into if how the ammo is set up (I tried to basically disable it) causes issues where it is allowed to fire too many times

cmr624 commented 7 months ago

Working on this in this repo! wieldable.gd

Phazorknight commented 7 months ago

Ah okay! Give me a second and I can probably update this to make your life easier.

Phazorknight commented 7 months ago

Okay, please take a look at https://github.com/Phazorknight/Cogito/commit/a0656b0cdfd9223d8ae85a39c6d7715b00b8f51d

I haven't really taken a look at your script but if you're trying to make a watering can that dispense water while the primary action button is held, I'd probably try to just copy the flashlight script, use the "charge" as the water level and adapt it that it turns on when primary action is pressed and then turns off when primary action is released.

cmr624 commented 7 months ago

Thank you! This really helps!

and thanks for the direction on the watering can - I'll definitely look into hooking into that!

cmr624 commented 7 months ago

We can close this since it's not a framerate issue!