From 6602d484e3916fd0ffb169d9513fd0582777449c Mon Sep 17 00:00:00 2001 From: Malcolm Nixon Date: Thu, 3 Mar 2022 16:33:08 -0500 Subject: [PATCH] Modified NodePath export style to preferred Godot3 approach. Removed unnecessary NodePath exports in preference for ARVR_Helpers Modified PlayerBody and movement functions to use new ARVRHelpers script. --- addons/godot-xr-tools/assets/PlayerBody.gd | 83 ++++++++----------- addons/godot-xr-tools/assets/PlayerBody.tscn | 2 - .../functions/Function_Climb_movement.gd | 13 +-- .../functions/Function_Glide_movement.gd | 24 ++---- .../functions/Function_Wind_movement.gd | 23 +---- .../functions/MovementProvider.gd | 20 ++--- 6 files changed, 51 insertions(+), 114 deletions(-) diff --git a/addons/godot-xr-tools/assets/PlayerBody.gd b/addons/godot-xr-tools/assets/PlayerBody.gd index 753748ac..42e86c64 100644 --- a/addons/godot-xr-tools/assets/PlayerBody.gd +++ b/addons/godot-xr-tools/assets/PlayerBody.gd @@ -38,17 +38,11 @@ export var push_rigid_bodies := true ## GroundPhysicsSettings to apply - can only be typed in Godot 4+ export (Resource) var physics = null setget set_physics, get_physics -## Path to the ARVROrigin node -export (NodePath) var origin = null - -## Path to the ARVRCamera node -export (NodePath) var camera = null - ## ARVROrigin node -var origin_node: ARVROrigin = null +onready var origin_node := ARVRHelpers.get_arvr_origin(self) ## ARVRCamera node -var camera_node: ARVRCamera = null +onready var camera_node := ARVRHelpers.get_arvr_camera(self) ## Player KinematicBody node onready var kinematic_node: KinematicBody = $KinematicBody @@ -91,44 +85,8 @@ class SortProviderByOrder: static func sort_by_order(a, b) -> bool: return true if a.order < b.order else false -# Get our origin node, make sure we have consistent code here -func _get_origin_node() -> ARVROrigin: - var node : ARVROrigin = get_node_or_null(origin) if origin else get_parent() - return node - -# Get our camera node -func _get_camera_node() -> ARVRCamera: - # if we have set a node, try and use it - var node : ARVRCamera - - if camera: - node = get_node_or_null(camera) - if node: - return node - - var o : ARVROrigin = _get_origin_node() - if !o: - return null - - # else get by default name - node = o.get_node_or_null("ARVRCamera") - if node: - return node - - # else find the first camera child - for child in o.get_children(): - if child is ARVRCamera: - return child - - # no luck - return null - # Called when the node enters the scene tree for the first time. func _ready(): - # Get the origin and camera nodes - origin_node = _get_origin_node() - camera_node = _get_camera_node() - # Get the movement providers ordered by increasing order _movement_providers = get_tree().get_nodes_in_group("movement_providers") _movement_providers.sort_custom(SortProviderByOrder, "sort_by_order") @@ -315,13 +273,13 @@ func _guaranteed_physics(): # - Maximum slope is valid func _get_configuration_warning(): # Check the origin node - var test_origin_node = _get_origin_node() - if !test_origin_node or !test_origin_node is ARVROrigin: + var test_origin_node = ARVRHelpers.get_arvr_origin(self) + if !test_origin_node: return "Unable to find ARVR Origin node" # Check the camera node - var test_camera_node = _get_camera_node() - if !test_camera_node or !test_camera_node is ARVRCamera: + var test_camera_node = ARVRHelpers.get_arvr_camera(self) + if !test_camera_node: return "Unable to find ARVR Camera node" # Verify the player radius is valid @@ -339,3 +297,32 @@ func _get_configuration_warning(): # Passed basic validation return "" + +## Find the Player Body from a player node and an optional path +static func get_player_body(node: Node, var path: NodePath = "") -> PlayerBody: + var player_body: PlayerBody + + # Try using the node path first + if path: + player_body = node.get_node(path) as PlayerBody + if player_body: + return player_body + + # Get the origin + var arvr_origin := ARVRHelpers.get_arvr_origin(node) + if !arvr_origin: + return null + + # Attempt to get by the default name + player_body = arvr_origin.get_node_or_null("PlayerBody") as PlayerBody + if player_body: + return player_body + + # Search all children of the origin for the player body + for child in arvr_origin.get_children(): + player_body = child as PlayerBody + if player_body: + return player_body + + # Could not find player body + return null diff --git a/addons/godot-xr-tools/assets/PlayerBody.tscn b/addons/godot-xr-tools/assets/PlayerBody.tscn index ce878058..67568964 100644 --- a/addons/godot-xr-tools/assets/PlayerBody.tscn +++ b/addons/godot-xr-tools/assets/PlayerBody.tscn @@ -8,8 +8,6 @@ height = 1.4 [node name="PlayerBody" type="Node"] script = ExtResource( 1 ) -origin = null -camera = null [node name="KinematicBody" type="KinematicBody" parent="." groups=["player_body"]] collision_layer = 524288 diff --git a/addons/godot-xr-tools/functions/Function_Climb_movement.gd b/addons/godot-xr-tools/functions/Function_Climb_movement.gd index 84ff655c..56b5278f 100644 --- a/addons/godot-xr-tools/functions/Function_Climb_movement.gd +++ b/addons/godot-xr-tools/functions/Function_Climb_movement.gd @@ -38,17 +38,17 @@ export var fling_multiplier := 1.0 export var velocity_averages := 5 ## Pickup function for the left hand -export (NodePath) var left_pickup = null +export (NodePath) var left_pickup ## Pickup function for the right hand -export (NodePath) var right_pickup = null +export (NodePath) var right_pickup # Is the player climbing var is_climbing := false # Node references -var _left_pickup_node : Function_Pickup = null -var _right_pickup_node : Function_Pickup = null +onready var _left_pickup_node: Function_Pickup = get_node(left_pickup) +onready var _right_pickup_node: Function_Pickup = get_node(right_pickup) # Velocity averaging fields var _distances = Array() @@ -57,11 +57,6 @@ var _deltas = Array() # Horizontal vector (multiply by this to get only the horizontal components const horizontal := Vector3(1.0, 0.0, 1.0) -# Called when the node enters the scene tree for the first time. -func _ready(): - _left_pickup_node = get_node(left_pickup) - _right_pickup_node = get_node(right_pickup) - func physics_movement(delta: float, player_body: PlayerBody): # Get the left-hand climbable var left_climbable = _left_pickup_node.picked_up_object diff --git a/addons/godot-xr-tools/functions/Function_Glide_movement.gd b/addons/godot-xr-tools/functions/Function_Glide_movement.gd index e35206f3..224f028a 100644 --- a/addons/godot-xr-tools/functions/Function_Glide_movement.gd +++ b/addons/godot-xr-tools/functions/Function_Glide_movement.gd @@ -48,15 +48,9 @@ export var horizontal_slew_rate := 1.0 ## Slew rate to transition to gliding export var vertical_slew_rate := 2.0 -## Left ARVR Controller -export (NodePath) var left_controller = null - -## Right ARVR Controller -export (NodePath) var right_controller = null - # Node references -var _left_controller_node: ARVRController = null -var _right_controller_node: ARVRController = null +onready var _left_controller_node := ARVRHelpers.get_left_controller(self) +onready var _right_controller_node := ARVRHelpers.get_right_controller(self) # Is the player gliding var is_gliding := false @@ -64,12 +58,6 @@ var is_gliding := false # Horizontal vector (multiply by this to get only the horizontal components const horizontal := Vector3(1.0, 0.0, 1.0) -# Called when the node enters the scene tree for the first time. -func _ready(): - # Get the controllers - _left_controller_node = get_node(left_controller) if left_controller else get_node("../LeftHandController") - _right_controller_node = get_node(right_controller) if right_controller else get_node("../RightHandController") - func physics_movement(delta: float, player_body: PlayerBody): # Skip if either controller is off if !_left_controller_node.get_is_active() or !_right_controller_node.get_is_active(): @@ -125,13 +113,13 @@ func _set_is_gliding(gliding: bool): # This method verifies the MovementProvider has a valid configuration. func _get_configuration_warning(): # Verify the left controller - var test_left_controller_node = get_node_or_null(left_controller) if left_controller else get_node_or_null("../LeftHandController") - if !test_left_controller_node or !test_left_controller_node is ARVRController: + var test_left_controller_node = ARVRHelpers.get_left_controller(self) + if !test_left_controller_node: return "Unable to find left ARVR Controller node" # Verify the right controller - var test_right_controller_node = get_node_or_null(right_controller) if right_controller else get_node_or_null("../RightHandController") - if !test_right_controller_node or !test_right_controller_node is ARVRController: + var test_right_controller_node = ARVRHelpers.get_right_controller(self) + if !test_right_controller_node: return "Unable to find right ARVR Controller node" # Check glide parameters diff --git a/addons/godot-xr-tools/functions/Function_Wind_movement.gd b/addons/godot-xr-tools/functions/Function_Wind_movement.gd index 220c8c1a..af4bcb47 100644 --- a/addons/godot-xr-tools/functions/Function_Wind_movement.gd +++ b/addons/godot-xr-tools/functions/Function_Wind_movement.gd @@ -27,7 +27,7 @@ func _ready(): return # Reparent the sense area to the camera - var camera = get_arvr_camera() + var camera := ARVRHelpers.get_arvr_camera(self) if camera: self.remove_child(_sense_area) camera.add_child(_sense_area) @@ -80,27 +80,6 @@ func physics_movement(delta: float, player_body: PlayerBody): drag_factor = clamp(drag_factor, 0.0, 1.0) player_body.velocity = lerp(player_body.velocity, wind_velocity, drag_factor) -# Get our camera node -func get_arvr_camera() -> ARVRCamera: - # Get the ARVROrigin node - var origin := get_arvr_origin() - if !origin: - return null - - # Attempt to get using the default name - var camera := origin.get_node_or_null("ARVRCamera") as ARVRCamera - if camera: - return camera - - # Find the first ARVRCamera child - for child in origin.get_children(): - camera = child as ARVRCamera - if camera: - return camera - - # Unable to find ARVRCamera - return null - # This method verifies the MovementProvider has a valid configuration. func _get_configuration_warning(): # Call base class diff --git a/addons/godot-xr-tools/functions/MovementProvider.gd b/addons/godot-xr-tools/functions/MovementProvider.gd index 49aa7525..9d332b60 100644 --- a/addons/godot-xr-tools/functions/MovementProvider.gd +++ b/addons/godot-xr-tools/functions/MovementProvider.gd @@ -18,20 +18,10 @@ extends Node ## Enable movement provider export var enabled := true -# Get our origin node, we should be in a branch of this -func get_arvr_origin() -> ARVROrigin: - var parent = get_parent() - while parent: - if parent is ARVROrigin: - return parent - parent = parent.get_parent() - - return null - # Get our player body, this should be a node on our ARVROrigin node. func get_player_body() -> PlayerBody: # get our origin node - var arvr_origin = get_arvr_origin() + var arvr_origin := ARVRHelpers.get_arvr_origin(self) if !arvr_origin: return null @@ -40,8 +30,8 @@ func get_player_body() -> PlayerBody: return null # get our player node - var player_body = arvr_origin.get_node("PlayerBody") - if player_body and player_body is PlayerBody: + var player_body := arvr_origin.get_node("PlayerBody") as PlayerBody + if player_body: return player_body return null @@ -49,7 +39,7 @@ func get_player_body() -> PlayerBody: # If missing we need to add our player body func _create_player_body_node(): # get our origin node - var arvr_origin = get_arvr_origin() + var arvr_origin := ARVRHelpers.get_arvr_origin(self) if !arvr_origin: return @@ -79,7 +69,7 @@ func physics_movement(delta: float, player_body: PlayerBody): # This method verifies the MovementProvider has a valid configuration. func _get_configuration_warning(): # Verify we're within the tree of an ARVROrigin node - var arvr_origin = get_arvr_origin() + var arvr_origin = ARVRHelpers.get_arvr_origin(self) if !arvr_origin: return "This node must be within a branch on an ARVROrigin node"