grow-graphics / gd

Go + Godot 4.3
https://learn.grow.graphics/
MIT License
206 stars 10 forks source link

Access to custom Resource's property crash the game #22

Closed ShirenY closed 4 months ago

ShirenY commented 4 months ago

Thanks for quickly fix for gd.ArrayOf. By using it I'm able to create custom resource for asset list. But when trying to load them in runtime, they don't looks like they are in correct form. When trying to access to the scene list in the resource, it crashes. MRP attached, you can use gd run to reproduce it.

the code,

package main

import (
    "fmt"

    "grow.graphics/gd"
    "grow.graphics/gd/gdextension"
)

// Testlib
//

type TestLib struct {
    gd.Class[TestLib, gd.Resource] `gd:"TestLib"`

    Prefabs gd.ArrayOf[gd.PackedScene]
    Number  gd.Int
}

var TestLibInstance *TestLib

func InitLib(ctx gd.Context) {
    lib, succeed := gd.Load[TestLib](ctx, "res://TestLib.tres")
    if !succeed {
        fmt.Println("Error: Can't load res://TestLib.tres.")
        return
    }

    fmt.Print("Lib  ", lib)
    fmt.Print("Prefab Count  ", lib.Prefabs.Size()) // CRASH

    TestLibInstance = &lib

    for i := 0; i < int(TestLibInstance.Prefabs.Size()); i++ {
        fmt.Println("element ", TestLibInstance.Prefabs.Index(ctx, int64(i)).Super().GetName(ctx))
    }
}

func (l TestLib) AsResource() gd.Resource { return *l.Super() }

// Booter
//

type Booter struct {
    gd.Class[Booter, gd.Node3D] `gd:"Booter"`
}

func (mc *Booter) Ready(ctx gd.Context) {
    fmt.Println("Boot up")
    InitLib(ctx)
}

// Main
func main() {
    godot, ok := gdextension.Link()
    if !ok {
        panic("could not link to godot")
    }

    gd.Register[TestLib](godot)
    gd.Register[Booter](godot)
}

image

output log

D:\Workspace2\testGrow\customResource>gd run
Godot Engine v4.2.2.stable.official.15073afe3 - https://godotengine.org
Vulkan API 1.3.224 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 2060

Boot up
Lib  {{{[] {[] {[] {0 0xc000604680}}}}} <nil> 0}exit status 0xc0000005

customResource.zip

Splizard commented 4 months ago

Crash has been fixed, please review this refactored version: (I would advise not to use global variables, as the lifetime of these cannot be tracked and this will lead to issues).

package main

import (
    "fmt"

    "grow.graphics/gd"
    "grow.graphics/gd/gdextension"
)

// Testlib
//

type TestLib struct {
    gd.Class[TestLib, gd.Resource] `gd:"TestLib"`

    Prefabs gd.ArrayOf[gd.PackedScene]
    Number  gd.Int
}

func (l TestLib) AsResource() gd.Resource { return *l.Super() }

// Booter
//

type Booter struct {
    gd.Class[Booter, gd.Node3D] `gd:"Booter"`

    TestLib *TestLib
}

func (mc *Booter) Ready(ctx gd.Context) {
    fmt.Println("Boot up")

    lib, succeed := gd.Load[*TestLib](mc.Pin(), "res://TestLib.tres")
    if !succeed {
        fmt.Println("Error: Can't load res://TestLib.tres.")
        return
    }

    fmt.Println("Lib  ", lib)
    fmt.Println("Prefab Count  ", lib.Prefabs.Size()) // CRASH

    mc.TestLib = lib

    for i := 0; i < int(mc.TestLib.Prefabs.Size()); i++ {
        fmt.Println("element ", mc.TestLib.Prefabs.Index(ctx, int64(i)).Super().GetName(ctx))
    }
}

// Main
func main() {
    godot, ok := gdextension.Link()
    if !ok {
        panic("could not link to godot")
    }

    gd.Register[TestLib](godot)
    gd.Register[Booter](godot)
}

Output

Godot Engine v4.2.2.stable.official.15073afe3 - https://godotengine.org
Vulkan API 1.3.255 - Forward+ - Using Vulkan Device #0: AMD - AMD Radeon RX 580 Series (RADV POLARIS10)

Boot up
Lib   &{{{[] {[] {[] {0 0xc000c02700}}}}} {[] {4 0xc000693f20}} 0}
Prefab Count   3
element  
element  
element  
Splizard commented 4 months ago

Have fixed resource leaking issues in this case.