Skip to content

Commit

Permalink
use try blocks instead of unwrap spam
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamby777 committed Dec 1, 2023
1 parent e1d077d commit 21073b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
30 changes: 15 additions & 15 deletions pets-lib/src/dialogue/dbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,23 @@ impl DialogBox {
viewport_y
};

let mut y_tween = node.create_tween().unwrap();
y_tween
.tween_property(
node.clone().upcast(),
"position:y".into(),
Variant::from(tw_end),
DBOX_TWEEN_TIME,
)
.unwrap()
.from(Variant::from(self.node.get_position().y))
.unwrap()
.set_trans(DBOX_TWEEN_TRANS)
.unwrap();
let y_tween: Option<Gd<Tween>> = try {
let mut y_tween = node.create_tween()?;
y_tween
.tween_property(
node.clone().upcast(),
"position:y".into(),
Variant::from(tw_end),
DBOX_TWEEN_TIME,
)?
.from(Variant::from(self.node.get_position().y))?
.set_trans(DBOX_TWEEN_TRANS)?;
y_tween
};

self.active = up;
self.tween = Some(y_tween.clone());
y_tween
self.tween = y_tween.clone();
y_tween.unwrap()
}
}

Expand Down
1 change: 1 addition & 0 deletions pets-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#![feature(variant_count)]
#![feature(try_blocks)]

use godot::engine::Engine;
use godot::prelude::*;
Expand Down
46 changes: 25 additions & 21 deletions pets-lib/src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,31 @@ impl TitleScreen {
theme.get_color("default_color".into(), "RichTextLabel".into())
};

let mut x_tween = node.create_tween().unwrap();
x_tween
.tween_property(
node.clone().upcast(),
"position:x".into(),
Variant::from(target_x),
MENU_TWEEN_TIME,
)
.unwrap()
.set_trans(MENU_TWEEN_TRANS);

let mut color_tween = node.create_tween().unwrap();
color_tween
.tween_property(
node.clone().upcast(),
"theme_override_colors/default_color".into(),
Variant::from(target_col),
MENU_TWEEN_TIME,
)
.unwrap()
.set_trans(MENU_TWEEN_TRANS);
// Tweens
let tweens: Option<()> = try {
// tween x
node.create_tween()?
.tween_property(
node.clone().upcast(),
"position:x".into(),
Variant::from(target_x),
MENU_TWEEN_TIME,
)?
.set_trans(MENU_TWEEN_TRANS);

// tween color
node.create_tween()?
.tween_property(
node.clone().upcast(),
"theme_override_colors/default_color".into(),
Variant::from(target_col),
MENU_TWEEN_TIME,
)?
.set_trans(MENU_TWEEN_TRANS);
};

// panic if null
tweens.unwrap();

// set bbcode
// extremely ugly and hacky solution, but...
Expand Down

0 comments on commit 21073b2

Please sign in to comment.