Skip to content

Commit a9b6b3d

Browse files
committed
Merge pull request godotengine#100776 from AThousandShips/improve_null_checks
Improve use of `Ref.is_null/valid`
2 parents f42e40b + a1846b2 commit a9b6b3d

File tree

177 files changed

+517
-519
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+517
-519
lines changed

editor/animation_bezier_editor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ void AnimationBezierTrackEdit::set_editor(AnimationTrackEditor *p_editor) {
689689
}
690690

691691
void AnimationBezierTrackEdit::_play_position_draw() {
692-
if (!animation.is_valid() || play_position_pos < 0) {
692+
if (animation.is_null() || play_position_pos < 0) {
693693
return;
694694
}
695695

editor/animation_track_editor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8066,7 +8066,7 @@ void AnimationTrackKeyEditEditor::_time_edit_exited() {
80668066
}
80678067

80688068
AnimationTrackKeyEditEditor::AnimationTrackKeyEditEditor(Ref<Animation> p_animation, int p_track, real_t p_key_ofs, bool p_use_fps) {
8069-
if (!p_animation.is_valid()) {
8069+
if (p_animation.is_null()) {
80708070
return;
80718071
}
80728072

editor/animation_track_editor_plugins.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Rect2 AnimationTrackEditAudio::get_key_rect(int p_index, float p_pixels_sec) {
220220

221221
Ref<AudioStream> stream = object->call("get_stream");
222222

223-
if (!stream.is_valid()) {
223+
if (stream.is_null()) {
224224
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
225225
}
226226

@@ -260,7 +260,7 @@ void AnimationTrackEditAudio::draw_key(int p_index, float p_pixels_sec, int p_x,
260260

261261
Ref<AudioStream> stream = object->call("get_stream");
262262

263-
if (!stream.is_valid()) {
263+
if (stream.is_null()) {
264264
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
265265
return;
266266
}
@@ -379,7 +379,7 @@ Rect2 AnimationTrackEditSpriteFrame::get_key_rect(int p_index, float p_pixels_se
379379

380380
if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
381381
Ref<Texture2D> texture = object->call("get_texture");
382-
if (!texture.is_valid()) {
382+
if (texture.is_null()) {
383383
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
384384
}
385385

@@ -422,7 +422,7 @@ Rect2 AnimationTrackEditSpriteFrame::get_key_rect(int p_index, float p_pixels_se
422422
}
423423

424424
Ref<Texture2D> texture = sf->get_frame_texture(animation_name, frame);
425-
if (!texture.is_valid()) {
425+
if (texture.is_null()) {
426426
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
427427
}
428428

@@ -456,7 +456,7 @@ void AnimationTrackEditSpriteFrame::draw_key(int p_index, float p_pixels_sec, in
456456

457457
if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
458458
texture = object->call("get_texture");
459-
if (!texture.is_valid()) {
459+
if (texture.is_null()) {
460460
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
461461
return;
462462
}
@@ -514,7 +514,7 @@ void AnimationTrackEditSpriteFrame::draw_key(int p_index, float p_pixels_sec, in
514514
}
515515

516516
texture = sf->get_frame_texture(animation_name, frame);
517-
if (!texture.is_valid()) {
517+
if (texture.is_null()) {
518518
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
519519
return;
520520
}
@@ -808,7 +808,7 @@ int AnimationTrackEditTypeAudio::get_key_height() const {
808808
Rect2 AnimationTrackEditTypeAudio::get_key_rect(int p_index, float p_pixels_sec) {
809809
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
810810

811-
if (!stream.is_valid()) {
811+
if (stream.is_null()) {
812812
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
813813
}
814814

@@ -841,7 +841,7 @@ bool AnimationTrackEditTypeAudio::is_key_selectable_by_distance() const {
841841

842842
void AnimationTrackEditTypeAudio::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
843843
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
844-
if (!stream.is_valid()) {
844+
if (stream.is_null()) {
845845
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right); // Draw diamond.
846846
return;
847847
}
@@ -1025,7 +1025,7 @@ void AnimationTrackEditTypeAudio::gui_input(const Ref<InputEvent> &p_event) {
10251025
for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
10261026
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);
10271027

1028-
if (!stream.is_valid()) {
1028+
if (stream.is_null()) {
10291029
continue;
10301030
}
10311031

editor/code_editor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ void CodeTextEditor::input(const Ref<InputEvent> &event) {
866866

867867
const Ref<InputEventKey> key_event = event;
868868

869-
if (!key_event.is_valid()) {
869+
if (key_event.is_null()) {
870870
return;
871871
}
872872
if (!key_event->is_pressed()) {
@@ -1053,7 +1053,7 @@ Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCo
10531053
tex = get_editor_theme_icon(p_option.display);
10541054
} else {
10551055
tex = EditorNode::get_singleton()->get_class_icon(p_option.display);
1056-
if (!tex.is_valid()) {
1056+
if (tex.is_null()) {
10571057
tex = get_editor_theme_icon(SNAME("Object"));
10581058
}
10591059
}

editor/create_dialog.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool CreateDialog::_is_type_preferred(const String &p_type) const {
114114
bool CreateDialog::_is_class_disabled_by_feature_profile(const StringName &p_class) const {
115115
Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
116116

117-
return !profile.is_null() && profile->is_class_disabled(p_class);
117+
return profile.is_valid() && profile->is_class_disabled(p_class);
118118
}
119119

120120
bool CreateDialog::_should_hide_type(const StringName &p_type) const {
@@ -312,7 +312,7 @@ void CreateDialog::_configure_search_option_item(TreeItem *r_item, const StringN
312312
r_item->set_suffix(0, "(" + suffix + ")");
313313
}
314314

315-
ERR_FAIL_COND(!scr.is_valid());
315+
ERR_FAIL_COND(scr.is_null());
316316
is_abstract = scr->is_abstract();
317317
} else {
318318
r_item->set_metadata(0, custom_type_parents[p_type]);

editor/debugger/editor_debugger_inspector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ ObjectID EditorDebuggerInspector::add_object(const Array &p_arr) {
167167
if (debug_obj->get_script() != var) {
168168
debug_obj->set_script(Ref<RefCounted>());
169169
Ref<Script> scr(var);
170-
if (!scr.is_null()) {
170+
if (scr.is_valid()) {
171171
ScriptInstance *scr_instance = scr->placeholder_instance_create(debug_obj);
172172
if (scr_instance) {
173173
debug_obj->set_script_and_instance(var, scr_instance);

editor/debugger/editor_debugger_node.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void EditorDebuggerNode::_notification(int p_what) {
331331
} break;
332332

333333
case NOTIFICATION_PROCESS: {
334-
if (!server.is_valid()) {
334+
if (server.is_null()) {
335335
return;
336336
}
337337

editor/editor_autoload_settings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
395395
scn.instantiate();
396396
scn->set_path(p_path);
397397
scn->reload_from_file();
398-
ERR_FAIL_COND_V_MSG(!scn.is_valid(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));
398+
ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));
399399

400400
if (scn.is_valid()) {
401401
n = scn->instantiate();

editor/editor_data.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void EditorSelectionHistory::cleanup_history() {
4646
bool fail = false;
4747

4848
for (int j = 0; j < history[i].path.size(); j++) {
49-
if (!history[i].path[j].ref.is_null()) {
49+
if (history[i].path[j].ref.is_valid()) {
5050
// If the node is a MultiNodeEdit node, examine it and see if anything is missing from it.
5151
Ref<MultiNodeEdit> multi_node_edit = history[i].path[j].ref;
5252
if (multi_node_edit.is_valid()) {
@@ -838,9 +838,9 @@ Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
838838
return Ref<Script>();
839839
}
840840
Ref<Script> s = edited_scene[p_idx].root->get_script();
841-
if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
841+
if (s.is_null() && edited_scene[p_idx].root->get_child_count()) {
842842
Node *n = edited_scene[p_idx].root->get_child(0);
843-
while (!s.is_valid() && n && n->get_scene_file_path().is_empty()) {
843+
while (s.is_null() && n && n->get_scene_file_path().is_empty()) {
844844
s = n->get_script();
845845
n = n->get_parent();
846846
}

editor/editor_file_system.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2475,7 +2475,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
24752475
}
24762476

24772477
Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
2478-
ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
2478+
ERR_FAIL_COND_V(importer.is_null(), ERR_FILE_CORRUPT);
24792479
List<ResourceImporter::ImportOption> options;
24802480
importer->get_import_options(p_files[i], &options);
24812481
//set default values

editor/editor_inspector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ void EditorInspectorArray::_panel_draw(int p_index) {
18541854
ERR_FAIL_INDEX(p_index, (int)array_elements.size());
18551855

18561856
Ref<StyleBox> style = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles));
1857-
if (!style.is_valid()) {
1857+
if (style.is_null()) {
18581858
return;
18591859
}
18601860
if (array_elements[p_index].panel->has_focus()) {

editor/editor_interface.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Vector<Ref<Texture2D>> EditorInterface::make_mesh_previews(const Vector<Ref<Mesh
169169

170170
for (int i = 0; i < p_meshes.size(); i++) {
171171
const Ref<Mesh> &mesh = p_meshes[i];
172-
if (!mesh.is_valid()) {
172+
if (mesh.is_null()) {
173173
textures.push_back(Ref<Texture2D>());
174174
continue;
175175
}
@@ -210,7 +210,7 @@ Vector<Ref<Texture2D>> EditorInterface::make_mesh_previews(const Vector<Ref<Mesh
210210
Main::iteration();
211211
Main::iteration();
212212
Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
213-
ERR_CONTINUE(!img.is_valid() || img->is_empty());
213+
ERR_CONTINUE(img.is_null() || img->is_empty());
214214
Ref<ImageTexture> it = ImageTexture::create_from_image(img);
215215

216216
RS::get_singleton()->free(inst);

editor/editor_node.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ Error EditorNode::load_resource(const String &p_resource, bool p_ignore_broken_d
12861286
OS::get_singleton()->shell_open(ProjectSettings::get_singleton()->globalize_path(p_resource));
12871287
return OK;
12881288
}
1289-
ERR_FAIL_COND_V(!res.is_valid(), ERR_CANT_OPEN);
1289+
ERR_FAIL_COND_V(res.is_null(), ERR_CANT_OPEN);
12901290

12911291
if (!p_ignore_broken_deps && dependency_errors.has(p_resource)) {
12921292
Vector<String> errors;
@@ -1706,7 +1706,7 @@ void EditorNode::_save_scene_with_preview(String p_file, int p_idx) {
17061706
// This check prevents the preview from regenerating in case those scenes are then saved.
17071707
// The preview will be generated if no feature profile is set (as the 3D editor is enabled by default).
17081708
Ref<EditorFeatureProfile> profile = feature_profile_manager->get_current_profile();
1709-
if (!profile.is_valid() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D)) {
1709+
if (profile.is_null() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D)) {
17101710
img = Node3DEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_texture()->get_image();
17111711
}
17121712
}
@@ -1815,7 +1815,7 @@ int EditorNode::_save_external_resources(bool p_also_save_external_data) {
18151815

18161816
for (const String &E : edited_resources) {
18171817
Ref<Resource> res = ResourceCache::get_ref(E);
1818-
if (!res.is_valid()) {
1818+
if (res.is_null()) {
18191819
continue; // Maybe it was erased in a thread, who knows.
18201820
}
18211821
Ref<PackedScene> ps = res;
@@ -4021,7 +4021,7 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
40214021
return ERR_FILE_MISSING_DEPENDENCIES;
40224022
}
40234023

4024-
if (!sdata.is_valid()) {
4024+
if (sdata.is_null()) {
40254025
_dialog_display_load_error(lpath, err);
40264026
opening_prev = false;
40274027

editor/editor_resource_picker.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ void EditorResourcePicker::_update_resource() {
8686
assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + edited_resource->get_class());
8787
}
8888

89-
assign_button->set_disabled(!editable && !edited_resource.is_valid());
89+
assign_button->set_disabled(!editable && edited_resource.is_null());
9090
}
9191

9292
void EditorResourcePicker::_update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj) {
93-
if (!edited_resource.is_valid() || edited_resource->get_instance_id() != p_obj) {
93+
if (edited_resource.is_null() || edited_resource->get_instance_id() != p_obj) {
9494
return;
9595
}
9696

@@ -711,7 +711,7 @@ void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_
711711
dropped_resource = drag_data["resource"];
712712
}
713713

714-
if (!dropped_resource.is_valid() && drag_data.has("type") && String(drag_data["type"]) == "files") {
714+
if (dropped_resource.is_null() && drag_data.has("type") && String(drag_data["type"]) == "files") {
715715
Vector<String> files = drag_data["files"];
716716

717717
if (files.size() == 1) {
@@ -733,7 +733,7 @@ void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_
733733
if (at == "BaseMaterial3D" && Ref<Texture2D>(dropped_resource).is_valid()) {
734734
// Use existing resource if possible and only replace its data.
735735
Ref<StandardMaterial3D> mat = edited_resource;
736-
if (!mat.is_valid()) {
736+
if (mat.is_null()) {
737737
mat.instantiate();
738738
}
739739
mat->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, dropped_resource);
@@ -743,7 +743,7 @@ void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_
743743

744744
if (at == "ShaderMaterial" && Ref<Shader>(dropped_resource).is_valid()) {
745745
Ref<ShaderMaterial> mat = edited_resource;
746-
if (!mat.is_valid()) {
746+
if (mat.is_null()) {
747747
mat.instantiate();
748748
}
749749
mat->set_shader(dropped_resource);
@@ -753,7 +753,7 @@ void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_
753753

754754
if (at == "ImageTexture" && Ref<Image>(dropped_resource).is_valid()) {
755755
Ref<ImageTexture> texture = edited_resource;
756-
if (!texture.is_valid()) {
756+
if (texture.is_null()) {
757757
texture.instantiate();
758758
}
759759
texture->set_image(dropped_resource);
@@ -888,7 +888,7 @@ Vector<String> EditorResourcePicker::get_allowed_types() const {
888888
}
889889

890890
void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
891-
if (!p_resource.is_valid()) {
891+
if (p_resource.is_null()) {
892892
edited_resource = Ref<Resource>();
893893
_update_resource();
894894
return;
@@ -948,7 +948,7 @@ void EditorResourcePicker::set_resource_owner(Object *p_object) {
948948

949949
void EditorResourcePicker::set_editable(bool p_editable) {
950950
editable = p_editable;
951-
assign_button->set_disabled(!editable && !edited_resource.is_valid());
951+
assign_button->set_disabled(!editable && edited_resource.is_null());
952952
load_button->set_visible(editable);
953953
edit_button->set_visible(editable);
954954
}
@@ -1269,7 +1269,7 @@ void EditorAudioStreamPicker::_update_resource() {
12691269

12701270
void EditorAudioStreamPicker::_preview_draw() {
12711271
Ref<AudioStream> audio_stream = get_edited_resource();
1272-
if (!audio_stream.is_valid()) {
1272+
if (audio_stream.is_null()) {
12731273
get_assign_button()->set_text(TTR("<empty>"));
12741274
return;
12751275
}

editor/editor_resource_preview.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Ref<Texture2D> EditorResourcePreviewGenerator::generate_from_path(const String &
6767
}
6868

6969
Ref<Resource> res = ResourceLoader::load(p_path);
70-
if (!res.is_valid()) {
70+
if (res.is_null()) {
7171
return res;
7272
}
7373
return generate(res, p_size, p_metadata);
@@ -217,7 +217,7 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<
217217
r_small_texture = generated_small;
218218
}
219219

220-
if (!r_small_texture.is_valid() && r_texture.is_valid() && preview_generators[i]->generate_small_preview_automatically()) {
220+
if (r_small_texture.is_null() && r_texture.is_valid() && preview_generators[i]->generate_small_preview_automatically()) {
221221
Ref<Image> small_image = r_texture->get_image();
222222
small_image = small_image->duplicate();
223223
small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC);
@@ -230,7 +230,7 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<
230230
}
231231
}
232232

233-
if (!p_item.resource.is_valid()) {
233+
if (p_item.resource.is_null()) {
234234
// Cache the preview in case it's a resource on disk.
235235
if (r_texture.is_valid()) {
236236
// Wow it generated a preview... save cache.
@@ -450,7 +450,7 @@ EditorResourcePreview::PreviewItem EditorResourcePreview::get_resource_preview_i
450450

451451
void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
452452
ERR_FAIL_NULL(p_receiver);
453-
ERR_FAIL_COND(!p_res.is_valid());
453+
ERR_FAIL_COND(p_res.is_null());
454454
_update_thumbnail_sizes();
455455

456456
{

editor/editor_settings.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,7 @@ Ref<Shortcut> ED_GET_SHORTCUT(const String &p_path) {
18561856

18571857
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
18581858

1859-
ERR_FAIL_COND_V_MSG(!sc.is_valid(), sc, "Used ED_GET_SHORTCUT with invalid shortcut: " + p_path);
1859+
ERR_FAIL_COND_V_MSG(sc.is_null(), sc, "Used ED_GET_SHORTCUT with invalid shortcut: " + p_path);
18601860

18611861
return sc;
18621862
}
@@ -1867,7 +1867,7 @@ void ED_SHORTCUT_OVERRIDE(const String &p_path, const String &p_feature, Key p_k
18671867
}
18681868

18691869
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
1870-
ERR_FAIL_COND_MSG(!sc.is_valid(), "Used ED_SHORTCUT_OVERRIDE with invalid shortcut: " + p_path);
1870+
ERR_FAIL_COND_MSG(sc.is_null(), "Used ED_SHORTCUT_OVERRIDE with invalid shortcut: " + p_path);
18711871

18721872
PackedInt32Array arr;
18731873
arr.push_back((int32_t)p_keycode);
@@ -1881,7 +1881,7 @@ void ED_SHORTCUT_OVERRIDE_ARRAY(const String &p_path, const String &p_feature, c
18811881
}
18821882

18831883
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
1884-
ERR_FAIL_COND_MSG(!sc.is_valid(), "Used ED_SHORTCUT_OVERRIDE_ARRAY with invalid shortcut: " + p_path);
1884+
ERR_FAIL_COND_MSG(sc.is_null(), "Used ED_SHORTCUT_OVERRIDE_ARRAY with invalid shortcut: " + p_path);
18851885

18861886
// Only add the override if the OS supports the provided feature.
18871887
if (!OS::get_singleton()->has_feature(p_feature)) {

0 commit comments

Comments
 (0)