Skip to content
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

Add Structs to GDscript #90356

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nlupugla
Copy link
Contributor

@nlupugla nlupugla commented Apr 7, 2024

Depends on #82198. That PR implements the new Struct type based on Reduz's proposal godotengine/godot-proposals#7329 in core, but not in GDScript. This PR implements them in GDScript. This PR includes the commit of the dependent PR, but I will drop it once #82198 is merged.

It is still very much a work in progress and currently not a stable branch.

@dalexeev dalexeev added this to the 4.x milestone Apr 7, 2024
@nlupugla nlupugla force-pushed the add-structs-to-gdscript branch 4 times, most recently from dc6625d to d906d86 Compare April 22, 2024 02:08
Comment on lines 2802 to 2899
case GDScriptParser::ClassNode::Member::STRUCT: {
if (!member.m_struct) {
break;
}
const GDScriptParser::StructNode &struct_node = *member.m_struct;
StructInfo struct_info = StructInfo(struct_node.identifier->name, struct_node.members.size());
for (int j = 0; j < struct_info.count; j++) {
const GDScriptParser::VariableNode *struct_member = struct_node.members[j];
Variant default_value = struct_member->initializer ? struct_member->initializer->reduced_value : Variant(); // TODO: need better logic here.
struct_info.set(j,
struct_member->identifier->name,
struct_member->datatype.builtin_type,
struct_member->datatype.native_type,
nullptr,
default_value);
//TODO: does not yet support nested structs.
}
p_script->structs.insert(struct_info.name, struct_info);
} break;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dalexeev @vnen Whenever either of you have time, I'd love some help on how I should be handling struct defaults. The current approach here is to store the reduced value of the initializer expression. The major problem with this approach is that It does not work for reference types like Dictionary or Object because every default struct will have a reference to the same value of the reference type, instead of a fresh copy.

I think the solution is to compile something like an implicit_new function and then call it to obtain a fresh default struct. I'm having trouble implementing that approach however, because I'm not quite sure how implicit_new functions are compiled. It seems like it occurs in _parse_function, but that seems to create the constructor for the whole class, whereas I just want the constructor for a struct within the class. Any tips?

@Lazy-Rabbit-2001
Copy link

Should a struct be exported via @export? If so, then it's better to make sure that:

  1. The exported struct should not display as "Array"; instead, it should be "Struct XXX"
  2. The exported struct cannot be extended or shortened in the inspector.
  3. Each element of the struct in the inspector should display its name rather than its index, and if an typed member of the exported struct is shown in the inspector, its type should be untweakable. Otherwise, a crash will happen.

@nlupugla nlupugla force-pushed the add-structs-to-gdscript branch 3 times, most recently from 86c68a9 to 35d7383 Compare October 3, 2024 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants