Tips (include, service variables, and JS serialize)

Hello,
I am a new user of Squiffy. I started writing a story and become familiar with the basic function of this software.
I have downloaded it on Linux and I have found the bug of copy-paste, but also some problem when I delete parts of the text using backspace, so I moved my script on the web. (I have read this bug have been already reported).

The program at the moment is going well enough but I have some questions:
My knowledge of JS is basic but I scripted in Flash actionscript 1.0/2.0/3.0 in the past so I have some reminiscence to the similarity of language. I wrote a function that detects a skill of a character and evaluates it by adding random elements.

function abilityCheck(mn,mx,abilityname,spc,trg){
  var ability = squiffy.get(abilityname);
  var min = Math.ceil(mn);
  var max = Math.floor(mx);
  var dice = Math.floor(Math.random() * (max - min + 1)) + min;
  var bonus = (Math.ceil(ability/5)*2)+spc;
  var skillcheck = (dice+bonus)>=trg?"success":"failure";
  if(skillcheck=="success"){
 .......
  }
  alert("Target="+trg+"\nRoll 2d6="+dice+"\nBonus ability="+bonus+"\n\nTotal="+(dice+bonus)+"\n"+skillcheck);
  return  dice+bonus-trg;
}

At the moment the function is not very functional because I should rewrite the same code in every part of the program where you want to call it. Is there a way to include this function in an external file with something like @include?

Second question. I have some "service variables" that I need to make checks on the script.
For example:

var spc=0;
squiffy.set( "skillresult", abilityCheck(2,12,"characterdexterity",spc,8) );

{if skillresult>=0:bla bla bla bla}

I would like to reset "skillresult" at the end of the block, so that if I read it on the next passage/section that variable is null or 0.
But if I put at the end of the textblock squiffy.set("skillresult", 0); or @unset skillresult, it does not work.
In fact if I try to display that var in the next page with {skillresult} the result is still stored.
An expedient would be to insert in the beginning and the top of the function the variable reset, but I wondered if there was another system.

Third and last question.
I wanted to create a variable that contained in memory all the equipment of the character (maximum 10 items). With a function then I could serialize the array, and another function at the right time of the script, find out whether the object exists in the array. Can someone help me?
Something like this:

var arr = [];
var arrSerialized = JSON.stringify(arr);
squiffy.set("charactersheetequipment", arrSerialized);

thanks in advance


In terms of your first point, could you not include the JS function in the main Squiffy HTML template, and then just call it in the normal way from within sections?

In terms of your second point, you might need to re-explain it to me, but you can reset / modify Squiffy attributes via links themselves, eg:

[[new section]](next, myattribute=+10)

Again, I think I might be misunderstanding your issue, so not sure if the above would help.

With regards to your last point, I'm sure it's possible but not something I have done myself. In fact, when I have wanted a basic inventory system, I have usually taken the simple road and just created a load of boolean variables for each potential item. Your solution would be more elegant though.


For the 1st point, I don't know how to include the function in the external Squiffy file.
For example if I write that function at the beginning of the script, that function is not available when I call it from a section in the center of the script. I have to rewrite that function everytime...
I have read this http://docs.textadventures.co.uk/squiffy/import.html but I don't know the command-line version of Squiffy (and how it works).

For the 2nd point (excuse me for my bad English), I know the syntax [[new section]](next, myattribute=+10) but I need to do something different:
[[mysection]]:

 // the function set the var skillresult to some value
 squiffy.set( "skillresult", abilityCheck(2,12,"strenght",0,8) );

I have my text here. bla bla bla. Then I have to switch the text depending on whether the test went well. So,
{if skillresult>=0:bla bla bla bla}{else: blablablabla}
Click [here] to continue.

Ok, in the next section, I would like to have my var "skillresult" cleaned. (0, null or NaN), but if I unset it in the previous section at the end of the text, it doesn't work (and I have understood why).
Click [here] to continue.
@unset skillresult

How can I clean that var?

For the 3rd point, do you manage object like in this way?
@set hashammer = 0
@set hasring = 1
@set hasrodoffire = 0
@set hassword = 0
and so on


If you are using the desktop version of Squiffy you can modify the index.html template and, in that, include your javascript function. When you compile it will include the modified code. You can then call it when needed from individual sections. I'm on my phone at the moment, but I can point you to the relevant directory tomorrow. As mentioned, you'll need to be using the desktop version as the web-based one gives you no access to the html template.

With the third point, yes. That's what I meant. It's a bit clunky but provided you don't have too many items to deal with it's just easier, as you can manage everything via Squiffy attributes rather than having to convert back and forth between JS variables.

I'll take another look at your second issue tomorrow (in bed with a cold at the moment).


You can only use Javascript at the start of a section. You can't swap freely between HTML / Text and Javascript. All Javascript has to be executed first.

So if you want to clear your Squiffy attribute "skillresult" after a section has been displayed, you could simply use:

{if skillresult>=0:bla bla bla bla}{else: blablablabla}
Click [here](here, skillresult=) to continue.

If you find yourself in a situation where you need to execute lots of Javascript after a section, you could always use something like:

 [[next link]](setup)

 [[setup]]:
 // all the Javascript you want to execute can go here
squiffy.story.go("exitsetup");

[[exitsetup]]:
The next normal bit of your story goes here.

So you basically put all your Javascript stuff into it's own section, and then use the squiffy.story.go function to automatically move the player to the next bit of the story.

Oh, and in terms of the index HTML template, if you're using the Windows version of Squiffy, the path is something like:

C:\Program Files (x86)\Squiffy\resources\app\node_modules\squiffy

The file is called index.template.


Thank you very much for the tips!
Under linux the file is located into /installpath/Squiffy-linux-x64/resources/app/node_modules/squiffy
I have tried the code for the other questions but I have still not tried the include. I will do it tomorrow. Thank you


Hello. The trick of inserting functions under index.template.html seem not work.
I have inserted my custom function after the Jquery.
Any idea? :/

	<script src="<!-- JQUERY -->"></script>
	<script src="story.js" charset="UTF-8"></script>
	<!-- SCRIPTS -->
	<script>
		jQuery(function($){
			$('#squiffy').squiffy();
			var restart = function () {
				$('#squiffy').squiffy('restart');
			};
			$('#restart').click(restart);
			$('#restart').keypress(function (e) {
				if (e.which !== 13) return;
				restart();
			});
		});


function abilityCheck(mn,mx,abilityname,spc,trg){
  var curxp = squiffy.get("charactersheetxp");
  var cursc = squiffy.get("charactersheetscore");
  var ability = squiffy.get(abilityname);
  var min = Math.ceil(mn);
  var max = Math.floor(mx);
  [.............]
  return  dice+bonus-trg;
}
	</script>

Will have a look tomo.


thank you!


I have found the solution here:
http://textadventures.co.uk/forum/squiffy/topic/6366/loading-variables-off-an-if-statement

So, the only thing to do is to put the function at the beginning of the script with this syntax:

@start Intro

[[]]:
// check ability function
squiffy.abilityCheck = function(mn,mx,abilityname,spc,trg){
 //....
}

And then, at any place of the script I can call it in this way.

 squiffy.set( "skillresult", squiffy.abilityCheck(2,12,"characterstatsdexterity",spc,8) );

Sorry didn't get back to you. Well done sorting it though.


This topic is now closed. Topics are closed after 180 days of inactivity.

Support

Forums