Skip to content

Commit

Permalink
Improve use of Ref.is_null/valid
Browse files Browse the repository at this point in the history
Use `is_null` over `!is_valid` and vice versa.
  • Loading branch information
AThousandShips committed Dec 23, 2024
1 parent 0f95e9f commit a1846b2
Show file tree
Hide file tree
Showing 177 changed files with 517 additions and 519 deletions.
2 changes: 1 addition & 1 deletion editor/animation_bezier_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ void AnimationBezierTrackEdit::set_editor(AnimationTrackEditor *p_editor) {
}

void AnimationBezierTrackEdit::_play_position_draw() {
if (!animation.is_valid() || play_position_pos < 0) {
if (animation.is_null() || play_position_pos < 0) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8066,7 +8066,7 @@ void AnimationTrackKeyEditEditor::_time_edit_exited() {
}

AnimationTrackKeyEditEditor::AnimationTrackKeyEditEditor(Ref<Animation> p_animation, int p_track, real_t p_key_ofs, bool p_use_fps) {
if (!p_animation.is_valid()) {
if (p_animation.is_null()) {
return;
}

Expand Down
18 changes: 9 additions & 9 deletions editor/animation_track_editor_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Rect2 AnimationTrackEditAudio::get_key_rect(int p_index, float p_pixels_sec) {

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

if (!stream.is_valid()) {
if (stream.is_null()) {
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
}

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

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

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

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

Expand Down Expand Up @@ -422,7 +422,7 @@ Rect2 AnimationTrackEditSpriteFrame::get_key_rect(int p_index, float p_pixels_se
}

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

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

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

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

if (!stream.is_valid()) {
if (stream.is_null()) {
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
}

Expand Down Expand Up @@ -841,7 +841,7 @@ bool AnimationTrackEditTypeAudio::is_key_selectable_by_distance() const {

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) {
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
if (!stream.is_valid()) {
if (stream.is_null()) {
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right); // Draw diamond.
return;
}
Expand Down Expand Up @@ -1025,7 +1025,7 @@ void AnimationTrackEditTypeAudio::gui_input(const Ref<InputEvent> &p_event) {
for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);

if (!stream.is_valid()) {
if (stream.is_null()) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ void CodeTextEditor::input(const Ref<InputEvent> &event) {

const Ref<InputEventKey> key_event = event;

if (!key_event.is_valid()) {
if (key_event.is_null()) {
return;
}
if (!key_event->is_pressed()) {
Expand Down Expand Up @@ -1053,7 +1053,7 @@ Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCo
tex = get_editor_theme_icon(p_option.display);
} else {
tex = EditorNode::get_singleton()->get_class_icon(p_option.display);
if (!tex.is_valid()) {
if (tex.is_null()) {
tex = get_editor_theme_icon(SNAME("Object"));
}
}
Expand Down
4 changes: 2 additions & 2 deletions editor/create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ bool CreateDialog::_is_type_preferred(const String &p_type) const {
bool CreateDialog::_is_class_disabled_by_feature_profile(const StringName &p_class) const {
Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();

return !profile.is_null() && profile->is_class_disabled(p_class);
return profile.is_valid() && profile->is_class_disabled(p_class);
}

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

ERR_FAIL_COND(!scr.is_valid());
ERR_FAIL_COND(scr.is_null());
is_abstract = scr->is_abstract();
} else {
r_item->set_metadata(0, custom_type_parents[p_type]);
Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_debugger_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ ObjectID EditorDebuggerInspector::add_object(const Array &p_arr) {
if (debug_obj->get_script() != var) {
debug_obj->set_script(Ref<RefCounted>());
Ref<Script> scr(var);
if (!scr.is_null()) {
if (scr.is_valid()) {
ScriptInstance *scr_instance = scr->placeholder_instance_create(debug_obj);
if (scr_instance) {
debug_obj->set_script_and_instance(var, scr_instance);
Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_debugger_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void EditorDebuggerNode::_notification(int p_what) {
} break;

case NOTIFICATION_PROCESS: {
if (!server.is_valid()) {
if (server.is_null()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion editor/editor_autoload_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
scn.instantiate();
scn->set_path(p_path);
scn->reload_from_file();
ERR_FAIL_COND_V_MSG(!scn.is_valid(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));
ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));

if (scn.is_valid()) {
n = scn->instantiate();
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void EditorSelectionHistory::cleanup_history() {
bool fail = false;

for (int j = 0; j < history[i].path.size(); j++) {
if (!history[i].path[j].ref.is_null()) {
if (history[i].path[j].ref.is_valid()) {
// If the node is a MultiNodeEdit node, examine it and see if anything is missing from it.
Ref<MultiNodeEdit> multi_node_edit = history[i].path[j].ref;
if (multi_node_edit.is_valid()) {
Expand Down Expand Up @@ -838,9 +838,9 @@ Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
return Ref<Script>();
}
Ref<Script> s = edited_scene[p_idx].root->get_script();
if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
if (s.is_null() && edited_scene[p_idx].root->get_child_count()) {
Node *n = edited_scene[p_idx].root->get_child(0);
while (!s.is_valid() && n && n->get_scene_file_path().is_empty()) {
while (s.is_null() && n && n->get_scene_file_path().is_empty()) {
s = n->get_script();
n = n->get_parent();
}
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
}

Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT);
ERR_FAIL_COND_V(importer.is_null(), ERR_FILE_CORRUPT);
List<ResourceImporter::ImportOption> options;
importer->get_import_options(p_files[i], &options);
//set default values
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,7 @@ void EditorInspectorArray::_panel_draw(int p_index) {
ERR_FAIL_INDEX(p_index, (int)array_elements.size());

Ref<StyleBox> style = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles));
if (!style.is_valid()) {
if (style.is_null()) {
return;
}
if (array_elements[p_index].panel->has_focus()) {
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Vector<Ref<Texture2D>> EditorInterface::make_mesh_previews(const Vector<Ref<Mesh

for (int i = 0; i < p_meshes.size(); i++) {
const Ref<Mesh> &mesh = p_meshes[i];
if (!mesh.is_valid()) {
if (mesh.is_null()) {
textures.push_back(Ref<Texture2D>());
continue;
}
Expand Down Expand Up @@ -210,7 +210,7 @@ Vector<Ref<Texture2D>> EditorInterface::make_mesh_previews(const Vector<Ref<Mesh
Main::iteration();
Main::iteration();
Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
ERR_CONTINUE(!img.is_valid() || img->is_empty());
ERR_CONTINUE(img.is_null() || img->is_empty());
Ref<ImageTexture> it = ImageTexture::create_from_image(img);

RS::get_singleton()->free(inst);
Expand Down
8 changes: 4 additions & 4 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ Error EditorNode::load_resource(const String &p_resource, bool p_ignore_broken_d
OS::get_singleton()->shell_open(ProjectSettings::get_singleton()->globalize_path(p_resource));
return OK;
}
ERR_FAIL_COND_V(!res.is_valid(), ERR_CANT_OPEN);
ERR_FAIL_COND_V(res.is_null(), ERR_CANT_OPEN);

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

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

if (!sdata.is_valid()) {
if (sdata.is_null()) {
_dialog_display_load_error(lpath, err);
opening_prev = false;

Expand Down
18 changes: 9 additions & 9 deletions editor/editor_resource_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ void EditorResourcePicker::_update_resource() {
assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + edited_resource->get_class());
}

assign_button->set_disabled(!editable && !edited_resource.is_valid());
assign_button->set_disabled(!editable && edited_resource.is_null());
}

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

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

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

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

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

if (at == "ImageTexture" && Ref<Image>(dropped_resource).is_valid()) {
Ref<ImageTexture> texture = edited_resource;
if (!texture.is_valid()) {
if (texture.is_null()) {
texture.instantiate();
}
texture->set_image(dropped_resource);
Expand Down Expand Up @@ -888,7 +888,7 @@ Vector<String> EditorResourcePicker::get_allowed_types() const {
}

void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
if (!p_resource.is_valid()) {
if (p_resource.is_null()) {
edited_resource = Ref<Resource>();
_update_resource();
return;
Expand Down Expand Up @@ -948,7 +948,7 @@ void EditorResourcePicker::set_resource_owner(Object *p_object) {

void EditorResourcePicker::set_editable(bool p_editable) {
editable = p_editable;
assign_button->set_disabled(!editable && !edited_resource.is_valid());
assign_button->set_disabled(!editable && edited_resource.is_null());
load_button->set_visible(editable);
edit_button->set_visible(editable);
}
Expand Down Expand Up @@ -1269,7 +1269,7 @@ void EditorAudioStreamPicker::_update_resource() {

void EditorAudioStreamPicker::_preview_draw() {
Ref<AudioStream> audio_stream = get_edited_resource();
if (!audio_stream.is_valid()) {
if (audio_stream.is_null()) {
get_assign_button()->set_text(TTR("<empty>"));
return;
}
Expand Down
8 changes: 4 additions & 4 deletions editor/editor_resource_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Ref<Texture2D> EditorResourcePreviewGenerator::generate_from_path(const String &
}

Ref<Resource> res = ResourceLoader::load(p_path);
if (!res.is_valid()) {
if (res.is_null()) {
return res;
}
return generate(res, p_size, p_metadata);
Expand Down Expand Up @@ -217,7 +217,7 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<
r_small_texture = generated_small;
}

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

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

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

{
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,7 @@ Ref<Shortcut> ED_GET_SHORTCUT(const String &p_path) {

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

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

return sc;
}
Expand All @@ -1867,7 +1867,7 @@ void ED_SHORTCUT_OVERRIDE(const String &p_path, const String &p_feature, Key p_k
}

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

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

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

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

0 comments on commit a1846b2

Please sign in to comment.