Why use JavaScript/TypeScript in C3?

Not favoritedFavorited Favorited 0 favourites
  • 14 posts
From the Asset Store
[C2] [C3] Support C3 build service and Android 15+
  • I’m getting back to Construct after a while and noticed that now we even have the option to code with JS/TS. At first, I thought it was interesting, but then I started wondering: why? Is it just for people who prefer writing code, or are there other benefits? Are there things that work better or perform better through code? Are there things you can do with code that aren’t possible with events? Thanks!

  • Are there things you can do with code that aren’t possible with events?

    Yeah there's some. I find that working with any kind of arrays and data objects is much easier in javascript. There's also how picking works in Construct, that you e.g. cannot loop over a combination of two object types, which you can easily do in js. Subclassing is imo one of the best features which does not have a real equivalent in events. You can get sort of close with custom actions, but not quite.

  • Could some of the things that are better in scripting be used as inspiration to improve Event coding at all?

    As someone that uses Construct because of the Event Sheets and because I don't like scripting, I'd rather see the event system improved to the best it can be.

  • It’s mainly useful to interact with other js/ts libraries if you don’t want to deal with making a plugin. Although plugins/behaviors are simpler to use for end users and provide some deeper hooks into the engine.

    It’s also a nice advertisement point for Construct and attracts some users that don’t like visual coding or look at it as a way to dabble in coding along with events.

    JavaScript/typescript code can perform better than events for more algorithmic stuff but it really depends on what you’re doing.

    There are pros and cons of course. Cons off hand are you have to deal with more boiler plate code to interact with the api, debugging is more involved, and you’re kinda thrown out there dealing with how those languages work. Construct can only help so much with that.

    Overall events and typed code have their own workflows and it mostly comes down to personal preference. Pick your poison. They all have their annoyances when trying to implement some logic.

  • Rojo's reply made me realize another thing: with typescript code, you can have typed code, wich is not possible with events. That's interesting.

    Awesome, thank you very much guys.

  • Haven't construct variables been typed since the beginning of time?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Haven't construct variables been typed since the beginning of time?

    Maybe simple types, like number, string, etc, but what if we need a variable that can receive either "YES" or "NO"? we can achieve this with TS, but not with events.

    ps: yes/no could be fixed with a boolean var, but we could need something like: "LOW", "MEDIUM", "HIGH".

  • we could need something like: "LOW", "MEDIUM", "HIGH"

    So just make a string variable that you give these 3 values? Or use integers? I don't think you need a specific variable type for that at all. Same for yes/no.

  • > we could need something like: "LOW", "MEDIUM", "HIGH"

    So just make a string variable that you give these 3 values? Or use integers? I don't think you need a specific variable type for that at all. Same for yes/no.

    Imagine that you have a variable called size, wich can receive "LOW", "MEDIUM" or "HIGH", but by mistake at some point you assign "HIG" for it, or even another value that you tought you could, like "HUGE". It won't work and maybe the problem will not be obvious for you when it happen.

    Typescript can fix that, cause you can type this variable like this:

    let size: "LOW" | "MEDIUM" | "HIGH"

    this way, if you assign anything else to this variable, your project won't even run and the problem will be shown.

  • Ah I suppose that's a fair point.

    I guess that is another answer to the original question of "are there benefits?". So yes, another benefit of using typescript is that you can use the whole typing thingamajig. I'm more of a "no typing just yolo" enjoyer.

    I'd not expect that kind of typing to be added to variables/instance variables any time soon.

  • I often need to work with arrays and JSON, using them for data structure conversion, more complex filtering, sorting, and more. However, the Eventsheet ACE functionality is limited and operations can be complex. Using JavaScript can save a lot of time.

    So, I use a function that takes Array.AsJSON and converts it into a true array using JavaScript. Then, I process the data internally, convert it back to a string, set the return value, and then make Array Load.

    const array = JSON.parse(localVars.arrayJson);
    ...
    runtime.setReturnValue(JSON.stringify(array));
    

    This part of the JavaScript is perfect for AI to help. Simply tell it the data structure you want and the desired result, and it will handle it perfectly.

  • I often need to work with arrays and JSON, using them for data structure conversion, more complex filtering, sorting, and more. However, the Eventsheet ACE functionality is limited and operations can be complex. Using JavaScript can save a lot of time.

    So, I use a function that takes Array.AsJSON and converts it into a true array using JavaScript. Then, I process the data internally, convert it back to a string, set the return value, and then make Array Load.

    > const array = JSON.parse(localVars.arrayJson);
    ...
    runtime.setReturnValue(JSON.stringify(array));
    

    This part of the JavaScript is perfect for AI to help. Simply tell it the data structure you want and the desired result, and it will handle it perfectly.

    Hmmmm that's an interesting point. Can we always use js/ts to handle array and object logic instead of events (wich are kind of limited and complex for this)? Like, if i need to create an object to store some weapons data, can i do this with ts and then use this object in events? It would be an awesome mix.

  • You'll have to do some shuffling with converting the Construct array to regular array and back as XHXIAIEIN mentioned, but it's absolutely possible and indeed convenient. I also mainly started with that, but eventually I got tired of shuffling back and forth and took the plunge.

  • Enums are just numbers under the hood but I can see the value of ensuring that only a certain set of values are used. You could use a function to set the values with events and do the checks there if you really wanted to.

    As for using array.asJson, if you look at the manual you can access the array object directly from JavaScript instead of having to pass the json back and forth.

    Personally the hoops needed to jump between programming languages is annoying enough that I try to avoid it completely. Events only in construct and if I want to use JavaScript I just don’t use construct at all.

    The exception is for performance. Same algorithm in events vs js can be x10 faster in js in some cases. So I’ve done that sometimes. I personally think there’s no reason events can’t be made to perform nigh as fast as JavaScript with some internals reworking. But I’d rather not worry about something I have no control over.

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)