Skip to content

Commit

Permalink
Added Structs to GDScript
Browse files Browse the repository at this point in the history
  • Loading branch information
nlupugla committed Oct 1, 2024
1 parent 1076403 commit 6fb6053
Show file tree
Hide file tree
Showing 40 changed files with 1,584 additions and 725 deletions.
1 change: 1 addition & 0 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class ScriptLanguage : public Object {
CODE_COMPLETION_KIND_NODE_PATH,
CODE_COMPLETION_KIND_FILE_PATH,
CODE_COMPLETION_KIND_PLAIN_TEXT,
CODE_COMPLETION_KIND_STRUCT,
CODE_COMPLETION_KIND_MAX
};

Expand Down
18 changes: 9 additions & 9 deletions doc/classes/CodeEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -620,31 +620,31 @@
<constant name="KIND_CLASS" value="0" enum="CodeCompletionKind">
Marks the option as a class.
</constant>
<constant name="KIND_FUNCTION" value="1" enum="CodeCompletionKind">
<constant name="KIND_FUNCTION" value="2" enum="CodeCompletionKind">
Marks the option as a function.
</constant>
<constant name="KIND_SIGNAL" value="2" enum="CodeCompletionKind">
<constant name="KIND_SIGNAL" value="3" enum="CodeCompletionKind">
Marks the option as a Godot signal.
</constant>
<constant name="KIND_VARIABLE" value="3" enum="CodeCompletionKind">
<constant name="KIND_VARIABLE" value="4" enum="CodeCompletionKind">
Marks the option as a variable.
</constant>
<constant name="KIND_MEMBER" value="4" enum="CodeCompletionKind">
<constant name="KIND_MEMBER" value="5" enum="CodeCompletionKind">
Marks the option as a member.
</constant>
<constant name="KIND_ENUM" value="5" enum="CodeCompletionKind">
<constant name="KIND_ENUM" value="6" enum="CodeCompletionKind">
Marks the option as an enum entry.
</constant>
<constant name="KIND_CONSTANT" value="6" enum="CodeCompletionKind">
<constant name="KIND_CONSTANT" value="7" enum="CodeCompletionKind">
Marks the option as a constant.
</constant>
<constant name="KIND_NODE_PATH" value="7" enum="CodeCompletionKind">
<constant name="KIND_NODE_PATH" value="8" enum="CodeCompletionKind">
Marks the option as a Godot node path.
</constant>
<constant name="KIND_FILE_PATH" value="8" enum="CodeCompletionKind">
<constant name="KIND_FILE_PATH" value="9" enum="CodeCompletionKind">
Marks the option as a file path.
</constant>
<constant name="KIND_PLAIN_TEXT" value="9" enum="CodeCompletionKind">
<constant name="KIND_PLAIN_TEXT" value="10" enum="CodeCompletionKind">
Marks the option as unclassified or plain text.
</constant>
<constant name="LOCATION_LOCAL" value="0" enum="CodeCompletionLocation">
Expand Down
20 changes: 10 additions & 10 deletions doc/classes/ScriptLanguageExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,25 @@
</constant>
<constant name="CODE_COMPLETION_KIND_CLASS" value="0" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_FUNCTION" value="1" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_FUNCTION" value="2" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_SIGNAL" value="2" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_SIGNAL" value="3" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_VARIABLE" value="3" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_VARIABLE" value="4" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_MEMBER" value="4" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_MEMBER" value="5" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_ENUM" value="5" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_ENUM" value="6" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_CONSTANT" value="6" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_CONSTANT" value="7" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_NODE_PATH" value="7" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_NODE_PATH" value="8" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_FILE_PATH" value="8" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_FILE_PATH" value="9" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_PLAIN_TEXT" value="9" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_PLAIN_TEXT" value="10" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_MAX" value="10" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_MAX" value="11" enum="CodeCompletionKind">
</constant>
</constants>
</class>
12 changes: 12 additions & 0 deletions modules/gdscript/editor/gdscript_docgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ void GDScriptDocGen::_doctype_from_gdtype(const GDType &p_gdtype, String &r_type
}
}
return;
case GDType::STRUCT:
r_type = "Array";
// TODO: just copied from enum for now, probably needs Struct specific logic
r_enum = String(p_gdtype.native_type).replace("::", ".");
if (r_enum.begins_with("res://")) {
r_enum = r_enum.trim_prefix("res://");
int dot_pos = r_enum.rfind(".");
if (dot_pos >= 0) {
r_enum = r_enum.left(dot_pos).quote() + r_enum.substr(dot_pos);
}
}
return;
case GDType::VARIANT:
case GDType::RESOLVING:
case GDType::UNRESOLVED:
Expand Down
32 changes: 32 additions & 0 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,33 @@ GDScript *GDScript::get_root_script() {
return result;
}

