Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2025

Conversation

makspll
Copy link
Owner

@makspll makspll commented Feb 22, 2025

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
Copy link
Owner Author

makspll commented Feb 22, 2025

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
16 checks passed
@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