extends Node # Starts/stops virtual reality and provides notifications for other components # # This is a central connecting point for games that can run with and without VR. # It sends out notifications when virtual reality is started/stopped so other parts # of the game can react to it. # ------------------------------------------------------------------------------------------------- # Emitted when virtual reality is started or stopped # @param state True if virtual reality is now active, false if it no longer is active signal VirtualRealityToggled(state) # ------------------------------------------------------------------------------------------------- # Configuration manager used to access the current settings of theg ame var configurationManager # Active virtual reality interface the game is using var virtualRealityInterface # Transform that is used to cancel out the user's default head position var calibrationTransform # ------------------------------------------------------------------------------------------------- # Initializes the script component when the node is added to the scene func _ready(): InitializeIfNotYetDone() # ------------------------------------------------------------------------------------------------- # Initializes virtual reality if this hasn't already happened # @remarks # With this method, other components can avoid having to depend on an initialization # order that needs to be set up behind the scenes (and that would have no documentation # in an obvious place to indicate that) func InitializeIfNotYetDone(): if self.configurationManager == null: self.configurationManager = get_node("/root/Systems/ConfigurationManager") assert(self.configurationManager != null) self.configurationManager.InitializeIfNotYetDone() # If virtual reality is enabled in the game's settings, start it now if self.configurationManager.IsVirtualRealityEnabled(): startVirtualRealityWithoutUpdatingConfiguration() # ------------------------------------------------------------------------------------------------- # Starts virtual reality and updates the game's settings func StartVirtualReality(): startVirtualRealityWithoutUpdatingConfiguration() self.configurationManager.EnableVirtualReality(true) # ------------------------------------------------------------------------------------------------- # Stops virtual reality and updates the game's settings func StopVirtualReality(): if self.virtualRealityInterface != null: print("%.3f Shutting down virtual reality" % (OS.get_ticks_msec() / 1000.0)) get_viewport().arvr = false get_viewport().hdr = true self.virtualRealityInterface.uninitialize() self.virtualRealityInterface = null print("%.3f Virtual reality now stopped" % (OS.get_ticks_msec() / 1000.0)) emit_signal("VirtualRealityToggled", false) self.configurationManager.EnableVirtualReality(false) # ------------------------------------------------------------------------------------------------- # Returns whether virtual reality is currently active # @returns True if virtual reality is active, false otherwise func IsVirtualRealityActive(): if self.virtualRealityInterface == null: return false else: return self.virtualRealityInterface.interface_is_initialized # ------------------------------------------------------------------------------------------------- # Stores a transform that cancels out the user's default head position # @param calibrationTransform Transform that cancels out the user's head position func StoreCalibrationTransform(var calibrationTransform): self.calibrationTransform = calibrationTransform # ------------------------------------------------------------------------------------------------- # Retrieves the transform for cancelling out the user's default head position # @returns The calibration transform for cancelling out the user's default head position func GetCalibrationTransform(): return self.calibrationTransform # ------------------------------------------------------------------------------------------------- # Starts virtual reality without updating theg game's configuration file func startVirtualRealityWithoutUpdatingConfiguration(): print("%.3f Attempting to initialize virtual reality" % (OS.get_ticks_msec() / 1000.0)) if self.virtualRealityInterface == null: var interfaceName = self.configurationManager.GetVirtualRealityInterfaceName() self.virtualRealityInterface = ARVRServer.find_interface(interfaceName) assert(self.virtualRealityInterface != null) self.virtualRealityInterface.initialize() get_viewport().arvr = true get_viewport().hdr = false # CHECK: is this necessary? print("%.3f Virtual reality now active" % (OS.get_ticks_msec() / 1000.0)) emit_signal("VirtualRealityToggled", true)