Skip to content

Commit 6c9765d

Browse files
committed
Merge pull request #106738 from BastiaanOlij/openxr_add_future_result
OpenXR Futures: Add return value support
2 parents d5301d1 + 16659e3 commit 6c9765d

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

modules/openxr/doc_classes/OpenXRFutureResult.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,26 @@
2121
Return the [code]XrFutureEXT[/code] value this result relates to.
2222
</description>
2323
</method>
24+
<method name="get_result_value" qualifiers="const">
25+
<return type="Variant" />
26+
<description>
27+
Returns the result value of our asynchronous function (if set by the extension). The type of this result value depends on the function being called. Consult the documentation of the relevant function.
28+
</description>
29+
</method>
2430
<method name="get_status" qualifiers="const">
2531
<return type="int" enum="OpenXRFutureResult.ResultStatus" />
2632
<description>
2733
Returns the status of this result.
2834
</description>
2935
</method>
36+
<method name="set_result_value">
37+
<return type="void" />
38+
<param index="0" name="result_value" type="Variant" />
39+
<description>
40+
Stores the result value we expose to the user.
41+
[b]Note:[/b] This method should only be called by an OpenXR extension that implements an asynchronous function.
42+
</description>
43+
</method>
3044
</methods>
3145
<signals>
3246
<signal name="completed">

modules/openxr/extensions/openxr_future_extension.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ void OpenXRFutureResult::_bind_methods() {
4040
ClassDB::bind_method(D_METHOD("get_future"), &OpenXRFutureResult::_get_future);
4141
ClassDB::bind_method(D_METHOD("cancel_future"), &OpenXRFutureResult::cancel_future);
4242

43+
ClassDB::bind_method(D_METHOD("set_result_value", "result_value"), &OpenXRFutureResult::set_result_value);
44+
ClassDB::bind_method(D_METHOD("get_result_value"), &OpenXRFutureResult::get_result_value);
45+
4346
ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::OBJECT, "result", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRFutureResult")));
4447

4548
BIND_ENUM_CONSTANT(RESULT_RUNNING);
@@ -52,10 +55,10 @@ void OpenXRFutureResult::_mark_as_finished() {
5255
status = RESULT_FINISHED;
5356

5457
// Perform our callback
55-
on_success_callback.call((uint64_t)future);
58+
on_success_callback.call(this); // Note, `this` will be converted to a variant that will be refcounted!
5659

57-
// Emit our signal
58-
emit_signal(SNAME("completed"), this);
60+
// Emit our signal, we assume our callback has provided us with the correct result value by calling set_result_value.
61+
emit_signal(SNAME("completed"), result_value);
5962
}
6063

6164
void OpenXRFutureResult::_mark_as_cancelled() {
@@ -65,7 +68,8 @@ void OpenXRFutureResult::_mark_as_cancelled() {
6568
// There is no point in doing a callback for cancellation as its always user invoked.
6669

6770
// But we do emit our signal to make sure any await finishes.
68-
emit_signal(SNAME("completed"), this);
71+
Variant no_result;
72+
emit_signal(SNAME("completed"), no_result);
6973
}
7074

7175
OpenXRFutureResult::ResultStatus OpenXRFutureResult::get_status() const {
@@ -80,6 +84,14 @@ uint64_t OpenXRFutureResult::_get_future() const {
8084
return (uint64_t)future;
8185
}
8286

87+
void OpenXRFutureResult::set_result_value(const Variant &p_result_value) {
88+
result_value = p_result_value;
89+
}
90+
91+
Variant OpenXRFutureResult::get_result_value() const {
92+
return result_value;
93+
}
94+
8395
void OpenXRFutureResult::cancel_future() {
8496
ERR_FAIL_COND(status != RESULT_RUNNING);
8597

modules/openxr/extensions/openxr_future_extension.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@ class OpenXRFutureResult : public RefCounted {
7474
ResultStatus get_status() const;
7575
XrFutureEXT get_future() const;
7676

77+
void set_result_value(const Variant &p_result_value);
78+
Variant get_result_value() const;
79+
7780
void cancel_future();
7881

7982
OpenXRFutureResult(XrFutureEXT p_future, const Callable &p_on_success);
8083

8184
private:
8285
ResultStatus status = RESULT_RUNNING;
8386
XrFutureEXT future;
87+
Variant result_value;
8488
Callable on_success_callback;
8589

8690
uint64_t _get_future() const;

0 commit comments

Comments
 (0)