Skip to content

feat: add construct global for constructing arbitrary types & Union type#302

Merged
makspll merged 1 commit into
mainfrom
feat/arbitrary-constructors
Feb 22, 2025
Merged

feat: add construct global for constructing arbitrary types & Union type#302
makspll merged 1 commit into
mainfrom
feat/arbitrary-constructors

Conversation

@makspll

@makspll makspll commented Feb 22, 2025

Copy link
Copy Markdown
Owner

Makes this possible:

local type = world.get_type_by_name("SimpleEnum")

-- Struct Variant
local constructed = construct(type, {
    variant = "Struct",
    foo = 123
})

assert(constructed:variant_name() == "Struct", "Value was constructed incorrectly, expected constructed.variant to be Struct but got " .. constructed:variant_name())
assert(constructed.foo == 123, "Value was constructed incorrectly, expected constructed.foo to be 123 but got " .. constructed.foo)


-- TupleStruct Variant
local constructed = construct(type, {
    variant = "TupleStruct",
    _1 = 123
})

assert(constructed:variant_name() == "TupleStruct", "Value was constructed incorrectly, expected constructed.variant to be TupleStruct but got " .. constructed:variant_name())
assert(constructed._1 == 123, "Value was constructed incorrectly, expected constructed._1 to be 123 but got " .. constructed._1)

-- Unit Variant
local constructed = construct(type, {
    variant = "Unit"
})

assert(constructed:variant_name() == "Unit", "Value was constructed incorrectly, expected constructed.variant to be Unit but got " .. constructed:variant_name())

@makspll makspll force-pushed the feat/arbitrary-constructors branch from 7094d12 to b65214a Compare February 22, 2025 01:23
@makspll makspll changed the title feat: add construct global for constructing arbitrary FromReflect feat: add construct global for constructing arbitrary types & Union type Feb 22, 2025
@makspll makspll added the needs documentation For features or changes which still need changes in the book label Feb 22, 2025
@makspll

makspll commented Feb 22, 2025

Copy link
Copy Markdown
Owner Author

There's a gotcha with tuples, which can be constructed but need FromReflect manually registered bevyengine/bevy#17981

@makspll makspll enabled auto-merge (squash) February 22, 2025 01:29
@makspll makspll merged commit e3275e2 into main Feb 22, 2025
@makspll makspll deleted the feat/arbitrary-constructors branch February 22, 2025 01:55
@makspll makspll removed the needs documentation For features or changes which still need changes in the book label Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant