@RemiHiyama
RemiHiyama wrote: ↑Tue May 21, 2019 3:48 pm
Is this supposed to work in GuideMe 3.8? When I try opening any of the examples in the 1.53 package in that GM version, it pops up two blank javascript error windows and then just sits on "Something went wrong".
This seems to be the issue we are discussing in the
GM thread
Remove the .state file in the data directory of you GM installation, and replace the line
Code: Select all
<Include file="Common/ScriptEngine.js" />
in the .xml files by the content of 'ScriptEngine.js'. For me, this works.
@bobhill
I will start with a small extension because it also serves as a meaningful example. It's a JavaScript function that is used to create pools of labels (e.g. to sessions) and randomly selects an existing target from the pool which has not been visited. A label is considered as visited if a flag with the same name is set.
I use it in this
demo and it may be useful for other teases to. It needs to be included in the 'GlobalJavascript' section. Feel free to append it to 'scriptEnginge.js'.
- Spoiler: show
Code: Select all
// -----------------------------------------------------------------------
// Randomly chooses an existing label from a range.
// This function is intended to implement pool of labels and randomly choosing one label that has not been visited.
// A label is considered as visited if a flag with the same name name is set.
// Thus, this function only considers labels that exists and have unset flag.
// If Labels exits but all flags are set, all flags will be cleared first.
// Example: 'findTarged(label,10,20)' finds label in the range of target10..target20
function findTarget(target,first,last) {
if ( first == last ) return target+first;
else if (first>last ) {
var i = last;
last = first;
first = i;
}
var flags = [];
var j = 0, k = 0;
for ( i=0; i<=last-first; i++ ) {
flags[i] = ( findScriptLabel(target+(first+i))<0 ) ? 0 : isSet(target+(first+i)) ? 1 : 2;
if ( flags[i] == 2 ) j++;
if ( flags[i] > 0 ) k++;
// jscriptLog("findTarget: " + target+(first+i) + ": " + flags[i] );
}
// no targets found
var result = target + Math.floor(Math.random()*(last-first)+first);
if ( k == 0 ) {
jscriptLog("Error: findTarget: No targets found, returning: " + result );
return result;
}
// return a unset target
if ( j > 0 ) {
j = Math.floor(Math.random()*j);
for ( i=0; i<=last-first; i++ ) {
if ( flags[i] == 2 ) {
if ( j==0 ) return target+(first+i);
j--;
}
}
}
// unset all valid targets and return a random one
for ( i=0; i<=last-first; i++ ) {
if ( flags[i]>0 ) comonFunctions.UnsetFlags(target+(first+i), guide.getFlags());
}
k = Math.floor(Math.random()*k);
for ( i=0; i<=last-first; i++ ) {
if ( flags[i] > 0 ) {
if ( k==0 ) return target+(first+i);
k--;
}
}
return result; // should not happen
}
In GSE scripts it can be used like this
Code: Select all
target = findTarget("floor_1-",1,20)
button Pool, <target>
bobhill wrote: ↑Tue May 21, 2019 2:46 pm
When you say two sets of syntax rules do you mean the GM syntax vs the ScriptEngine syntax?
The two lines above are written in two different syntaxes. Line 1 is JavaScript (like) syntax and consists in a assignment and a call to a function with a string and two integers as arguments. Line 2 is GSE syntax (a description language) and defines a button with two string arguments.
This is neither forbidden nor unusual (HTML/PHP, GideMe/Javascript, ...) but the domains have to be separated somehow, usually by embedding the programming language into the description language using syntax elements of the description language. (GSE tries to guess somehow what the programmer could mean. At least for me this is ambiguous.)
Because GSE already uses tags, this is how I would do that (example from above):
Code: Select all
<e target = findTarget("floor_1-",1,20)>
button Pool, <e target>
That would be equivalent to
Code: Select all
button Pool, <e findTarget("floor_1-",1,20)>
Whenever GSE finds an e tag, it is passed to the JavaScript interpreter.
bobhill wrote: ↑Tue May 21, 2019 2:46 pm
I'm sorry, I still not sure what you mean by expressions aren't evaluated as cited. I believe that expressions are evaluated correctly, this, for example, works fine:
Code: Select all
count = 0
no-02.jpg {
Count is <count>
Button Continue, count=count+1
}
no-03.jpg {
Count is <count>
Button Continue
}
In this example GSE correctly guesses which syntax is meant. In this example it fails:
Code: Select all
globalButton Test, findTarget("floor_1-",1,20)
Error message is "onButtonClick: No Label or Page named findTarget("floor_1-",1,20) was found."
Workaround would be
Code: Select all
globalButton Test, test
...
test:
label=findTarget("floor_1-",1,20)
goto <label>
What I really need is a button that executes a JS function without interrupting the page actions (interrupting the sound, because I'm interested in EStim teases)