Using the "Platform Info" script interface? [SOLVED]

0 favourites
  • 8 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Solved:

    Solution

    I updated C3 to the most recent version, and the Platform Info script interface now works as expected.

    Problem

    Without realizing it, I had been using an outdated version of C3, that predated the addition of the Platform Info script interface.

    Cause

    It was me all along! My C3 version was out of date as a result of my having left the same C3 Chrome tab open across several days. Without refreshing the tab, the version fell behind. I'd updated the C3 version in an adjacent tab at some point, and didn't realize later on that I then had multiple C3 tabs open that were running different versions.

    Original post:

    I'm trying to use the "Platform Info" script interface to determine if my project is running in a browser, or in NW.js.

    I can't seem to access or interact with the platform info script interface at all.

    I assume I'm doing something wrong, but I'm not sure what.

    What I tried

    I initially just wanted to test out printing the platform info's os value to verify that I could.

    1. I created a new empty project.

    2. In the layout editor I right clicked to insert a new object, and chose the "Platform Info" object.

    So, in the project bar, in the "Object types" folder, there is now an object named "PlatformInfo".

    3. In the "Scripts" folder, I double-clicked "main.js" to open it in a tab.

    4. In the main.js tab, in the function body for

    async function OnBeforeProjectStart(runtime){}

    (edit: Following R0J0Hound's reply below, I should also mention I've also done all of these tests from within the Tick() function as well, just to be sure that pretty much everything would get a chance to load.)

    I added the following five console.log statements to the top of the function body. Each line is just trying a different way to access the platform info data.

    console.log( runtime.platformInfo );
    console.log( runtime.objects.PlatformInfo.getFirstInstance() );
    console.log( runtime.objects.PlatformInfo.getFirstInstance().os );
    console.log( runtime.objects.PlatformInfo );
    console.log( runtime.objects.PlatformInfo.os );

    5. I ran the project in preview mode, in Chrome, and when checking the Chrome dev tools, I got the following output in the console:

    // undefined
    // [object Object]
    // undefined
    // [object Object]
    // undefined
    

    For clarity, here are the expressions and their output shown together:

    runtime.platformInfo 
    // undefined
    
    runtime.objects.PlatformInfo.getFirstInstance() 
    // [object Object]
    
    runtime.objects.PlatformInfo.getFirstInstance().os 
    // undefined
    
    runtime.objects.PlatformInfo 
    // [object Object]
    
    runtime.objects.PlatformInfo.os 
    // undefined

    Chrome dev tools

    To better understand what I was looking at, I used a break point in Chrome dev tools to pause execution of the preview. I set the break point at the end of the function body for

    async function OnBeforeProjectStart(runtime){}

    and with the execution paused, I inspected the "runtime" variable to see if I could find

    "runtime.platformInfo", but I wasn't able to.

    I was looking for this property on runtime because I thought the Platform Info script interface documentation was explaining in the second paragraph that it is the preferred way to interact with the platform info features.

    So, I'm pretty sure I'm just missing something pretty obvious, but I'm not sure where I've diverged from what I'm supposed to be doing.

  • It’s probably when you’re accessing it that’s the issue. I’m guessing that onbeforeprojectstart runs before the the plugin’s object type is initialized. I mean since it’s undefined when you call it that makes sense.

    Maybe try calling it from the runonstartup callback, or at least some point after things are defined.

    construct.net/en/make-games/manuals/construct-3/scripting/using-scripting/script-files

  • Hey R0J0hound, thanks for the reply.

    That's what I thought initially, but I've also tried evaluating those same lines in the middle of the tick() function, at which time I'd expect everything to be loaded.

    I basically just modified the tick function as follows,

    let tickCount = 0; 
    function Tick(runtime)
    {
    	console.log( 'Tick()' );
    	if ( tickCount === 0 ){
    		
    		console.log( runtime.platformInfo );
    		console.log( runtime.objects.PlatformInfo.getFirstInstance() );
    		console.log( runtime.objects.PlatformInfo.getFirstInstance().os );
    		console.log( runtime.objects.PlatformInfo );
    		console.log( runtime.objects.PlatformInfo.os );
    
    	} 
    	// Code to run every tick
    	tickCount ++;
    }

    I get the same output as in my original post above,

    // undefined
    // [object Object]
    // undefined
    // [object Object]
    // undefined
    

    (just copied this from the console after running the modified code above)

    and I still don't find any sign of the "runtime.platformInfo" property.

    Likewise, I've used Chrome dev tools to pause mid tick(), and on inspection of the objects returned by these two expressions,

    runtime.objects.PlatformInfo
    runtime.objects.PlatformInfo.getFirstInstance()
    

    I still don't find the property "os" owned by either object, or any of their prototypes.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ah, maybe the sdk2 update broke something with the scripting interface? I can’t test, I haven’t really used my computer in weeks.

    You could file a bug report. Or if you’re up for some deeper testing on your end before you report you could spot check some older releases in case it’s a regression. I’d check the versions before the sdk2 changes started.

    An unsupported way to do it is to bypass the script interface of the plugin and access the object instance directly. However that seems to be officially frowned on and they are trying to hide how to do that more.

    Worst case you could just access the values from the event sheet side. If you want to access it from JavaScript you could make an event sheet function to get the values and call it from js.

  • It works fine for me: https://www.dropbox.com/scl/fi/mgfxpf0ur4ihyxuzs397y/platforminfo-test.c3p?rlkey=bv0qahbldzncsx6ur2ez2th1v&dl=0

    As ever the fastest way to get help is to share a project file, so we can spot if you made a mistake somewhere.

  • Thanks Ashley,

    When I loaded your example, I got a prompt that the version I was using (r388.2 stable) was too old to open the file (made in r398.2 beta), so I enabled beta releases in the construct settings, and got r398.2 beta.

    In r398.2 beta, your cp3 previews correctly on my computer, and in Chrome dev tools, runtime.platformInfo now shows up in the debug view as expected.

    It looks like my C3 version was just behind enough to be missing the runtime.platformInfo script interface.

    As a test, I created a completely blank project in the newer r398.2 beta, and runtime.platformInfo is present as a property of runtime, even without adding the Platform Info object to the project bar. It shows up between mouse and ticksPerSecond.

    I then returned to an instance of the older r388.2 stable, and even when I add the Platform Info object to the project bar, runtime.platformInfo doesn't appear to be present as a property of runtime.

    So I think it may have just been a case of my version not being up to date enough.

  • The Platform Info script interface was added in r393.

  • Okay cool, that makes sense. I figured it had to be that I was just overlooking something.

    I'm pretty sure my version was behind because, through a combination of random chance and me just being kind of dumb across several days, I somehow never thought to reload the C3 Chrome tab I'd been using as a sandbox for testing script related stuff.

    I also just saw that there was even another Stable release, that came out a week or so back, that had the Platform Info update. And in hind sight, I'm pretty sure I actually had that version open in my main project Chrome tab, right beside the outdated sandbox tab. Ironically, I didn't test in the main project tab, because I hadn't gotten it working in the sandbox tab yet. With work split up across several days, I think I may have just been generally aware that I'd updated C3, but it didn't occur to me, later on, that it was just my main project tab, and not all tabs were running the same version.

    Anyway, thanks again Ashley and R0J0hound.

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