Godot引擎运行时能否添加/隐藏导出变量?工具模式下动态控制脚本变量显隐方案咨询
Great question! Let's break this down step by step—starting with why your initial code wasn't working, then evaluating your proposed max-appendages solution, and finally sharing the most reliable approaches to achieve what you want in Godot.
Why Your First Code Failed
Your initial approach had the right idea, but two critical issues prevented it from working:
- Missing editor refresh trigger: Calling
_get_property_list()directly doesn't tell the Godot editor to update the Inspector. You need to usenotify_property_list_changed()to trigger a refresh. - Invalid loop syntax:
for j in 2isn't valid in Godot. To iterate over 0 and 1, you need to userange(2)instead.
Is Your Max-Appendages + Visibility Toggle Approach Feasible?
Yes, this concept works—but you'll need to adjust how you control visibility (Godot doesn't have a direct HIDE_GROUP() function). Instead, you can use Godot's editor API to toggle group visibility in the Inspector. That said, this method leaves all properties in the background (even when hidden), which is less clean than dynamically generating only the properties you need.
Recommended Methods to Implement This
Method 1: Dynamically Generate Only Needed Properties (Cleanest Approach)
This method generates exactly the number of appendage properties you've set, and refreshes the Inspector automatically when the count changes. It's efficient and keeps your property list clutter-free.
tool extends Node2D # Limit the export range to 0-5 to avoid invalid values export(int, 0, 5) var number_of_appendages = 0 setget number_of_appendages_changed func number_of_appendages_changed(new_val): # Clamp the value to stay within our max limit number_of_appendages = clamp(new_val, 0, 5) # Tell the editor to refresh the property list notify_property_list_changed() func _get_property_list(): var variables_list = [] for i in range(number_of_appendages): # Append the "Name" property for this appendage variables_list.append({ "name": "Appendages/Appendage " + str(i+1) + "/Name", "type": TYPE_STRING, "usage": PROPERTY_USAGE_DEFAULT, "hint": PROPERTY_HINT_NONE, "hint_string": "" }) # Append the "Length" property (with range hint) variables_list.append({ "name": "Appendages/Appendage " + str(i+1) + "/Length", "type": TYPE_REAL, "usage": PROPERTY_USAGE_DEFAULT, "hint": PROPERTY_HINT_RANGE, "hint_string": "-360, 360, 30" }) return variables_list
- Property grouping (
Appendages/...) keeps the Inspector organized with collapsible sections. clamp()ensures the appendage count never exceeds our maximum limit.notify_property_list_changed()is the critical call that makes the editor update visible properties.
Method 2: Pre-Generate All Properties and Toggle Visibility
If you need to keep all appendage properties in the background (e.g., to preserve data even when hidden), you can pre-generate all max properties and toggle their group visibility via the editor API.
tool extends Node2D export(int, 0, 5) var number_of_appendages = 0 setget number_of_appendages_changed const MAX_APPENDAGES = 5 func number_of_appendages_changed(new_val): number_of_appendages = clamp(new_val, 0, MAX_APPENDAGES) # Only run this in the editor (avoids issues in exported builds) if Engine.editor_hint: var editor_interface = get_editor_interface() var inspector = editor_interface.get_inspector() # Toggle visibility for each appendage group for i in range(MAX_APPENDAGES): var group_name = "Appendages/Appendage " + str(i+1) # Show the group if it's within the current count, hide otherwise inspector.set_group_visible(group_name, i < number_of_appendages) func _get_property_list(): var variables_list = [] for i in range(MAX_APPENDAGES): # Add "Name" property variables_list.append({ "name": "Appendages/Appendage " + str(i+1) + "/Name", "type": TYPE_STRING, "usage": PROPERTY_USAGE_DEFAULT, "hint": PROPERTY_HINT_NONE, "hint_string": "" }) # Add "Length" property variables_list.append({ "name": "Appendages/Appendage " + str(i+1) + "/Length", "type": TYPE_REAL, "usage": PROPERTY_USAGE_DEFAULT, "hint": PROPERTY_HINT_RANGE, "hint_string": "-360, 360, 30" }) return variables_list
- Wrapping editor API calls in
if Engine.editor_hint:ensures this code only runs in the Godot editor (it won't execute in exported games). set_group_visible()controls whether each appendage's section is shown in the Inspector.
Final Notes
- Method 1 is almost always preferable because it keeps your property list lean and avoids unnecessary background data.
- Always validate input values (like using
clamp()) to prevent invalid states. - If you need to keep default node properties alongside your custom ones, merge them with your custom list using
super._get_property_list().
内容的提问来源于stack exchange,提问作者cak3_lover

