Wtf happened with subclasses?

Not favoritedFavorited Favorited 0 favourites
  • 9 posts
From the Asset Store
Shark.io
$9.99 USD
Shark.io in the sea full of killer sharks, Sunday of life is the game. Who knows what is happening under the sea?
  • Ok so my subclasses was working great, now i am getting error:

    runtime.js:1 [C3 runtime] Error in runOnStartup function: TypeError: expected function at C3.RequireFunction (typeChecks.js:1:2563) at self.ISpriteObjectType.setInstanceClass (IObjectClass.js:1:1269) at main.js:9:22 at C3.Runtime._RunOnStartupFunction (runtime.js:1:12581) at C3.Runtime.Init (runtime.js:1:12030) at async Q._InitDOM (domSide.js:16:12442) at async Q._Init (domSide.js:16:7621)

    But this was working great before, i tried with a empty project, and the same happened?

    Here is main.js:

    import * as plr from "./plr.js";
    // Import any other script files here, e.g.:
    // import * as myModule from "./mymodule.js";
    
    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	runtime.objects.plr.setInstanceClass(plr);
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    });
    
    async function OnBeforeProjectStart(runtime)
    {
    	// Code to run just before 'On start of layout' on
    	// the first layout. Loading has finished and initial
    	// instances are created and available to use here.
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    function Tick(runtime)
    {
    	// Code to run every tick
    }
    

    Here is my subclass:

    class plr extends globalThis.InstanceType.plr
    {
    
    constructor(){
    super();
    console.log("toatewg");
    }
    }

    I have now tried multiple fresh projects, i am unable to subclass, i have no idea why.

    [b

    ] P.S: Whoever thought "undo" should apply to all my script files in a project and not the CURRENT script file im working on, you and me we have a problem !

  • In your subclass it doesn't show that you are exporting the class.

  • In your subclass it doesn't show that you are exporting the class.

    Hey, yeah i tried that, usually dont have to export subclasses, i might be wrong tho.

    I tried adding export, sadly still same error 😭.

  • You need to export the class and

    runtime.objects.plr.setInstanceClass(plr.plr);
    

    the first plr is the module, the second plr is the class.

  • You need to export the class and

    > runtime.objects.plr.setInstanceClass(plr.plr);
    

    the first plr is the module, the second plr is the class.

    Hello, ok thank you.

    So basically, i noticed why it was working for me before.

    If i set instance class of a instance in main, you both are right i have to export the subclass and then use plr.plr

    However, i noticed, if i add the runOnStartUp in my subclass file, i dont have to export the class at all, and it works aswell.

    Look at the Player class here:

    class PlayerInstance extends globalThis.InstanceType.Player
    {
    
    constructor()
    {
    super();
    console.log("Created!");
    }
    
    }
    
    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	
    	runtime.objects.Player.setInstanceClass(PlayerInstance);
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    });
    
    async function OnBeforeProjectStart(runtime)
    {
    	// Code to run just before 'On start of layout' on
    	// the first layout. Loading has finished and initial
    	// instances are created and available to use here.
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    function Tick(runtime)
    {
    	// Code to run every tick
    }
    
    

    Then i just import the class in main, without using export at all.

    HOWEVER, if i setInstanceClass in main, i will have to do PlayerInstance.PlayerInstance like you mentioned, and i have to export the subclass.

    Thanks for clearing this out guys, i was confused after a few hours of work yesterday.

    Have a great weekend!

  • Well yeah, like that you have direct access to the class without having to import, but I'd say it's kind of dirty to program it like that hehe. At least to me it makes sense to have it organized that way. Just as an example:

    import * as Player from "./player.js";
    

    Now I have the Player Module, which can include more than just the player itself. It could also contain, say, the camera and the player weapon, which kind of make sense to be part of the player Module. Like so:

    runtime.objects.player.setInstanceClass(Player.Player);
    runtime.objects.camera.setInstanceClass(Player.Camera);
    runtime.objects.sword.setInstanceClass(Player.Sword);
    

    So everything regarding the player will be inside one separate file. And the setInstanceClass can happen only in one file in one place. So if something goes wrong or I have to add another instance class, I know exactly where to look.

    I write plenty of dirty code btw. but I still try to keep it minimal and do regular cleanups. There's nothing wrong with dirty code to a degree. Be more like Balatro-dev and less like Yandere-dev. Both have "dirty" code but Balatro is printing infinite money and yandere simulator still isn't finished (it's been 11 years)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well yeah, like that you have direct access to the class without having to import, but I'd say it's kind of dirty to program it like that hehe. At least to me it makes sense to have it organized that way. Just as an example:

    > import * as Player from "./player.js";
    

    Now I have the Player Module, which can include more than just the player itself. It could also contain, say, the camera and the player weapon, which kind of make sense to be part of the player Module. Like so:

    > runtime.objects.player.setInstanceClass(Player.Player);
    runtime.objects.camera.setInstanceClass(Player.Camera);
    runtime.objects.sword.setInstanceClass(Player.Sword);
    

    So everything regarding the player will be inside one separate file. And the setInstanceClass can happen only in one file in one place. So if something goes wrong or I have to add another instance class, I know exactly where to look.

    I write plenty of dirty code btw. but I still try to keep it minimal and do regular cleanups. There's nothing wrong with dirty code to a degree. Be more like Balatro-dev and less like Yandere-dev. Both have "dirty" code but Balatro is printing infinite money and yandere simulator still isn't finished (it's been 11 years)

    Yeah now that you said it, i feel like your approach is better in the long run for sure. I to like organized code :p im just unfamiliar with how JS organized code looks like, since i am coming from c# and backends like php where i am used to strict types and not loose types like in js.

    Thanks !

  • i am coming from c# and backends like php where i am used to strict types and not loose types like in js.

    You might like to try using TypeScript which gives you strict type checking.

  • > i am coming from c# and backends like php where i am used to strict types and not loose types like in js.

    You might like to try using TypeScript which gives you strict type checking.

    For some reason i never checked it out xD After some research on it i am now using it, its so nice i love the fact that i can use strict types in js and i was able to connect my vs code to construct 3 by following the guide and its just so great, thanks for the tip!

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