Index of every page on this site: llms.txt
All skills
jame581 avatar

/resource-pattern

@426e8ab
by Jan Mesarčjame581/godotprompter979 stars
63
Run it yourself

Use when creating data containers in Godot — custom Resources for configuration, items, stats, and editor integration

Use this Skill: https://skilld.dev/gh/jame581/godotprompter/resource-pattern

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencescollections.md

≈992 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Resource Collections

Reference for skills/resource-pattern/SKILL.md — Array[Resource] exports (v1.6.0 C# parity preserved), ResourcePreloader, loading all resources from a directory.

← Back to SKILL.md


6. Resource Collections

Array[Resource] exports

Declare a typed array on a Resource or Node to hold multiple sub-resources editable in the Inspector.

# item_database.gd
class_name ItemDatabase
extends Resource

@export var items: Array[ItemData] = []


func find_by_name(item_name: String) -> ItemData:
    for item: ItemData in items:
        if item.name == item_name:
            return item
    return null

ResourcePreloader

ResourcePreloader is a Node that loads a named set of resources at scene load, holding strong references so they are not unloaded.

# Attach a ResourcePreloader node named "Preloader" to your scene.
# In the Inspector, add resources with string keys.

@onready var preloader: ResourcePreloader = $Preloader


func _ready() -> void:
    var potion: ItemData = preloader.get_resource("health_potion")
    var sword:  ItemData = preloader.get_resource("iron_sword")

Loading all resources from a directory

func load_all_items(dir_path: String) -> Array[ItemData]:
    var result: Array[ItemData] = []
    var dir := DirAccess.open(dir_path)
    if dir == null:
        push_error("ItemDatabase: cannot open directory '%s'" % dir_path)
        return result

    dir.list_dir_begin()
    var file_name := dir.get_next()
    while file_name != "":
        # Exported builds list text resources as "name.tres.remap"; load() follows the remap.
        file_name = file_name.trim_suffix(".remap")
        if not dir.current_is_dir() and (file_name.ends_with(".tres") or file_name.ends_with(".res")):
            var res := load(dir_path.path_join(file_name))
            if res is ItemData:
                result.append(res)
        file_name = dir.get_next()

    return result
// ItemDatabase.cs — typed Resource collection exposed to the Inspector.
using Godot;
using Godot.Collections;

[GlobalClass]
public partial class ItemDatabase : Resource
{
    [Export] public Array<ItemData> Items { get; set; } = new();

    public ItemData FindByName(string itemName)
    {
        foreach (ItemData item in Items)
        {
            if (item.Name == itemName)
                return item;
        }
        return null;
    }
}

// Loading all resources from a directory at runtime.
public static class ItemDatabaseLoader
{
    public static Array<ItemData> LoadAllItems(string dirPath)
    {
        var result = new Array<ItemData>();
        using var dir = DirAccess.Open(dirPath);
        if (dir == null)
        {
            GD.PushError($"ItemDatabase: cannot open directory '{dirPath}'");
            return result;
        }

        dir.ListDirBegin();
        string fileName = dir.GetNext();
        while (fileName != string.Empty)
        {
            // Exported builds list text resources as "name.tres.remap"; Load() follows the remap.
            fileName = fileName.TrimSuffix(".remap");
            if (!dir.CurrentIsDir() && (fileName.EndsWith(".tres") || fileName.EndsWith(".res")))
            {
                var res = ResourceLoader.Load(dirPath.PathJoin(fileName));
                if (res is ItemData item)
                    result.Add(item);
            }
            fileName = dir.GetNext();
        }
        return result;
    }
}

Godot 4.4+: ResourceLoader.list_directory(path) (C#: ResourceLoader.ListDirectory(path)) returns the original file names with the .remap, .import and .uid suffixes already stripped and sub-directories ending in /, so the manual .remap handling above is only needed on 4.3.

Use Godot.Collections.Array<T> (not System.Collections.Generic.List<T>) for [Export] — only the Godot collection is editor-serializable.


Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts7d3 checks · Risk SAFE
  • Gen Agent Trust Hub7d

    This skill provides standard design patterns for Godot engine development using the Resource class for data storage and configuration. It includes clear documentation on best practices and specifically warns users about the security risks associated with loading resource files from untrusted sources.

  • Socket7d

    No alerts

  • Snyk7d

    Risk: LOW · No issues

Signed by skilld at 426e8ab. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 6 days ago.

Activeupdated last month

README badge

README badge for jame581/godotprompter/resource-pattern