How to have a global variable in c3 runtime?

0 favourites
  • 5 posts
From the Asset Store
[C2] [C3] Support C3 build service and Android 14
  • I have this sample c2runtime.js

    Important code has TESTVAR.

    How do I migrate to c3 runtime?

    Thanks

    // ECMAScript 5 strict mode
    "use strict";
    
    assert2(cr, "cr namespace not created");
    assert2(cr.plugins_, "cr.plugins_ not created");
    
    /////////////////////////////////////
    // Plugin class
    cr.plugins_.MyCompany_SingleGlobal = function(runtime)
    {
    	this.runtime = runtime;
    };
    
    (function ()
    {
    var TESTVAR=5;
    	var pluginProto = cr.plugins_.MyCompany_SingleGlobal.prototype;
    		
    	/////////////////////////////////////
    	// Object type class
    	pluginProto.Type = function(plugin)
    	{
    		this.plugin = plugin;
    		this.runtime = plugin.runtime;
    	};
    
    	var typeProto = pluginProto.Type.prototype;
    
    	typeProto.onCreate = function()
    	{
    	};
    
    	/////////////////////////////////////
    	// Instance class
    	pluginProto.Instance = function(type)
    	{
    		this.type = type;
    		this.runtime = type.runtime;
    		
    		// Initialise object properties
    		this.testProperty = 0;
    	};
    	
    	var instanceProto = pluginProto.Instance.prototype;
    	
    	instanceProto.onCreate = function()
    	{
    		// Read properties set in C3
    		this.testProperty = this.properties[0];
    	};
    	
    	instanceProto.saveToJSON = function ()
    	{
    		return {};
    	};
    	
    	instanceProto.loadFromJSON = function (o)
    	{
    	};
    	
    	/**BEGIN-PREVIEWONLY**/
    	instanceProto.getDebuggerValues = function (propsections)
    	{
    	};
    	/**END-PREVIEWONLY**/
    
    	//////////////////////////////////////
    	// Conditions
    	function Cnds() {};
    	
    	Cnds.prototype.IsLargeNumber = function (number)
    	{
    		//return number > 100;
    		return TESTVAR;
    	};
    	
    	pluginProto.cnds = new Cnds();
    
    	//////////////////////////////////////
    	// Actions
    	function Acts() {};
    
    	Acts.prototype.Alert = function ()
    	{
    		alert("Test property = " + this.testProperty);
    	};
    	
    	pluginProto.acts = new Acts();
    
    	//////////////////////////////////////
    	// Expressions
    	function Exps() {};
    	
    	Exps.prototype.Double = function (ret, number)
    	{
    		ret.set_float(number * 2);
    	};
    	
    	pluginProto.exps = new Exps();
    
    }());
  • Just move it somewhere else that everything that needs to access it can find it. For example if you only need to use it in actions, move it to the scope that encloses actions. Otherwise if several things need to use it, put it as a static on the plugin, a property of the instance, etc.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • do you mean like this in instance.js?:

    "use strict";
    
    {
    	C3.Plugins.MyCompany_SingleGlobal.Instance = class SingleGlobalInstance extends C3.SDKInstanceBase
    	{
    	var TESTVAR=5;
    		constructor(inst, properties)
    		{
    			super(inst);
    			
    			// Initialise object properties
    			this._testProperty = 0;
    			
    			if (properties)		// note properties may be null in some cases
    			{
    				this._testProperty = properties[0];
    			}
    		}
    		
    		Release()
    		{
    			super.Release();
    		}
    		
    		SaveToJson()
    		{
    			return {
    				// data to be saved for savegames
    			};
    		}
    		
    		LoadFromJson(o)
    		{
    			// load state for savegames
    		}
    	};
    }
  • No, that is not valid JavaScript syntax. I'm afraid I can't help you learn JavaScript, try starting here: https://developer.mozilla.org/bm/docs/Web/JavaScript

  • I moved TESTVAR=5 to line 3 and it works. Thanks

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