Skip to content

Implement Structs in GDScript - #1152

Draft
decryptedchaos wants to merge 43 commits into
Redot-Engine:masterfrom
decryptedchaos:structs
Draft

Implement Structs in GDScript#1152
decryptedchaos wants to merge 43 commits into
Redot-Engine:masterfrom
decryptedchaos:structs

Conversation

@decryptedchaos

@decryptedchaos decryptedchaos commented Jan 9, 2026

Copy link
Copy Markdown
Member

This adds Structs to GDScript

Status


Functional POC

Tasks:

Refactoring this to fit Godot's COW model. It shouldn't be anymore heavy than the Variant based Refcounted instance, and solve the fundamental issue.

image

This will need extensive debug, testing and cleanup. but is now functional

Examples

# Types.gd
class_name Types

struct Item:
	var id: int
	var quantity: int

struct PlayerSave:
	var username: StringName
	var inventory: Array 
# PlayerManager.gd
extends Node

var data: Types.PlayerSave

func _ready() -> void:
	# 1. Initialize the struct
	data = Types.PlayerSave.new()
	data.username = &"PlayerOne"
	data.inventory = [] # You MUST manually initialize the array
	
	# 2. To add an item, you MUST copy, modify, and re-assign
	add_item(101, 1)
	
	print("Player: ", data.username)
	print("First Item ID: ", data.inventory[0].id)

func add_item(id: int, qty: int) -> void:
	# A. Create the new item
	var new_item := Types.Item.new()
	new_item.id = id
	new_item.quantity = qty
	
	# B. Get a copy of the current inventory
	var temp_inventory := data.inventory.duplicate()
	
	# C. Modify the copy
	temp_inventory.append(new_item)
	
	# D. OVERWRITE the old inventory with the new one
	data.inventory = temp_inventory

Summary by CodeRabbit

Summary

  • New Features

    • Added native GDScript struct support end-to-end: type system, parsing/editor/analysis/compiler/VM runtime integration, struct wrappers and copy-on-write instances, property listing, and JSON/marshalling/bytecode support, plus language-agnostic struct creation/introspection APIs.
    • Struct variants can be encoded/decoded where GDScript module support is enabled.
  • Breaking Changes

    • Removed legacy utilities for “with objects” var/string conversions.
  • Bug Fixes / Behavior Changes

    • Struct variants no longer participate in built-in method/constructor lookup; invalid method/constructor calls now fail early.
  • Tests

    • Updated default-value validation tests to cover struct variants.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Open

Development

Successfully merging this pull request may close these issues.

4 participants