wheresmything wrote: ↑Tue Jul 07, 2020 9:57 pm
bobhill & philo
Thanks for your efforts, but after going over this again and again, I still can't get it working. Here's the script I've got so far. The app seems to load the arrays ok, and then on the start page, it does create all the appropriate buttons. However, no matter what I try, the buttons don't seem to pass the index value to the setModel function or (after that) overwrite the value for vSelected. Therefore, when I get to the "tease" page, the message I get is always "vSelected is 99" regardless of the button pressed. Why would this be? Thanks.
Incidentally, I did try changing my for loop to the do loop you suggested, but whenever I try that, the app crashes, so I stuck with the for until I can figure the other stuff out.
I've included the full code below (tested in Guideme 3.6).
You're running into trouble because you're using global variables that get re-initialized on every page. The same is actually happening with your arrays, which is not very efficient. They could be initialized once in the pageLoad function of the start page and saved as scriptVars. I've demonstrated this with arrayAllow in my code.
The downside is that every page that needs the array has to use something like
Code: Select all
var Models = scriptVars.get("Models");
to get a reference to the array stored in the scriptVars. It's a little less convenient that way, but just so you know, the way you have it coded all those global variables get recreated and re-initialized every time you go to another page that uses javascript, whether those pages use the variables or not. If you make changes to the global variable in one page, your changes disappear on the next page. scriptVars() is how you work around that in GuideMe.
Anyway, the following code does what you want and should help get you going.
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<Tease>
<Title>At thier whim</Title>
<Author id="1">
<Name>"1"</Name>
</Author>
<MediaDirectory>At thier whim</MediaDirectory>
<Settings>
<AutoSetPageWhenSeen>false</AutoSetPageWhenSeen>
<ForceStartPage>true</ForceStartPage>
</Settings>
<GlobalJavascript>
<![CDATA[
//Difficulty settings: Very Easy = 0, Easy = 1, Medium = 2, Hard = 3, Very Hard = 4, Insane = 5
var arrayModel = [
{ Name: "Abha", Difficulty: 2, Path: "Abha" },
{ Name: "Adele", Difficulty: 3, Path: "Adele" },
{ Name: "Agatha", Difficulty: 2, Path: "Agatha" },
{ Name: "Alessa", Difficulty: 3, Path: "Alessa" },
{ Name: "Alex", Difficulty: 2, Path: "Alex" },
{ Name: "Jenja", Difficulty: 2, Path: "Jenja" },
{ Name: "Marie", Difficulty: 1, Path: "Marie\\Marie-1" },
{ Name: "Marie", Difficulty: 4, Path: "Marie\\Marie-2" },
];
//Time is in seconds, chances must add to 100
var arrayDifficulty = [
{ Level: "Very Easy", DelayMin: 60, DelayMax: 180, AllowChance: 15, RuinChance: 20, DenyChance: 65},
{ Level: "Easy", DelayMin: 120, DelayMax: 300, AllowChance: 12, RuinChance: 15, DenyChance: 73},
{ Level: "Medium", DelayMin: 180, DelayMax: 360, AllowChance: 9, RuinChance: 10, DenyChance: 81},
{ Level: "Hard", DelayMin: 300, DelayMax: 480, AllowChance: 5, RuinChance: 7, DenyChance: 88},
{ Level: "Very Hard", DelayMin: 420, DelayMax: 600, AllowChance: 3, RuinChance: 4, DenyChance: 93},
{ Level: "Insane", DelayMin: 600, DelayMax: 900, AllowChance: 1, RuinChance: 2, DenyChance: 97},
];
var arrayRuin = [
"Ruin 1",
"Ruin 2",
"Ruin 3",
];
var arrayDeny = [
"Deny 1",
"Deny 2",
"Deny 3",
];
]]>
</GlobalJavascript>
<Pages>
<Page id="start">
<Text>Who'd you like to see?</Text>
<Image id="test.jpg" />
<javascript>
<![CDATA[
function pageLoad() {
var i = 0;
var vSelected = parseInt(scriptVars.get("vSelected"),10);
var arrayAllow = scriptVars.get("arrayAllow");
jscriptLog("vSelected = " + vSelected);
if ( isNaN(vSelected) || vSelected == undefined ) {
vSelected = 1;
scriptVars.put("vSelected", vSelected);
jscriptLog("vSelected initialized to " + vSelected);
}
if ( arrayAllow == undefined ) {
jscriptLog("Initializing arrayAllow");
arrayAllow = [
"Allow 1",
"Allow 2",
"Allow 3",
];
scriptVars.put("arrayAllow",arrayAllow);
}
for (i=0; i < arrayModel.length; i++) {
var vName = arrayModel[i].Name;
overRide.addButton("Tease", vName, "", "", "setModel(" + i +")", "", "");
}
overRide.setHtml("You are currently using " + arrayModel[vSelected].Name);
}
function setModel(i)
{
scriptVars.put("vSelected", i);
}
]]>
</javascript>
</Page>
<Page id="Tease">
<Text>Something went wrong</Text>
<javascript>
<![CDATA[
function pageLoad() {
var i = parseInt(scriptVars.get("vSelected"),10);
overRide.setHtml("You are currently using " + arrayModel[i].Name);
}
function setResult(i)
{
scriptVars.put("vResult", i);
}
]]>
</javascript>
<Button target="Result" onclick="setResult(0)">Result 1</Button>
<Button target="Result" onclick="setResult(1)">Result 2</Button>
<Button target="Result" onclick="setResult(2)">Result 3</Button>
</Page>
<Page id="Result">
<Text>Something went wrong</Text>
<javascript>
<![CDATA[
function pageLoad() {
var i = parseInt(scriptVars.get("vResult"),10);
var arrayAllow = scriptVars.get("arrayAllow");
jscriptLog("Got result = " + i + " (" + arrayAllow[i] + ")");
overRide.setHtml("You got result " + arrayAllow[i]);
}
]]>
</javascript>
<Button target="start">Restart</Button>
</Page>
</Pages>
</Tease>
In the pageload function of the start page you'll see
Code: Select all
var vSelected = parseInt(scriptVars.get("vSelected"),10);
which "gets" the scriptVar named vSelected, parsing it as an integer (the default is as a string which usually yields incorrect results).
If vSelected is not a number or not defined it is initialized to the value 1 and the new value is saved for other pages.
You'll also see some calls to jscriptLog() which logs messages to the jScript log (in the logs folder under your GuideMe folder). It can be enormously helpful tracking down bugs.
You'll see I also retrieve the arrayAllow variable and initialize it if it's not defined.
In the Tease page I then retrieve the current value of vSelected and use it to display the name of the selected model.
I've also added a function and buttons to select a result, then in the Result page I "get" arrayAllow and display the result you got.
With this code you can even close the tease and come back later, and whatever model you last selected will remain selected until you choose a different one. And the tease will "remember" the result you got last time too.
A couple other things:
Buttons support a sortOrder option so you can make them display in the order you want. In xml use
Code: Select all
<Button target="somePage" sortOrder="1">Button text</Button>
The order number must be in quotes.
In javascript there is another form of overRide.addButton that supports more options.
Code: Select all
overRide.addButton( target, buttonText, set, unSet, jScript, image, hotKey, sortOrder, disabled, id)
In this function all arguments are strings EXCEPT the disabled argument which must be a boolean (true or false). That's tripped me up many times.
Also, when working with scriptVars as demonstrated in my code I've gotten in the habit of always using the same name for the variable in my javascript and for the name of the scriptVar as in
Code: Select all
var vSelected = scriptVars.get("vSelected");
instead of
Code: Select all
var i = scriptVars.get("vSelected");
It keeps it clear what you're using and again, not doing it has tripped me up so many times I just got in the habit of being verbose in my code.
Hope that helps,
PG