How do I initiate YouTube API script from another layout (Not the first one)

0 favourites
  • 5 posts
From the Asset Store
Here is the wonderful & amazing script for your website or fun with maths.
  • Basically what I ask on the subject. Is it even possible? I had try many conmbinations but nothing works.

    Also, is it posible to change the on click a button from the example API to a OnTouchedObject from a Touch?

    I´m kinda new on JS so I feel totaly lost on this.

    Thanks in adavance.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • export async function createVideo(iframeId) {

    Globals.ytPlayer[iframeId]["player"] = await YouTube.CreatePlayer(iframeId, {

    "onStateChange": e => { Globals.ytPlayer[iframeId]["state"] = e.data; },

    "onReady": e => {}

    });

    return true;

    };

    How do I use the state to change a global variable at an event sheet?

    I want something like this, but I cant find the way to mix both examples:

    const stateTextInst = runtime.objects.StateText.getFirstInstance();

    const ytPlayer = await YouTube.CreatePlayer("youtubeIframe", {

    "onStateChange": e =>

    {

    // When the player state changes, show it in the StateText object.

    stateTextInst.text = "" + e.data;

    }

    });

    Thanks for the links.

  • You can use

    	runtime.globalVars["Variable name"] = newValue
    

    or

    	runtime.globalVars.myVariable = newValue;
    

    see

    globalVars

    An object with a property for each global variable on an event sheet in the project. For example if the project has a global variable on an event sheet named Score, then runtime.globalVars.Score provides access to the global variable from script.

    https://www.construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/iruntime

    I'm not on pc now but I think something like this could go:

    	const ytPlayer = await YouTube.CreatePlayer("youtubeIframe", {
    		"onStateChange": e => {
    		runtime.globalVars.myVariable = "" + e.data;
    		}
    	});
    

    To simplify the use of "runtime" I prefer to insert a code of this type in 'main.js':

    	runOnStartup(async runtime => {	globalThis.g_runtime = runtime; });
    

    I use g_runtime instead of runtime

    	const ytPlayer = await YouTube.CreatePlayer("youtubeIframe", {
    		"onStateChange": e => {
    		g_runtime.globalVars.myVariable = "" + e.data;
    		}
    	});
    

    If you want "ytPlayer" to be global you have to use the one imported from Globals.js

    	Globals.ytPlayer = await YouTube.CreatePlayer("youtubeIframe", {
    		"onStateChange": e => {
    		g_runtime.globalVars.myVariable = "" + e.data;
    		}
    	});
    

    However, remember that the variables in the event sheet can only be strings, numbers or booleans

    I can't test this code right now

  • Thanks a lot, I finally made it works!

    I really apreciate you help.

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