migueldeicaza / SwiftGodotKit

Embed Godot into Swift apps
225 stars 27 forks source link

Cant load resources #9

Open BadChoice opened 11 months ago

BadChoice commented 11 months ago

I'm trying to load resources and it seems it is not working

I tried different ways, but always same error Full path, with the file referenced as as folder

        let path = Bundle.main.path(forResource: "JandaManateeSolid", ofType: "ttf", inDirectory: "Assets/Fonts")!
        let font:Font? = GD.load(path: path)
ERROR: No loader found for resource: /Users/crypto/Library/Developer/Xcode/DerivedData/godotest2-gpffnlaboxbwjxfsoygydhlmxanz/Build/Products/Debug/godotest2.app/Contents/Resources/Assets/Fonts/JandaManateeSolid.ttf (expected type: )
at: _load (core/io/resource_loader.cpp:281)

Or added as a reference group into the project as well, and event trying to use the res:// syntax, all with same error

        let font:Font? = GD.load(path: "res://" + path)
        let font:Font? = GD.load(path: "res://JandaManateeSolid.ttf)
PadraigK commented 11 months ago

You're on the right track with the second approach, I think it's just returning something that's not a Font.

let resource = GD.load(path: "res://JandaManateeSolid.ttf)
GD.print(resource)

and see what type it's giving you

BadChoice commented 11 months ago

Hi I tried this 4 cases,

let path = Bundle.main.path(forResource: "JandaManateeSolid", ofType: "ttf", inDirectory: "Assets/Fonts")!
let resource = GD.load(path: path)
print("[TBD] Resource: \(resource)")

let path2 = Bundle.main.path(forResource: "JandaManateeSolid", ofType: "ttf")!
let resource2 = GD.load(path: path2)
print("[TBD] Resource: \(resource2)")

let resource3 = GD.load(path: "res://JandaManateeSolid.ttf")
print("[TBD] Resource: \(resource3)")

let resource4 = GD.load(path: "res://Assets/Fonts/JandaManateeSolid.ttf")
print("[TBD] Resource: \(resource4)")

all of them returned nil

This is how I store the files (one as reference folder and the other one as group)

image
migueldeicaza commented 11 months ago

Is this SeiftGodotKit or SeiftGodot?

PadraigK commented 11 months ago

When I do this:

let font = GD.load(path: "res://assets/Kenney Thick.ttf")
GD.print(font)

It produces: // Optional(<FontFile#-9223372004793514644>)

so I would have expected your fourth option to work 🤔

Is your project.godot at the root? Anything unusual about the font file itself? If you try opening the godot project once, does the ttf file appear in resources?

BadChoice commented 11 months ago

I'm using SwiftGodotKit, so no editor or project.godot file

PadraigK commented 11 months ago

Ah, I see. I've used this in my SceneGen project like this: https://github.com/PadraigK/SceneGen/blob/b57e157fef771275da6c9b94efbab389c69f19ce/Sources/SceneGen/Helpers/GodotTools.swift#L26-L35

I found I did need a project file to be able to access resources at all, but it can probably be very minimal.

migueldeicaza commented 11 months ago

I suspect SwiftGodotKit needs some initialization to establish the base directory to load files from when using res://

BadChoice commented 10 months ago

I added a simple project.godot

; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
;   [section] ; section goes between []
;   param=value ; assign values to parameters

config_version=5

[application]

config/name="Godot name"
config/features=PackedStringArray("4.0")

[debug]

gdscript/warnings/unused_parameter=0

[display]

window/size/resizable=true

[importer_defaults]

texture={
"compress/channel_pack": 0,
"compress/hdr_compression": 1,
"compress/high_quality": false,
"compress/lossy_quality": 0.7,
"compress/mode": 0,
"compress/normal_map": 0,
"detect_3d/compress_to": 0,
"mipmaps/generate": false,
"mipmaps/limit": -1,
"process/fix_alpha_border": true,
"process/hdr_as_srgb": false,
"process/hdr_clamp_exposure": false,
"process/normal_map_invert_y": false,
"process/premult_alpha": false,
"process/size_limit": 0,
"roughness/mode": 0,
"roughness/src_normal": ""
}

[rendering]

2d/snap/snap_2d_transforms_to_pixel=true
2d/snap/snap_2d_vertices_to_pixel=true
textures/canvas_textures/default_texture_filter=0

and loaded the godot like this

runGodot(
    args: ["--path", Bundle.main.resourcePath ?? ""],
    initHook: registerTypes,
    loadScene: loadScene,
    loadProjectSettings: { settings in
        settings.set(property: "application/config/name", value: "TBD Property")
    }
)

where bundle.main.resourcePath is where the project.godot file are, as well as the Assets

image

But still all the different load methods return nil, I also tried with resource loader itself, and with another filetype with no luck

let a = ResourceLoader.load(path: "res://Assets/Music/8bits-retro.mp3")
PadraigK commented 10 months ago

Is the ResourceLoader.load running inside the loadScene callback?

BadChoice commented 10 months ago

yes, I have a Game.setup() method that ends up calling the ResourceLoad.load

pcbeard commented 7 months ago

From my understanding GD.load() only works with "res://" URLs. You have to import resources into a Godot project before you can access them with GD.load(). You can then export your Godot project as a .pck file and resources can also be loaded from that. This is how you'd want to ship your game.

hyouuu commented 5 months ago

I have a similar issue when trying to run the Dodge sample: https://gist.github.com/hyouuu/94a59fc5a5dbe071c736a39c68678325

Thoughts @migueldeicaza ?

BadChoice commented 1 month ago

I still haven't been able to get resources working on a pure swiftgodokit app, anybody got any luck with that? I creted a godot project, imported the files there, and trying to use it, but no luck either