fix resourcehandler errors

This commit is contained in:
Rowan 2025-05-08 15:06:58 -05:00
parent ba9a169288
commit 8cf3ae2686
5 changed files with 5 additions and 69 deletions

View file

@ -18,10 +18,11 @@ config/icon="res://icon.svg"
[autoload]
AsyncResourceHandler="*res://src/async_resource_handler.ts"
Console="*res://addons/dev_console/console.tscn"
DebugDraw="*res://src/debug_draw.ts"
MessageBus="*res://src/message_bus.ts"
AsyncResourceLoader="*res://src/async_resource_loader.ts"
SaveSerializer="*res://src/save_serializer.ts"
[editor]

View file

@ -36,7 +36,7 @@ export default class AsyncResourceHandler extends Node {
return this._instance.load(path, hint_string, use_sub_threads, cache_mode)
}
load<T extends Resource>(path: string, hint_string: string = '', use_sub_threads?: boolean, cache_mode?: ResourceLoader.CacheMode): Promise<T> {
load<T extends Resource>(path: string, hint_string: string = '', use_sub_threads: boolean = false, cache_mode: ResourceLoader.CacheMode = ResourceLoader.CacheMode.CACHE_MODE_REUSE): Promise<T> {
return new Promise(async (resolve, reject) => {
const err = ResourceLoader.load_threaded_request(path, hint_string, use_sub_threads, cache_mode)
@ -51,7 +51,6 @@ export default class AsyncResourceHandler extends Node {
await this.get_tree().process_frame.as_promise()
status = get_status()
}
if (status === ResourceLoader.ThreadLoadStatus.THREAD_LOAD_LOADED) {
return resolve(ResourceLoader.load_threaded_get(path) as T)
} else {

View file

@ -1,61 +0,0 @@
import { GArray, GError, Node, Resource, ResourceLoader, ResourceSaver } from 'godot'
import { GodotError } from './godot_error'
export class ResourceLoadError extends Error { }
export default class AsyncResourceHandler extends Node {
private static _instance: AsyncResourceHandler
static readonly SaveFlags = ResourceSaver.SaverFlags
static readonly CacheMode = ResourceLoader.CacheMode
static get instance() {
return this._instance
}
_ready(): void {
AsyncResourceHandler._instance = this
}
save<T extends Resource>(path: string, data: T, flags: ResourceSaver.SaverFlags): Promise<void> {
return new Promise((resolve, reject) => {
const err = ResourceSaver.save(data, path, flags)
if (err === GError.OK) {
resolve()
} else {
reject(err)
}
})
}
load<T extends Resource>(path: string, hint_string: string = '', use_sub_threads?: boolean, cache_mode?: ResourceLoader.CacheMode): Promise<T> {
return new Promise(async (resolve, reject) => {
const err = ResourceLoader.load_threaded_request(path, hint_string, use_sub_threads, cache_mode)
if (err != GError.OK) {
return reject(new GodotError(err))
}
const get_status = () => ResourceLoader.load_threaded_get_status(path)
let status = get_status()
while (status === ResourceLoader.ThreadLoadStatus.THREAD_LOAD_IN_PROGRESS) {
await this.get_tree().process_frame.as_promise()
status = get_status()
}
if (status === ResourceLoader.ThreadLoadStatus.THREAD_LOAD_LOADED) {
return resolve(ResourceLoader.load_threaded_get(path) as T)
} else {
return reject(new ResourceLoadError())
}
})
}
get_progress(path: string): number {
const arr = new GArray()
ResourceLoader.load_threaded_get_status(path, arr)
return arr.get_indexed(0)
}
}

View file

@ -1,4 +1,4 @@
import { Color, GArray, Node, Node3D, PackedScene, PhysicsBody3D, PhysicsRayQueryParameters3D, Signal1, Timer, Vector3 } from 'godot'
import { Color, GArray, Node, Node3D, PackedScene, PackedStringArray, PhysicsBody3D, PhysicsRayQueryParameters3D, Signal1, Timer, Vector3 } from 'godot'
import { export_file, signal } from 'godot.annotations'
import { export_node, onready } from './annotations'
import Weapon from './weapon'
@ -36,7 +36,7 @@ export default class EquippedWeapon extends Node3D {
_ready(): void {
if (this.starting_weapon != null) {
AsyncResourceHandler.instance.load<Weapon>(this.starting_weapon, 'Weapon').then(weapon => this.equip(weapon))
AsyncResourceHandler.load<Weapon>(this.starting_weapon, 'Weapon').then(weapon => this.equip(weapon))
}
}

View file

@ -223,9 +223,6 @@ export default class PlayerInput extends Node3D {
this._await_camera_callable = Callable.create(this, this._await_camera)
MessageBus.instance.active_camera_changed.connect(this._await_camera_callable)
this._look_direction._camera = this._camera
this._look_direction._world_3d = this.get_world_3d()
this._min_range_sqr = this._sqr(this.min_range)
this._max_range_sqr = this._sqr(this.max_range)