You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Merge pull request #82639 from golfinq/gdscript-improve-indexing-error
GDScript: Improve error messages for invalid indexing
This commit is contained in:
@@ -1171,30 +1171,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) {
|
||||
@@ -1203,31 +1221,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