Skip to content

Commit 971bc94

Browse files
committed
fix settings docs
1 parent a328700 commit 971bc94

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

mods/settings.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ struct MyComplexSettingValue {
590590
std::string value;
591591
592592
// Make sure your value type is comparable
593-
bool operator==(MyComplexSettingValue& other) const = default;
593+
bool operator==(MyComplexSettingValue const& other) const = default;
594594
595595
// If your value type is a thin wrapper around another type, you can allow
596596
// implicit conversions from your type to the wrapped type
@@ -602,29 +602,22 @@ struct MyComplexSettingValue {
602602
MyComplexSettingValue(std::string_view value) : value(value) {}
603603
604604
// Setting values must be copyable!
605-
MyComplexSettingValue(MyComplexSettingValue&) = default;
605+
MyComplexSettingValue(MyComplexSettingValue const&) = default;
606606
};
607607
608608
// You'll have to manually implement JSON serialization for the value
609609
template<>
610610
struct matjson::Serialize<MyComplexSettingValue> {
611611
// Serialize the value into JSON. In this case, we just return the value,
612612
// as strings are inherently JSON-serializable
613-
static matjson::Value to_json(MyComplexSettingValue& settingValue) {
613+
static matjson::Value toJson(MyComplexSettingValue const& settingValue) {
614614
return settingValue.value;
615615
}
616-
617616
// Deserialize the value from JSON, again taking advantage of strings being
618617
// inherently JSON-serializable
619-
static MyComplexSettingValue from_json(matjson::Value const& json) {
620-
return MyComplexSettingValue(json.as_string());
621-
}
622-
623-
// Validate that the JSON value is the type we expect. You can do more
624-
// complex validation here, but in practice most implementations just check
625-
// if it's roughly the correct type (usually object, array, or string)
626-
static bool is_json(matjson::Value const& json) {
627-
return json.is_string();
618+
static Result<MyComplexSettingValue> fromJson(matjson::Value const& json) {
619+
GEODE_UNWRAP_INTO(auto str, json.asString());
620+
return Ok(MyComplexSettingValue(str));
628621
}
629622
};
630623
```
@@ -639,14 +632,19 @@ Custom settings do not necessarily need to inherit from the `SettingValueNodeV3<
639632
#include <Geode/loader/SettingV3.hpp>
640633
#include <Geode/loader/Mod.hpp>
641634

635+
// If you use PCH these are most likely not necessary
636+
#include <Geode/binding/ButtonSprite.hpp>
637+
#include <Geode/binding/CCMenuItemSpriteExtra.hpp>
638+
#include <Geode/binding/FLAlertLayer.hpp>
639+
642640
using namespace geode::prelude;
643641

644642
// Inherit from SettingV3 directly over SettingBaseValueV3, as our setting
645643
// doesn't actually control any value, but it just a simple button
646644
class MyButtonSettingV3 : public SettingV3 {
647645
public:
648646
// Once again implement the parse function
649-
static Result<std::shared_ptr<MyButtonSettingV3>> parse(std::string const& key, std::string const& modID, matjson::Value const& json) {
647+
static Result<std::shared_ptr<SettingV3>> parse(std::string const& key, std::string const& modID, matjson::Value const& json) {
650648
auto res = std::make_shared<MyButtonSettingV3>();
651649
auto root = checkJson(json, "MyButtonSettingV3");
652650

@@ -660,7 +658,7 @@ public:
660658
res->parseEnableIf(root);
661659
662660
root.checkUnknownKeys();
663-
return root.ok(res);
661+
return root.ok(std::static_pointer_cast<SettingV3>(res));
664662
}
665663

666664
// Since there's no data to save or load, these can just return true

0 commit comments

Comments
 (0)