Javascript getting started: How do I manipulate an object instance directly?

0 favourites
  • 7 posts
From the Asset Store
112 High-Quality destruction sounds for videogames
  • Hi,

    I am trying to get my first Construct JS scripting off the ground.

    Using the default built-in script, I can easily define global vars, incrementally update every tick, and also manipulate global vars created by the visual code.

    however, I tried to get an object instance and change its text, but it breaks the runtime (game freezes when I press play):

    // Put any global functions etc. here
    var touches = 0;
    var tickcount = 0;
    var jsTxto;
    
    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    
    });
    
    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.
    	
    	jsTxto = runtime.objects.jsTxt.getFirstInstance();
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    function Tick(runtime)
    {
    	// Code to run every tick
    	tickcount += 1;
    	runtime.globalVars.globaltick = tickcount;
    	jsTxto.text = tickcount; // <- this breaks it
    	//console.log("tick: " + tickcount);
    }
    
    
    function myFubar()
    {
    	console.log("fubar");
    }

    I am most certainly doing something wrong, however I can't figure out what.

    Can anyone help? Thanks!

  • The very first thing to do is: press F12 and look for an error message in the browser console.

  • For the record:

    I have an Object which is a textbox, that is called jsTxt in the objects panel.

    Layout is designed with exactly one of this Object as part of the layout.

    I have used the call javascript action and the myFubar() function is triggered correctly, posts "fubar" to the console. This errors follows immediately:

    Uncaught (in promise) TypeError: this._bbstr.includes is not a function at new C3.BBString (bbstring.js:1) at C3.Gfx.WebGLText._MaybeWrapText (text.js:1) at C3.Gfx.WebGLText._DoUpdate (text.js:1) at C3.Gfx.WebGLText._MaybeUpdate (text.js:1) at C3.Gfx.WebGLText.GetTexture (text.js:1) at C3.Plugins.Text.Instance.Draw (instance.js:1) at C3.Instance.Draw (instance.js:1) at C3.Layer._DrawInstance (layer.js:1) at C3.Layer._DrawInstances (layer.js:1) at C3.Layer.Draw (layer.js:1)

  • It looks like it might be a bug. Can you post your project?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I am posting here per your request, also because the project is very early.

    But I would prefer to send the link as a PM if possible, but clicking on your name I couldn't find a way to send private messages.

    Paying customers (me) have a way to report bugs?

    Direct link:

    1drv.ms/u/s!ArcXERob0wJkg7AbFnjUMcG4EsZxqg

  • Well, the problem is you're assigning a number where it expects a string, which is breaking it. Make sure you convert the tickcount to a string and it should work. It looks like it's not reporting the error correctly though.

    You can file bugs here: https://github.com/Scirra/Construct-3-bugs - it would be useful to post this there so it's not lost on the forum.

  • Oh, I see.

    Being a programmer for some time, I should have thought of that - it just didn't occur to me, as I am used to implicit type coercion.

    Is it because of web workers or strict mode that it broke?

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