You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-22 15:06:45 +00:00
GDScript: Improve error messages for invalid indexing
These errors are very common when using an invalid property name or calling on an object of the wrong type, and the previous message was a bit cryptic for users. Co-authored-by: Rémi Verschelde <rverschelde@gmail.com> Co-authored-by: golfinq <golfinqz@gmail.com>
This commit is contained in:
@@ -1166,30 +1166,48 @@ bool Variant::has_key(const Variant &p_key, bool &r_valid) const {
|
||||
}
|
||||
}
|
||||
|
||||
void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) {
|
||||
void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid, VariantSetError *err_code) {
|
||||
if (err_code) {
|
||||
*err_code = VariantSetError::SET_OK;
|
||||
}
|
||||
if (type == DICTIONARY || type == OBJECT) {
|
||||
bool valid;
|
||||
set_keyed(p_index, p_value, valid);
|
||||
if (r_valid) {
|
||||
*r_valid = valid;
|
||||
if (!valid && err_code) {
|
||||
*err_code = VariantSetError::SET_KEYED_ERR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bool valid = false;
|
||||
if (p_index.get_type() == STRING_NAME) {
|
||||
set_named(*VariantGetInternalPtr<StringName>::get_ptr(&p_index), p_value, valid);
|
||||
if (!valid && err_code) {
|
||||
*err_code = VariantSetError::SET_NAMED_ERR;
|
||||
}
|
||||
} else if (p_index.get_type() == INT) {
|
||||
bool obb;
|
||||
set_indexed(*VariantGetInternalPtr<int64_t>::get_ptr(&p_index), p_value, valid, obb);
|
||||
if (obb) {
|
||||
valid = false;
|
||||
if (err_code) {
|
||||
*err_code = VariantSetError::SET_INDEXED_ERR;
|
||||
}
|
||||
}
|
||||
} else if (p_index.get_type() == STRING) { // less efficient version of named
|
||||
set_named(*VariantGetInternalPtr<String>::get_ptr(&p_index), p_value, valid);
|
||||
if (!valid && err_code) {
|
||||
*err_code = VariantSetError::SET_NAMED_ERR;
|
||||
}
|
||||
} else if (p_index.get_type() == FLOAT) { // less efficient version of indexed
|
||||
bool obb;
|
||||
set_indexed(*VariantGetInternalPtr<double>::get_ptr(&p_index), p_value, valid, obb);
|
||||
if (obb) {
|
||||
valid = false;
|
||||
if (err_code) {
|
||||
*err_code = VariantSetError::SET_INDEXED_ERR;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (r_valid) {
|
||||
@@ -1198,31 +1216,49 @@ void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid)
|
||||
}
|
||||
}
|
||||
|
||||
Variant Variant::get(const Variant &p_index, bool *r_valid) const {
|
||||
Variant Variant::get(const Variant &p_index, bool *r_valid, VariantGetError *err_code) const {
|
||||
if (err_code) {
|
||||
*err_code = VariantGetError::GET_OK;
|
||||
}
|
||||
Variant ret;
|
||||
if (type == DICTIONARY || type == OBJECT) {
|
||||
bool valid;
|
||||
ret = get_keyed(p_index, valid);
|
||||
if (r_valid) {
|
||||
*r_valid = valid;
|
||||
if (!valid && err_code) {
|
||||
*err_code = VariantGetError::GET_KEYED_ERR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bool valid = false;
|
||||
if (p_index.get_type() == STRING_NAME) {
|
||||
ret = get_named(*VariantGetInternalPtr<StringName>::get_ptr(&p_index), valid);
|
||||
if (!valid && err_code) {
|
||||
*err_code = VariantGetError::GET_NAMED_ERR;
|
||||
}
|
||||
} else if (p_index.get_type() == INT) {
|
||||
bool obb;
|
||||
ret = get_indexed(*VariantGetInternalPtr<int64_t>::get_ptr(&p_index), valid, obb);
|
||||
if (obb) {
|
||||
valid = false;
|
||||
if (err_code) {
|
||||
*err_code = VariantGetError::GET_INDEXED_ERR;
|
||||
}
|
||||
}
|
||||
} else if (p_index.get_type() == STRING) { // less efficient version of named
|
||||
ret = get_named(*VariantGetInternalPtr<String>::get_ptr(&p_index), valid);
|
||||
if (!valid && err_code) {
|
||||
*err_code = VariantGetError::GET_NAMED_ERR;
|
||||
}
|
||||
} else if (p_index.get_type() == FLOAT) { // less efficient version of indexed
|
||||
bool obb;
|
||||
ret = get_indexed(*VariantGetInternalPtr<double>::get_ptr(&p_index), valid, obb);
|
||||
if (obb) {
|
||||
valid = false;
|
||||
if (err_code) {
|
||||
*err_code = VariantGetError::GET_INDEXED_ERR;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (r_valid) {
|
||||
|
||||
Reference in New Issue
Block a user