Kode / Kha

Ultra-portable, high performance, open source multimedia framework.
http://kha.tech
zlib License
1.49k stars 173 forks source link

Type Dynamic callbacks in Assets #1477

Closed RblSb closed 11 months ago

RblSb commented 11 months ago

I spended enough hours today debugging why this (not my) silly code breaks on cpp:

Assets.loadEverything(function () {
    // Avoid passing update/render directly,
    // so replacing them via code injection works
    Scheduler.addTimeTask(function () { update(); }, 0, 1 / 60);
    System.notifyOnFrames(function (frames) { render(frames); });
}, (err:AssetError) -> {
    throw err;
});

I want to improve this api, which will also fix future dynamic problems, like dynamicObj.name.contains failure on some static targets. With typed callbacks this should be harder to break and easier to use now. Also, is this possible to get shaders in assets loader? Seems like not, or there is some ways to enable runtime loading?

RobDangerous commented 11 months ago

I'm sorry but... I don't know what you are talking about.

RblSb commented 11 months ago

This PR solves two problems:

Assets.loadEverything(() -> {}, (err:AssetError) -> {
    throw err;
});

Will be detected as (callback, null, null, failed), instead of (callback, filter), since callbacks are not Dynamic anymore. And it also solves problem, when you try to access item.name fields, like:

Assets.loadEverything(() -> {}, item -> {
return item.name.contains("menu");
});

Since item.name will be typed as String, and not Dynamic with unknown native contains method (for example, js has str.includes, but no str.contains)

RobDangerous commented 11 months ago

OK. But I originally made those dynamic because I wanted to add the option to add any data to assets in the khafile. Can typedefs support that?

RblSb commented 11 months ago

There is no typedef A = {foo:Int} & Dynamic; support in haxe yet, but if you think about making this feature before that support, i can change this typedef to abstract, that will provide completion for item.name / other fields with good types, but also allow item.anythingElse fields and return Dynamic values from them.

RobDangerous commented 11 months ago

Yes, let's make some abstract art.