#Why does this function not work when a value is passed in as an argument?

19 messages · Page 1 of 1 (latest)

mellow peak
#

I'm very confused here. I have this weapon class, and in my _attack() function, I'm shooting a raycast using my _query_collision() function.

This works fine:

extends BallisticWeapon
class_name HitscanWeapon

var raycast_origin_node: Node3D

func _attack() -> void:
    super._attack()
    
    # Collision detected and damage
    var collision = _query_collision()
        print(collision)

## Shoots out a ray from the weapon, and returns the first collision that 
## it finds in that path.
func _query_collision() -> Dictionary:
    var space := get_world_3d().direct_space_state
    var query := PhysicsRayQueryParameters3D.create(
        self.raycast_origin_node.global_position,
        self.raycast_origin_node.global_position + -self.global_transform.basis.z * self.max_range
    )
    return space.intersect_ray(query)

But, if I add a destination: Vector3 argument to _query_collision(), replace the self.raycast_origin_node.global_position + -self.global_transform.basis.z with destination, and pass self.raycast_origin_node.global_position + -self.global_transform.basis.z from the _attack() function to _query_collision(), it no longer works.

func _attack() -> void:
    super._attack()
    
    # Collision detected and damage
    var collision = _query_collision(self.raycast_origin_node.global_position + -self.global_transform.basis.z)
    if collision:
        print(collision)

## Shoots out a ray from the weapon, and returns the first collision that 
## it finds in that path.
func _query_collision(destination: Vector3) -> Dictionary:
    var space := get_world_3d().direct_space_state
    var query := PhysicsRayQueryParameters3D.create(
        self.raycast_origin_node.global_position,
        destination * self.max_range
    )
    return space.intersect_ray(query)

What's going on here?

radiant tundra
#

I can't see anything wrong with this code snippet either.

#

Could destination * max_range result in a different location than expected compared to the first snippet?

#

Oh nevermind. I see...

mellow peak
#

Could it have something to do with the super class' _attack()? Here's that:

func _attack() -> void:
    super._attack()
    
    # Rate of fire
    self.can_fire = false
    self.fire_timer.start()
    
    # Decreasing ammo
    self.current_ammo -= 1
    
    Events.player_attacked.emit(
            self.raycast_origin_node.global_position,
            self.raycast_origin_node.global_transform.basis.z * self.max_range
    )
radiant tundra
#

Hm...

#

I can't see why that would effect it...

mellow peak
#

Me neither. I'm tearing my hair out here lol.

#

Is it an engine bug or something?

#

I printed the value of self.raycast_origin_node.global_position + -self.global_transform.basis.z in both functions, and it's the same in both.

#

Hmm. Moving the * self.max_range out of _query_collision like this...

var collision = _query_collision(self.raycast_origin_node.global_position + -self.global_transform.basis.z  * self.max_range)

...seems to fix it

#

But why?

radiant tundra
#

Hm...

#

Is _attack called in physics process?

mellow peak
#

It's called in _input()

radiant tundra
#

Oh... usually I think physics queries should be done in the physics process. Not sure why that would give this specific behavior, but I know doing physics queries outside of it can lead to unexpected behavior. Worth a shot maybe?

mellow peak
#

Okay, I'll give it a shot. Thanks.

#

Hmm, doesn't seem to fix it. Bizarre.

#

Hmmm, is it an order of operations issue?