Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return values from coroutines #7

Closed
broly opened this issue Feb 1, 2023 · 3 comments
Closed

Return values from coroutines #7

broly opened this issue Feb 1, 2023 · 3 comments

Comments

@broly
Copy link

broly commented Feb 1, 2023

Sometimes, when I awaited some data, for example of async loading of resources. I need return it from my coroutine. The best thing that comes to mind is using TTask with co_return. Can you add support for this?

For example:

UE::Tasks::TTask<UItemInstance*> GrantItem(TSoftObjectPtr<UItemAsset> ItemAssetSoft)
{
	UItemAsset* ItemAsset = co_await UE5Coro::Latent::AsyncLoadObject(ItemAssetSoft);

	UItemInstance* ItemInstance = CreateItemInstance(ItemAsset);
	
	co_return ItemInstance;
}

But TTask is not awaitable? Maybe add new type of task, like TCoroTask?

@landelare
Copy link
Owner

In 1.6 the next best thing you can do is FAsyncCoroutine GrantItem(TSoftObjectPtr<T> X, UItemInstance*& Y); with a suitably long-lived pointer. This has the added benefit of being compatible with UFUNCTION.

I'm not willing to mark an engine type as a coroutine, that would break a lot of things. I have experimented with a TAsyncCoroutine<T> subclass that has return values. It's not straightforward to do, which is why it hasn't shipped. :) It's on the list of things I'm interested in adding, but no timeline as to when/if it will happen.

@broly
Copy link
Author

broly commented Feb 1, 2023

Ok, I just want to say: it's would be very useful stuff, especailly with TTuple.

Also I think we can always make USTRUCT child from templated TAsyncCoroutine. And it seems lot of code with coroutines should use UFUNCTIONs rarely.

(I wish templated objects will be supported by UHT. They made possibility to add custom plugins, will see...)

@landelare landelare changed the title TTask within co_return Return values from coroutines Mar 4, 2023
@landelare
Copy link
Owner

1.7 will add support for co_return value;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants