Sami424's Forum Posts

  • 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!

  • 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 😭.

  • Alright thx, feature request it is. The workaround is ok, but I always set out to do a game and make it "javascript only" just to end up back in the event sheet for random reasons. Either be it workarounds, missing APIs (or intentionally left out) or simply convenience with some things. Not a bad thing per se but not exactly what I plan, haha.

    I feel you, however after awhile you will notice that some stuff is just easier to do in the event sheet, code is still js even if you use the event sheet, i usually say if someone has done it, why redo it 😉 that said i had the same issue with some stuff like pin etc, where i had to create it myself, but then i said screw it and just used the event sheet, works fine for simple things like on start of layout etc.

  • 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 !

  • Why dont you want to just import the files in c3 by right clicking the sounds folder ? Just curious

  • You want to upload a file from the code ? Am i correct ?

  • If i am correct, your trying to make it like a candy crush feature almost, that if 3 same color blocks are on eachother they will get removed ?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The code in your initial post appears to be correct. Maybe you didn't set the right script purpose properties. The easiest way to get help is to share a project file, so others can see exactly what you've done - otherwise all we can do is guess.

    Basically the code above is all i did to test the import/export. I solved it as the problem was on my end, i forgot to export the class properly. Thanks for your reply

  • See the guide in the manual on using an external editor.

    Thanks for your answer, i read it and i kind off did that but my intellisense etc is not catching up the c3 classes etc. I might done something wrong, i will try again. Thanks

  • You can use APIs online, such as firebase etc.

    Or make your own using php and storing data to database and use Ajax calls to save to it from c3

  • I have been looking around, i found a very old plugin for VS code that doesnt seem to work. Any way for me to work in vs code for my construct 3 scripts? I work with VS Code so this would make stuff much easier.

  • Alright so i solved it by doing

    // I import the class named myClass from my js file, the class has an export tag. 
    import { myClass } from "./myclass.js" ;
    
    // I instantiate the class
    let customClass = new myClass();
    // I can now use the functions declared inside myClass
    customClass.runMyFunction();
    
    

    This is working, i dont know if i am doing it correct, but this way its working. I am getting more familiar with the import/export in construct.

  • hello everyone !

    how u doing ?

    Is it possible to hide or show a layer from a specific layout or from the current layout?

    You can get specific layers easily from a layout:

    // get a layer from the current layout
    let myLayer = runtime.layout.getLayer(1) // 1 is the index of my layer, you can also just put the name of the layer as parameter.
    

    after getting the layer you can set visibility:

    myLayer.isVisible = false;// Will hide the layer

    You can also use the

    runtime.getLayout("myLayout");
    

    to get a specific layout.

  • I've been trying to create a system for reading and writing variables through expressions (i.e. a function to evaluate an expression like "Var1 == 0 & Var2 = 1" and similar functions to change/add/subtract variables). For a while, I've been assuming the best way to do this is to declare the variables in the Event Sheet as normal, then interact with them through code but it occurred to me after another readthrough of the (brilliant) scripting documentation that I could just declare the variables in script (let Var1 = 1, etc.) and avoid the awkwardness of Event Sheet/Javascript cross-communication altogether.

    I'm still not completely confident with Javascript and would still keep the majority of the project in visual code. With that in mind, are there any particular pitfalls or dangers to handling most of my variables in javascript?

    As Ashley mentioned it depends on what your doing, i actually like working with the variables in the event sheet.

    Declaring a variable like

    Let myVar = 5; 

    is just declaring the property in JavaScript .

    Is not the same as declaring s global,local or instance variable ( i think )

  • Hi Ashley

    Thank you for your reply.

    Ok for arguments sake I have created a new project and on the event sheet I've added one script as follows

    System > On start of layout >

    let audio = new Audio("https://streaming.galaxywebsolutions.com/stream/moorland");

    audio.play();

    When you run the project it starts and plays the audio from the script.

    However as it is ran direct from the script I can not tick the play in background on the audio object as the audio object as not been added to the project, therefore all works perfectly apart from the background issue when the app sleeps or loses focus.

    I can not call (or at least I don't know how to call) the stream from the audio object as it is continuous and a live mpeg stream which can not be changed to a different format.

    How would I get my project to steam this URL via the audio object so that I can then check the play in background box, so that the stream continues to play in background mode on both Android & iPhone?

    Many thanks

    Adrian

    I dont think you can make it play in the background on mobile phones using the default audio object. The option is for browsers that support it.

    I might be wrong tho.