You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-06 12:20:30 +00:00
Add is_instance_valid() method to GDScript, ending more than a decade of pain.
This commit is contained in:
@@ -1919,9 +1919,7 @@ ObjectID ObjectDB::add_instance(Object *p_object) {
|
|||||||
|
|
||||||
rw_lock->write_lock();
|
rw_lock->write_lock();
|
||||||
instances[++instance_counter] = p_object;
|
instances[++instance_counter] = p_object;
|
||||||
#ifdef DEBUG_ENABLED
|
|
||||||
instance_checks[p_object] = instance_counter;
|
instance_checks[p_object] = instance_counter;
|
||||||
#endif
|
|
||||||
rw_lock->write_unlock();
|
rw_lock->write_unlock();
|
||||||
|
|
||||||
return instance_counter;
|
return instance_counter;
|
||||||
@@ -1932,9 +1930,7 @@ void ObjectDB::remove_instance(Object *p_object) {
|
|||||||
rw_lock->write_lock();
|
rw_lock->write_lock();
|
||||||
|
|
||||||
instances.erase(p_object->get_instance_id());
|
instances.erase(p_object->get_instance_id());
|
||||||
#ifdef DEBUG_ENABLED
|
|
||||||
instance_checks.erase(p_object);
|
instance_checks.erase(p_object);
|
||||||
#endif
|
|
||||||
|
|
||||||
rw_lock->write_unlock();
|
rw_lock->write_unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -762,15 +762,10 @@ public:
|
|||||||
static void debug_objects(DebugFunc p_func);
|
static void debug_objects(DebugFunc p_func);
|
||||||
static int get_object_count();
|
static int get_object_count();
|
||||||
|
|
||||||
#ifdef DEBUG_ENABLED
|
|
||||||
_FORCE_INLINE_ static bool instance_validate(Object *p_ptr) {
|
_FORCE_INLINE_ static bool instance_validate(Object *p_ptr) {
|
||||||
|
|
||||||
return instance_checks.has(p_ptr);
|
return instance_checks.has(p_ptr);
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
_FORCE_INLINE_ static bool instance_validate(Object *p_ptr) { return true; }
|
|
||||||
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//needed by macros
|
//needed by macros
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
|
|||||||
"print_stack",
|
"print_stack",
|
||||||
"instance_from_id",
|
"instance_from_id",
|
||||||
"len",
|
"len",
|
||||||
|
"is_instance_valid",
|
||||||
};
|
};
|
||||||
|
|
||||||
return _names[p_func];
|
return _names[p_func];
|
||||||
@@ -1276,6 +1277,17 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} break;
|
||||||
|
case IS_INSTANCE_VALID: {
|
||||||
|
|
||||||
|
VALIDATE_ARG_COUNT(1);
|
||||||
|
if (p_args[0]->get_type() != Variant::OBJECT) {
|
||||||
|
r_ret = false;
|
||||||
|
} else {
|
||||||
|
Object *obj = *p_args[0];
|
||||||
|
r_ret = ObjectDB::instance_validate(obj);
|
||||||
|
}
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
case FUNC_MAX: {
|
case FUNC_MAX: {
|
||||||
|
|
||||||
@@ -1798,7 +1810,11 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
|
|||||||
mi.return_val.type = Variant::INT;
|
mi.return_val.type = Variant::INT;
|
||||||
return mi;
|
return mi;
|
||||||
} break;
|
} break;
|
||||||
|
case IS_INSTANCE_VALID: {
|
||||||
|
MethodInfo mi("is_instance_valid", PropertyInfo(Variant::OBJECT, "instance"));
|
||||||
|
mi.return_val.type = Variant::BOOL;
|
||||||
|
return mi;
|
||||||
|
} break;
|
||||||
case FUNC_MAX: {
|
case FUNC_MAX: {
|
||||||
|
|
||||||
ERR_FAIL_V(MethodInfo());
|
ERR_FAIL_V(MethodInfo());
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ public:
|
|||||||
PRINT_STACK,
|
PRINT_STACK,
|
||||||
INSTANCE_FROM_ID,
|
INSTANCE_FROM_ID,
|
||||||
LEN,
|
LEN,
|
||||||
|
IS_INSTANCE_VALID,
|
||||||
FUNC_MAX
|
FUNC_MAX
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user