diff --git a/core/core_bind.cpp b/core/core_bind.cpp index 0a1faf7940c..7c5ca60465c 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1350,7 +1350,7 @@ void Thread::_start_func(void *ud) { target_callable.callp(nullptr, 0, ret, ce); // If script properly kept a reference to the thread, we should be able to re-reference it now // (well, or if the call failed, since we had to break chains anyway because the outcome isn't known upfront). - t = Ref(ObjectDB::get_instance(th_instance_id)); + t = ObjectDB::get_ref(th_instance_id); if (t.is_valid()) { t->ret = ret; t->running.clear(); diff --git a/core/error/error_macros.cpp b/core/error/error_macros.cpp index adf58f5b674..74dc933695b 100644 --- a/core/error/error_macros.cpp +++ b/core/error/error_macros.cpp @@ -188,7 +188,7 @@ void _physics_interpolation_warning(const char *p_function, const char *p_file, } else { String node_name; if (p_id.is_valid()) { - Node *node = Object::cast_to(ObjectDB::get_instance(p_id)); + Node *node = ObjectDB::get_instance(p_id); if (node && node->is_inside_tree()) { node_name = "\"" + String(node->get_path()) + "\""; } else { diff --git a/core/object/object.h b/core/object/object.h index 38d0ad7f410..66ed5bba412 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -46,6 +46,9 @@ template class TypedArray; +template +class Ref; + enum PropertyHint { PROPERTY_HINT_NONE, ///< no hint provided. PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians_as_degrees][,degrees][,exp][,suffix:] range. @@ -1075,6 +1078,15 @@ public: return object; } + + template + _ALWAYS_INLINE_ static T *get_instance(ObjectID p_instance_id) { + return Object::cast_to(get_instance(p_instance_id)); + } + + template + _ALWAYS_INLINE_ static Ref get_ref(ObjectID p_instance_id); // Defined in ref_counted.h + static void debug_objects(DebugFunc p_func); static int get_object_count(); }; diff --git a/core/object/ref_counted.h b/core/object/ref_counted.h index 5b330fe4acd..10d5e761614 100644 --- a/core/object/ref_counted.h +++ b/core/object/ref_counted.h @@ -299,3 +299,8 @@ struct VariantInternalAccessor &> { // Zero-constructing Ref initializes reference to nullptr (and thus empty). template struct is_zero_constructible> : std::true_type {}; + +template +Ref ObjectDB::get_ref(ObjectID p_instance_id) { + return Ref(get_instance(p_instance_id)); +} diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index b1dfa1529fa..0e6303f13f7 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -3099,7 +3099,7 @@ void EditorFileSystem::_queue_refresh_filesystem() { void EditorFileSystem::_refresh_filesystem() { for (const ObjectID &id : folders_to_sort) { - EditorFileSystemDirectory *dir = Object::cast_to(ObjectDB::get_instance(id)); + EditorFileSystemDirectory *dir = ObjectDB::get_instance(id); if (dir) { dir->subdirs.sort_custom(); } diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 00fb834800f..52f3e980220 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -2486,7 +2486,7 @@ void EditorNode::edit_item(Object *p_object, Object *p_editing_owner) { if (kv.key == owner_id || !kv.value.has(plugin)) { continue; } - EditorPropertyResource *epres = Object::cast_to(ObjectDB::get_instance(kv.key)); + EditorPropertyResource *epres = ObjectDB::get_instance(kv.key); if (epres) { // If it's resource property editing the same resource type, fold it later to avoid premature modifications // that may result in unsafe iteration of active_plugins. @@ -2617,7 +2617,7 @@ void EditorNode::_add_to_history(const Object *p_object, const String &p_propert ObjectID history_id = editor_history.get_current(); if (id != history_id) { const MultiNodeEdit *multi_node_edit = Object::cast_to(p_object); - const MultiNodeEdit *history_multi_node_edit = Object::cast_to(ObjectDB::get_instance(history_id)); + const MultiNodeEdit *history_multi_node_edit = ObjectDB::get_instance(history_id); if (multi_node_edit && history_multi_node_edit && multi_node_edit->is_same_selection(history_multi_node_edit)) { return; } diff --git a/editor/gui/scene_tree_editor.cpp b/editor/gui/scene_tree_editor.cpp index 85c17dac919..f790502bfb0 100644 --- a/editor/gui/scene_tree_editor.cpp +++ b/editor/gui/scene_tree_editor.cpp @@ -1290,7 +1290,7 @@ void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_ void SceneTreeEditor::_tree_scroll_to_item(ObjectID p_item_id) { ERR_FAIL_NULL(tree); - TreeItem *item = Object::cast_to(ObjectDB::get_instance(p_item_id)); + TreeItem *item = ObjectDB::get_instance(p_item_id); if (item) { tree->scroll_to_item(item, true); } diff --git a/editor/plugins/animation_player_editor_plugin.cpp b/editor/plugins/animation_player_editor_plugin.cpp index 15eb7b0ac75..ae10f0af9a7 100644 --- a/editor/plugins/animation_player_editor_plugin.cpp +++ b/editor/plugins/animation_player_editor_plugin.cpp @@ -1932,7 +1932,7 @@ AnimationMixer *AnimationPlayerEditor::fetch_mixer_for_library() const { } Node *AnimationPlayerEditor::get_cached_root_node() const { - return Object::cast_to(ObjectDB::get_instance(cached_root_node_id)); + return ObjectDB::get_instance(cached_root_node_id); } bool AnimationPlayerEditor::_validate_tracks(const Ref p_anim) { diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index fe9f4f2f2a1..3267b14653e 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -4740,7 +4740,7 @@ void CanvasItemEditor::_popup_callback(int p_op) { undo_redo->create_action(TTR("Paste Pose")); for (const PoseClipboard &E : pose_clipboard) { - Node2D *n2d = Object::cast_to(ObjectDB::get_instance(E.id)); + Node2D *n2d = ObjectDB::get_instance(E.id); if (!n2d) { continue; } diff --git a/editor/plugins/editor_resource_tooltip_plugins.cpp b/editor/plugins/editor_resource_tooltip_plugins.cpp index dfeb59214c2..4b72c0d2ccb 100644 --- a/editor/plugins/editor_resource_tooltip_plugins.cpp +++ b/editor/plugins/editor_resource_tooltip_plugins.cpp @@ -38,7 +38,7 @@ void EditorResourceTooltipPlugin::_thumbnail_ready(const String &p_path, const Ref &p_preview, const Ref &p_small_preview, const Variant &p_udata) { ObjectID trid = p_udata; - TextureRect *tr = Object::cast_to(ObjectDB::get_instance(trid)); + TextureRect *tr = ObjectDB::get_instance(trid); if (!tr) { return; diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 52da8bae21d..9ce9c5715a5 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -758,7 +758,7 @@ void Node3DEditorViewport::_clear_selected() { } void Node3DEditorViewport::_select_clicked(bool p_allow_locked) { - Node *node = Object::cast_to(ObjectDB::get_instance(clicked)); + Node *node = ObjectDB::get_instance(clicked); Node3D *selected = Object::cast_to(node); clicked = ObjectID(); @@ -2121,7 +2121,7 @@ void Node3DEditorViewport::_sinput(const Ref &p_event) { const bool movement_threshold_passed = _edit.original_mouse_pos.distance_to(_edit.mouse_pos) > 8 * EDSCALE; if (selection_in_progress && movement_threshold_passed && clicked.is_valid()) { - if (clicked_wants_append || !editor_selection->is_selected(Object::cast_to(ObjectDB::get_instance(clicked)))) { + if (clicked_wants_append || !editor_selection->is_selected(ObjectDB::get_instance(clicked))) { cursor.region_select = true; cursor.region_begin = _edit.original_mouse_pos; clicked = ObjectID(); @@ -4852,8 +4852,8 @@ bool Node3DEditorViewport::_create_audio_node(Node *p_parent, const String &p_pa void Node3DEditorViewport::_perform_drop_data() { EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); if (spatial_editor->get_preview_material_target().is_valid()) { - GeometryInstance3D *geometry_instance = Object::cast_to(ObjectDB::get_instance(spatial_editor->get_preview_material_target())); - MeshInstance3D *mesh_instance = Object::cast_to(ObjectDB::get_instance(spatial_editor->get_preview_material_target())); + GeometryInstance3D *geometry_instance = ObjectDB::get_instance(spatial_editor->get_preview_material_target()); + MeshInstance3D *mesh_instance = ObjectDB::get_instance(spatial_editor->get_preview_material_target()); if (mesh_instance && spatial_editor->get_preview_material_surface() != -1) { undo_redo->create_action(vformat(TTR("Set Surface %d Override Material"), spatial_editor->get_preview_material_surface())); undo_redo->add_do_method(geometry_instance, "set_surface_override_material", spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_material()); @@ -8522,7 +8522,7 @@ void Node3DEditor::_request_gizmo(Object *p_obj) { } void Node3DEditor::_request_gizmo_for_id(ObjectID p_id) { - Node3D *node = Object::cast_to(ObjectDB::get_instance(p_id)); + Node3D *node = ObjectDB::get_instance(p_id); if (node) { _request_gizmo(node); } diff --git a/editor/plugins/tiles/tile_map_layer_editor.cpp b/editor/plugins/tiles/tile_map_layer_editor.cpp index 2c491a144d9..bfcae65e1d9 100644 --- a/editor/plugins/tiles/tile_map_layer_editor.cpp +++ b/editor/plugins/tiles/tile_map_layer_editor.cpp @@ -48,7 +48,7 @@ #include "core/os/keyboard.h" TileMapLayer *TileMapLayerSubEditorPlugin::_get_edited_layer() const { - return Object::cast_to(ObjectDB::get_instance(edited_tile_map_layer_id)); + return ObjectDB::get_instance(edited_tile_map_layer_id); } void TileMapLayerSubEditorPlugin::draw_tile_coords_over_viewport(Control *p_overlay, const TileMapLayer *p_edited_layer, Ref p_tile_set, bool p_show_rectangle_size, const Vector2i &p_rectangle_origin) { @@ -2177,7 +2177,7 @@ void TileMapLayerEditorTilesPlugin::edit(ObjectID p_tile_map_layer_id) { } } - TileMapLayer *new_tile_map_layer = Object::cast_to(ObjectDB::get_instance(edited_tile_map_layer_id)); + TileMapLayer *new_tile_map_layer = ObjectDB::get_instance(edited_tile_map_layer_id); Ref new_tile_set; if (new_tile_map_layer) { new_tile_set = new_tile_map_layer->get_tile_set(); @@ -3641,7 +3641,7 @@ TileMapLayerEditorTerrainsPlugin::TileMapLayerEditorTerrainsPlugin() { } TileMapLayer *TileMapLayerEditor::_get_edited_layer() const { - return Object::cast_to(ObjectDB::get_instance(edited_tile_map_layer_id)); + return ObjectDB::get_instance(edited_tile_map_layer_id); } void TileMapLayerEditor::_find_tile_map_layers_in_scene(Node *p_current, const Node *p_owner, Vector &r_list) const { diff --git a/editor/plugins/tiles/tile_set_editor.cpp b/editor/plugins/tiles/tile_set_editor.cpp index d92f7c5de4f..a559e993770 100644 --- a/editor/plugins/tiles/tile_set_editor.cpp +++ b/editor/plugins/tiles/tile_set_editor.cpp @@ -784,7 +784,7 @@ void TileSetEditor::remove_expanded_editor() { return; } - Node *original_parent = Object::cast_to(ObjectDB::get_instance(expanded_editor_parent)); + Node *original_parent = ObjectDB::get_instance(expanded_editor_parent); if (original_parent) { expanded_editor->remove_meta("reparented"); expanded_editor->reparent(original_parent); diff --git a/editor/plugins/tiles/tiles_editor_plugin.cpp b/editor/plugins/tiles/tiles_editor_plugin.cpp index e1d37069d1d..f86504499b8 100644 --- a/editor/plugins/tiles/tiles_editor_plugin.cpp +++ b/editor/plugins/tiles/tiles_editor_plugin.cpp @@ -339,14 +339,14 @@ void TileMapEditorPlugin::_tile_map_layer_changed() { void TileMapEditorPlugin::_tile_map_layer_removed() { // Workaround for TileMap, making sure the editor stays open when you delete the currently edited layer. - TileMap *tile_map = Object::cast_to(ObjectDB::get_instance(tile_map_group_id)); + TileMap *tile_map = ObjectDB::get_instance(tile_map_group_id); if (tile_map) { edit(tile_map); } } void TileMapEditorPlugin::_update_tile_map() { - TileMapLayer *edited_layer = Object::cast_to(ObjectDB::get_instance(tile_map_layer_id)); + TileMapLayer *edited_layer = ObjectDB::get_instance(tile_map_layer_id); if (edited_layer) { Ref tile_set = edited_layer->get_tile_set(); if (tile_set.is_valid() && tile_set_id != tile_set->get_instance_id()) { @@ -363,7 +363,7 @@ void TileMapEditorPlugin::_update_tile_map() { } void TileMapEditorPlugin::_select_layer(const StringName &p_name) { - TileMapLayer *edited_layer = Object::cast_to(ObjectDB::get_instance(tile_map_layer_id)); + TileMapLayer *edited_layer = ObjectDB::get_instance(tile_map_layer_id); ERR_FAIL_NULL(edited_layer); Node *parent = edited_layer->get_parent(); @@ -415,7 +415,7 @@ void TileMapEditorPlugin::_notification(int p_notification) { } void TileMapEditorPlugin::edit(Object *p_object) { - TileMapLayer *edited_layer = Object::cast_to(ObjectDB::get_instance(tile_map_layer_id)); + TileMapLayer *edited_layer = ObjectDB::get_instance(tile_map_layer_id); if (edited_layer) { edited_layer->disconnect(CoreStringName(changed), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_changed)); edited_layer->disconnect(SceneStringName(tree_exited), callable_mp(this, &TileMapEditorPlugin::_tile_map_layer_removed)); diff --git a/editor/property_selector.cpp b/editor/property_selector.cpp index c4b73f26bf6..d65f4067ca3 100644 --- a/editor/property_selector.cpp +++ b/editor/property_selector.cpp @@ -180,7 +180,7 @@ void PropertySelector::_update_search() { Variant::construct(type, v, nullptr, 0, ce); v.get_method_list(&methods); } else { - Ref