Toddler
Can I use class ? I really want to use class...it's more OOP like.
Yes. You can use any valid JavaScript. I didn't use class because not all browsers support it. For example, some old Androids with webview 30- (or such).
The only thing that the plugin doesn't see is global variables with const and let. Shouldn't be a problem.
You can even call object functions like this: object1.function1.
You can even call object functions like this: object1.propery['one']['two'].foo['bar'].function1
Return value can be accessed via JS.StoredReturnValue.
Or you can compare returned value right away with conditions like Compare Function return value or Compare alias call
Since StoredReturnValue contains only string, use JSON to transfer your data.
That's not true. StoredReturnValue contains any Construct data type. It means it can be string, integer or float (boolean will be converted to 0/1). There's no need to use JSON. If you have to transfer data to Construct in JSON format, you probably have a wrong architecture. The only valid reason for retrieving JSON from javascript is when you want to put it to JSON plugin. But in this case you're actually passing some string. It just happens to be JSON.
Now the second question is, can you create a trigger from a javascript file ?
Sure. You can call Construct functions from javascript like this:
c2_callFunction(name, args_array)
I came up with this wrapper:
function ConstructCallback(name, args)
{
if( name == "" )
return;
args = (args === undefined) ? [] : args;
if( c2_callFunction !== undefined )
{
return c2_callFunction(name, args);
}
if( c3_callFunction !== undefined )
{
return c3_callFunction(name, args);
}
}
ConstructCallback triggers Construct function and even gets the return value from Construct that you can use in JS if you like.
Question 1: Why is "All scripts loaded" continuously triggering ?
Because it's not a trigger. It's a simple condition. Don't confuse one with another. Once scripts are loaded, All scripts loaded is always true. If you want to make a trigger out of it, add Trigger once (see the example on the promo page). I just didn't want to make a trigger condition since it is so easy to do it yourself.
Question 2: Can we have js.variable_value("enter variable name here") instead of js.AliasValue("enter the alias name here") ?
You sure can. Just use Execute JS code action or JSCodeValue expression or Compare JS code completion value condition. You can pass pure javascript to them. The problem is that it's only possible because they work by eval'ing those strings. And this is not a very good practice.
The alias system is there for a reason. When you do
js.InitAlias "Alias" with javascript "foo"
js.AliasValue("Alias.bar")
It actually translates to window["foo"]["bar"] on runtime. This is 100 times faster than eval'ing the string "foo.bar".
Getting back to your question. I could make actions, conditions and expressions that work exactly like Aliases but you don't have to init the alias first. But it would mean +10 ACEs the user would need to learn to use.
If you have lots of global variables (which is weird) that you want to access from Construct, just do
js.InitAlias "Global" with javascript "_window"
And add this to your javascript:
Now you can access all global variables with alias "Global": js.AliasValue("Global.foo")
Question 3: Now this is difficult I think...can you somehow create a way for the custom javascript to have its own triggers ?
What do you mean "its own triggers"? You mean make javascript code tick itself? Well you sure can use SetInterval function for that. But I can't even imagine why would you want your javascript code to have ticks. Javascript code is a model of your game objects. A model doesn't tick. A model stores data and makes actions (changes itself) when you ask it to. There should be only one ticking mechanism in the game and you already have it: it's event sheets. But you're still free to use any async things in your javascript.