Initial commit, reusable code for my nvgt based games.
This commit is contained in:
58
dict_utils.nvgt
Normal file
58
dict_utils.nvgt
Normal file
@@ -0,0 +1,58 @@
|
||||
// Dictionary utility helpers.
|
||||
|
||||
double dict_get_number(dictionary@ data, const string&in key, double defaultValue) {
|
||||
double value = 0.0;
|
||||
if (@data is null) return defaultValue;
|
||||
if (data.get(key, value)) return value;
|
||||
|
||||
int valueInt = 0;
|
||||
if (data.get(key, valueInt)) return valueInt;
|
||||
|
||||
string valueString = "";
|
||||
if (data.get(key, valueString)) {
|
||||
return parse_int(valueString);
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
bool dict_get_bool(dictionary@ data, const string&in key, bool defaultValue) {
|
||||
bool value = false;
|
||||
if (@data is null) return defaultValue;
|
||||
if (data.get(key, value)) return value;
|
||||
|
||||
int valueInt = 0;
|
||||
if (data.get(key, valueInt)) return valueInt != 0;
|
||||
|
||||
string valueString = "";
|
||||
if (data.get(key, valueString)) return valueString == "1" || valueString == "true";
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
bool dict_has_keys(dictionary@ data) {
|
||||
if (@data is null) return false;
|
||||
string[]@ keys = data.get_keys();
|
||||
return @keys !is null && keys.length() > 0;
|
||||
}
|
||||
|
||||
bool dict_has_number_key(dictionary@ data, const string&in key) {
|
||||
double value = 0.0;
|
||||
if (@data is null) return false;
|
||||
if (data.get(key, value)) return true;
|
||||
|
||||
int valueInt = 0;
|
||||
if (data.get(key, valueInt)) return true;
|
||||
|
||||
string valueString = "";
|
||||
if (data.get(key, valueString)) return valueString.length() > 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
string[] dict_get_string_list(dictionary@ data, const string&in key) {
|
||||
string[] result;
|
||||
if (@data is null) return result;
|
||||
if (!data.get(key, result)) return result;
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user