[HELP] Resources can't share objects

The export system in the CFX framework is horrifyingly broken garbage, and I bet none of you will have anything useful to say to help me. Edit: was proven wrong <3

Let’s find out. Here is a stripped down version of my issue.

Explain why Test #3 fails despite the other two tests passing. I will literally buy you a beer/coffee over PayPal if you can give me a way make one resource make a class/functioned object in another. It isn’t possible.

EDIT: resources are here: RacingGameModes.zip (1.7 KB) , save them to here:
gamemode

My guess is that it has to do with calling the class as a static https://jsfiddle.net/2tds6z14/. So in your case var race = new expo.... should work.

1 Like

(thanks for replying) you know, as I saved that image out I noticed that one line of collapsed code and thought ‘surely nobody is gonna notice that’. But you did. Thank you for paying attention. Unfortunately I am using ‘new’ - any other ideas?

Image from original post is now updated, see er_server.js:2

1 Like

Can you do a console.debug(race) below the var race = export.. and show the result of that.

Returns an empty object {}

This is essentially the crux of the problem I am having. Where did my functionality go??
It’s as if CFX.re is just ‘exporting’ the properties but not the methods…

EDIT: Here’s the code, I encourage you to try this yourself because man I am so stumped…
RacingGameModes.zip (1.7 KB) , extract this here:
gamemode

Try not using a class which needs to have its prototype methods invoked with a bind-this, but a normal object containing closures, which might also just be bound to the class.

ScRT exports are meant to be universal and cross-runtime, so it has no understanding of high-level constructs in JS/C# like prototype/class ‘methods’, just functions which get passed by-reference and values which get passed by-value.

E.g.:

exports('wtf', () => {
    const race = new Race();

    return {
        getStuff: race.getStuff.bind(race)
    };
});

You can also of course automate generation of objects with bound closures.

P.S.: Calling a system you do not understand the limitations and design decisions of ‘horrifyingly broken garbage’ is not likely to evoke a positive response.

2 Likes

You mean like this? No dice.

Regardless of that ““fact”” which you mention, I am able to compile ECMA6 which includes OOP closures, and as you can see from my example, CFX understands classes and runs them. In actual fact EMCA6 is just making ‘plain old prototyped objects’ in the background, just in a less agonizingly hard-to-read way. Yes I am dumbing down how it actually works, but you see what I mean. It shouldn’t be the issue…

But hey let’s assume you are right and pretend that Classes are the problem here…
Let’s do it “YE OLE FASHIONED WAY” for no reason at all.

I tried that. The method calls, then it throws the access to the properties away.
Note the undefined variable.

I’m happy to take back my assertions about the broken garbage when you prove me wrong.

Edit: Yes I tried call(race), and yes I tried puttng the export after the definition.
Also no dice.

You are misunderstanding the logic behind exports. Exports aren’t broken. Exports are created to be interchangeable between all supported runtimes. That means no complex data structures will be supported. How would you interpret the context of this in Lua? Or C#? Your JS object is nothing like those languages, hence complex datastructures get lost. Nothing is broken here.

In your last example, omitting this. from sayHello would work fine. Otherwise I suggest to work with simpler data structures for your resource, or consider an alternative approach to your problem. Of course, if you have any suggestions, feel free to share. Or even submit a PR :slight_smile:

2 Likes

Yeah okay, I see what you’re saying…

So I gotta go back to using ctx args in my methods?
Man, I just can’t. I nearly quit JS altogether once I started having to do that. It’s so gross. Reading it a PITA and debugging it is even harder. I’ll be honest, ECMA6 was a godsend for that language…

Okay, I’ll concieve. It’s not ‘broken garbage’ - but I’m just not prepared to add that many hoops just to make a Class accessible in two resources…

Just to clarify though… there… there really isn’t a way to deeply make one resource use another as a framework… right?

Look at my code, tell me, be honest
You see what I’m attempting here, right?
Is it worth bothering with resource-based extensions?
Or is it just not worth the screwing around?
Be honest.

Because honestly my solution at this point is this: screw it. My race framework won’t be user contributable because reasons… it was a nice to have, and I’m probably lying to myself pretending anyone will help contribute to it anyway.

You’ll do fine with JS, I promise. Consider it this way. You have an API. Your API needs to be supported by any platform, even if that other platform isn’t JS. Your best bet is likely to serialize your data, could be JSON, or in the case of exports, msgpack. You cannot pass an entire class along through JSON, so you’ll have to do a few concessions to get your data across. That’s similar to what happens here, except msgpack is quite feature rich in that it does support functions.

Now you have a race resource. Say you want your map to create checkpoints. You could:
A: Let your race resource export a class that allows for the map to create checkpoints through functions, but that doesn’t work.
B: Pass along the coordinates of your checkpoints to a single exported function from your race resource. Pure data, no complexity, fully works.

It’s just a different way to deal with data. I hope that helps.

1 Like

Did you see the example in my post containing a .bind()-ed object? You can trivially generate these from the methods in your prototype (or ES6 class, because, again, they’re backed the same way) for use in external exports.

I’d have adapted your ‘test’ code to work this way, but since you only posted screenshots and not actual code blocks I had to only post a contrived example.

Again, simple answer: the ScRT serialization layer does not understand prototypes, or any sugar atop it (like ES6 classes, or other equivalents to it like C# classes or Lua metatables) - you’re going to need to provide plain data objects, such as an object with bound closures. It’s basically equivalent to JSON except some richer data types round-trip as well, and you can pass functions/closures by reference.

1 Like

Hmmm. I see what you’re saying regarding the API serialisation.
That’s interesting. I’d not considered users being able to support from other platforms (C#, lua etc)
That is honestly pretty high level coding expectation for potential contributors…

Regarding the approaches, solution A was honestly what I was attempting… (i.e. doing all the ‘hard coding’ for contributors and just let them call things like ‘race.checkpoint()’, ‘race.finish()’, listen for ‘onFinished’/‘onStarted’ etc…)

Regardng solution B… I was hoping for richer functionality. For instance, who said for instance a race is made of checkpoints? Maybe the race is the first person to kill a pedestrian (wild example, but it’s an example of the kinds of freedom I wanted to implement, that I am currently thinking is probably a bit too much to expect from contributors…)

Thanks for helping - I’m marking yours as the solution because despite deterministic_bubble mentioning the runtimes, yours was the comment that gave me the ‘aha’ moment. Regarding a PR - hey I’m just sitting here making dumb little race frameworks… you guys made a multilanguage framework which deep integrates with a third-party commerical game. I can barely get my head around Natives half of the time.

Maybe I’ll get to coding genius level like you guys one day, but not today. Thanks for the help

Hey - I did. But how does bind even function correctly if I can’t use this in child-resources?
If I can’t use this, what does bind attach to? That seems like the kind of ‘JS only’ abstraction which I should be avoiding right?

Regarding the adapted sample, I am not seeing that, where? (apologies)

Also, I see what you mean about needing to make objects more plain for the purpose of cross-platform usage (despite not using cross-platform contexts)

PS: Apologies for the ‘broken garbage’ sass. I actually have a lot of respect for what you guys do (it’s why I’m a Patreon donor), it was a combination of A) slamming my head on this problem for a week and not finding any documentation about it anywhere, B) being provocative is clickbait. Don’t believe me? I posted this same question without the sass yesterday, zero replies. I throw some shade? Two mods are here and I recieved prompt service. Just something to think about haha

No hard feelings I hope. I owe you both coffee IMO, so PM me your PayPal please gents.
Sorry for being a hassle (and lowkey socially engineering both of you lol) and thanks for the assistance

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.