const StructInfo *GDScript::get_script_struct_info(const String &p_struct_name, bool p_no_inheritance) const {
if (const StructInfo *info = structs.getptr(p_struct_name)) {
return info;
}

Vector<String> names = String(p_struct_name).split(".");
// TODO: this is to handle cases where the name is something like U"res://main.gd.MyStruct" but there should probably be a better solution
const StructInfo *info = structs.getptr(names[names.size() - 1]);
if (p_no_inheritance) {
return info;
}
// TODO: walk through inheritance chain to look for struct info.
return info;
}

void GDScript::set_script_struct_info(const StructInfo &p_struct_info) {
if (ClassDB::get_struct_info(p_struct_info.name)) {
// TODO: warn about shadowing native struct?
return;
}
if (structs.has(p_struct_info.name)) {
// TODO: warn about shadowing script struct?
return;
}
structs.insert(p_struct_info.name, p_struct_info);
}

RBSet<GDScript *> GDScript::get_dependencies() {
RBSet<GDScript *> dependencies;

Expand Down Expand Up @@ -1361,6 +1388,7 @@ void GDScript::_collect_function_dependencies(GDScriptFunction *p_func, RBSet<GD
}

void GDScript::_collect_dependencies(RBSet<GDScript *> &p_dependencies, const GDScript *p_except) {
// TODO: Do I need Struct logic here?
if (p_dependencies.has(this)) {
return;
}
Expand Down Expand Up @@ -1410,6 +1438,7 @@ GDScript::GDScript() :
}

void GDScript::_save_orphaned_subclasses(ClearData *p_clear_data) {
// TODO: Do I need Struct logic here?
struct ClassRefWithName {
ObjectID id;
String fully_qualified_name;
Expand Down Expand Up @@ -2253,6 +2282,8 @@ void GDScriptLanguage::init() {
_add_global(n, nc);
}

// TODO: Structs?

//populate singletons

List<Engine::Singleton> singletons;
Expand Down Expand Up @@ -2741,6 +2772,7 @@ void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
"namespace", // Reserved for potential future use.
"signal",
"static",
"struct",
"trait", // Reserved for potential future use.
"var",
// Other keywords.
Expand Down
3 changes: 3 additions & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class GDScript : public Script {
HashMap<StringName, Variant> constants;
HashMap<StringName, GDScriptFunction *> member_functions;
HashMap<StringName, Ref<GDScript>> subclasses;
HashMap<StringName, StructInfo> structs;
HashMap<StringName, MethodInfo> _signals;
Dictionary rpc_config;

Expand Down Expand Up @@ -259,6 +260,8 @@ class GDScript : public Script {
}
const HashMap<StringName, GDScriptFunction *> &get_member_functions() const { return member_functions; }
const Ref<GDScriptNativeClass> &get_native() const { return native; }
const StructInfo *get_script_struct_info(const String &p_struct_name, bool p_no_inheritance = false) const;
void set_script_struct_info(const StructInfo &p_struct_info);

RBSet<GDScript *> get_dependencies();
HashMap<GDScript *, RBSet<GDScript *>> get_all_dependencies();
Expand Down
Loading

0 comments on commit 6fb6053

Please sign in to comment.