How do I get a return value back into my event sheet (Global Vars)

0 favourites
  • 6 posts
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • After getting some data (xmlHttp.responseText) from the web I now need the result to be put back into my event sheet.

    My global variable is called 'returned_data' (its a string)

    I tried runtime.globalVars.returned_data = xmlHttp.responseText but my global variable will not change despite the fact that the alert with the same data displays perfectly, as does the console log with the returned data. I need to get the returned data back to the event sheet for further manipulation.

    Here is the JS script

    function apiPostRequest(request, body) {
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "POST", baseApiURL + request, false );
    	xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    	xmlHttp.setRequestHeader('X-Auth-token', token);
    	xmlHttp.send(body);
    	console.log("Resp: "+xmlHttp.responseText);//PRINTS RESULT PERFECTLY
    	alert ("Resp :"+xmlHttp.responseText);//PRINTS RESULT PERFECTLY
    	
    	runtime.globalVars.returned_data = xmlHttp.responseText;//DOES NOTHING - Global ver is not changed
    	
    	return xmlHttp.responseText;
    }
    
  • Is "runtime" defined in the context where the script is ?

  • Its got this in the same script. Is that what you mean? ..or do I need something else?

    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    	
    });
    
  • Just to explain further in the most simple example:

    I call this function from a JS block..

    var resp;//this is a variable which gets adjusted with other script code
    
    function GetResult(runtime)
    {
    	alert("Looky here: "+resp);//This prints fine as expected! The resp is full of lovely data.
    	
    	runtime.globalVars.returned_data = resp;//The global value is not set in the runtime according to the debugger
    }
    

    and I get the error: TypeError: Cannot read property 'globalVars' of undefined

  • SOLVED.

    Instead of calling

    GetResult();
    

    I need to call..

    GetResult(runtime);
    
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • And indeed runtime was undefined ! You've got to be weary, that variable is not in the global scope, so you must pass it along like you discovered.

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