Skip to content

Commit

Permalink
Hotfix/invalid object instance (#54)
Browse files Browse the repository at this point in the history
* Fix set attribute error when setting an invalid object instance.

* seperate dead and fmod valid checks
  • Loading branch information
piiertho committed Dec 31, 2020
1 parent cbd10ee commit 4e763c2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.13)
project(GodotFmod)

set(CMAKE_CXX_STANDARD 14)
set(OS "osx")
set(OS "windows")
set(ARCH "")

link_directories("../godot-cpp/bin/")
Expand Down
3 changes: 2 additions & 1 deletion demo/Scenes/FmodScriptTest.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ margin_top = 52.1241
margin_right = 102.068
margin_bottom = 139.124
size_flags_stretch_ratio = 0.0
text = "Listener"
text = "Listener
Kill it with K key!"
align = 1
autowrap = true
__meta__ = {
Expand Down
2 changes: 2 additions & 0 deletions demo/Script/Emitter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ func _process(delta):
else:
print("Mower paused")
Fmod.set_event_paused(id, true)
elif Input.is_action_just_pressed("kill_event"):
self.queue_free()
4 changes: 3 additions & 1 deletion demo/Script/Listener.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ func _process(delta):
direction.y = direction.y * delta * 200
self.position += direction
self.rotate(rotation_dir * delta * 5)
if Input.is_action_just_pressed("lock_listener"):
if Input.is_action_pressed("lock_listener"):
Fmod.set_listener_lock(0, !Fmod.get_listener_lock(0))
elif Input.is_action_pressed("kill"):
self.queue_free()
12 changes: 11 additions & 1 deletion demo/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
config_version=4

_global_script_classes=[ {
"base": "Node",
"base": "",
"class": "FmodNative",
"language": "NativeScript",
"path": "res://addons/fmod/Fmod.gdns"
Expand Down Expand Up @@ -115,3 +115,13 @@ space={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
]
}
kill={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":75,"unicode":0,"echo":false,"script":null)
]
}
lock_listener={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":76,"unicode":0,"echo":false,"script":null)
]
}
34 changes: 21 additions & 13 deletions src/godot_fmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void Fmod::_process(float delta) {
EventInfo *eventInfo = getEventInfo(eventInstance);
if (eventInfo) {
if (eventInfo->gameObj) {
if (isNull(eventInfo->gameObj)) {
if (isDead(eventInfo->gameObj)) {
FMOD_STUDIO_STOP_MODE m = FMOD_STUDIO_STOP_IMMEDIATE;
ERROR_CHECK(eventInstance->stop(m));
releaseOneEvent(eventInstance);
Expand Down Expand Up @@ -268,7 +268,7 @@ void Fmod::setListenerAttributes() {
if (listener->listenerLock) {
continue;
}
if (isNull(listener->gameObj)) {
if (isDead(listener->gameObj)) {
listener->gameObj = nullptr;
ERROR_CHECK(system->setListenerWeight(i, 0));
continue;
Expand Down Expand Up @@ -352,15 +352,23 @@ Dictionary Fmod::getTransform2DInfoFrom3DAttribut(FMOD_3D_ATTRIBUTES &attr) {
return _2Dattr;
}

bool Fmod::isNull(Object *o) {
auto *ci = Object::cast_to<CanvasItem>(o);
auto *s = Object::cast_to<Spatial>(o);
return ci == nullptr && s == nullptr;
bool Fmod::isDead(Object* o) {
if (!o) {
return true;
}
return !godot::core_1_1_api->godot_is_instance_valid(o->_owner);
}

bool Fmod::isFmodValid(Object* o) {
if (o) {
return Object::cast_to<Spatial>(o) || Object::cast_to<CanvasItem>(o);
}
return false;
}

void Fmod::updateInstance3DAttributes(FMOD::Studio::EventInstance *instance, Object *o) {
// try to set 3D attributes
if (instance && !isNull(o)) {
if (instance && isFmodValid(o)) {
auto *ci = Object::cast_to<CanvasItem>(o);
if (ci != nullptr) {
auto attr = get3DAttributesFromTransform2D(ci->get_global_transform());
Expand Down Expand Up @@ -1207,7 +1215,7 @@ void Fmod::playOneShot(const String eventName, Object *gameObj) {
FMOD::Studio::EventInstance *instance = createInstance(eventName, true, nullptr);
if (instance) {
// set 3D attributes once
if (!isNull(gameObj)) {
if (isFmodValid(gameObj)) {
updateInstance3DAttributes(instance, gameObj);
}
ERROR_CHECK(instance->start());
Expand All @@ -1219,7 +1227,7 @@ void Fmod::playOneShotWithParams(const String eventName, Object *gameObj, const
FMOD::Studio::EventInstance *instance = createInstance(eventName, true, nullptr);
if (instance) {
// set 3D attributes once
if (!isNull(gameObj)) {
if (isFmodValid(gameObj)) {
updateInstance3DAttributes(instance, gameObj);
}
// set the initial parameter values
Expand All @@ -1235,7 +1243,7 @@ void Fmod::playOneShotWithParams(const String eventName, Object *gameObj, const
}

void Fmod::playOneShotAttached(const String eventName, Object *gameObj) {
if (!isNull(gameObj)) {
if (isFmodValid(gameObj)) {
FMOD::Studio::EventInstance *instance = createInstance(eventName, true, gameObj);
if (instance) {
ERROR_CHECK(instance->start());
Expand All @@ -1244,7 +1252,7 @@ void Fmod::playOneShotAttached(const String eventName, Object *gameObj) {
}

void Fmod::playOneShotAttachedWithParams(const String eventName, Object *gameObj, const Dictionary parameters) {
if (!isNull(gameObj)) {
if (isFmodValid(gameObj)) {
FMOD::Studio::EventInstance *instance = createInstance(eventName, true, gameObj);
if (instance) {
// set the initial parameter values
Expand All @@ -1260,8 +1268,8 @@ void Fmod::playOneShotAttachedWithParams(const String eventName, Object *gameObj
}

void Fmod::attachInstanceToNode(const uint64_t instanceId, Object *gameObj) {
if (isNull(gameObj)) {
GODOT_LOG(1, "Trying to attach event instance to null game object")
if (!isFmodValid(gameObj)) {
GODOT_LOG(1, "Trying to attach event instance to null game object or object is not Spatial or CanvasItem")
return;
}
FIND_AND_CHECK(instanceId, events)
Expand Down
3 changes: 2 additions & 1 deletion src/godot_fmod.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ namespace godot {
Dictionary getTransformInfoFrom3DAttribut(FMOD_3D_ATTRIBUTES &attribut);
Dictionary getTransform2DInfoFrom3DAttribut(FMOD_3D_ATTRIBUTES &attribut);

bool isNull(Object *o);
static bool isDead(Object* o);
static bool isFmodValid(Object* o);
void updateInstance3DAttributes(FMOD::Studio::EventInstance *instance, Object *o);
void runCallbacks();

Expand Down

0 comments on commit 4e763c2

Please sign in to comment.