Many people want to improve the flexibility of "{if:}" statement . So I tried to find the way to improve it only with "in-squiffy javascript" by real time function redefinition. But I found it is technically impossible because some squiffy functions are hidden.
I suggest the Creator to change source codes as the following. I tried to achieve the maximum flexibility with the minimum change of source codes. I think it deserve to be applied for many squiffy users.
Thanks.
p.s. If someone want to try it, modify "story.js" after "build" according to "How to".
Don't be afraid. It's a piece of cake. :)
Change squiffy variables in "if" statement
1.1. Usage
{if a=1:{@b+=1,c-=1,d=2,not e}}{else:{@b+=2,c+=2,d=3,e}}
// add "{@ operations}" after condition colon(:)
// "not e" means "e=false", "e" means "e=true"
1.2 How to
Add additional "else if" clause in "processTextCommand" function as the following.
(story.js line # 415~420)
else if (startsWith(text,'@') && !startsWith(text,'@replace')) {
processAttributes(text.substring(1).split(","));
return "";
}
Comparison between squiffy variables
2.1 Usage
{if a=@b: } // if you add @ before second variable, it is treated as a squiffy variable not value.
2.2. How to
Add only one line in "processTextCommand_If" function as the following.
( story.js line # 438~439 in original code or #442~443 after adding additional "else if" clause above. )
if (match) {
var lhs = squiffy.get(match[1]);
var op = match[2];
var rhs = match[3];
if(startsWith(rhs,'@')) rhs=squiffy.get(rhs.substring(1)); // <-- add only this one.
As we know, multiple conditions can be handled with nested-if-clause (e.g. {if:{if:{if... )
var setAttribute = function(expr) {
var lhs, rhs, op, value;
var setRegex = /^([\w]*)\s*=\s*(.*)$/;
var setMatch = setRegex.exec(expr);
if (setMatch) {
lhs = setMatch[1];
rhs = setMatch[2];
if (isNaN(rhs)) {
if(startsWith(rhs,"@")) rhs=squiffy.get(rhs.substring(1)); // @variable
squiffy.set(lhs, rhs);
}
else {
squiffy.set(lhs, parseFloat(rhs));
}
}
else {
var incDecRegex = /^([\w]*)\s*([\+\-\*\/])=\s*(.*)$/; // add * and /
var incDecMatch = incDecRegex.exec(expr);
if (incDecMatch) {
lhs = incDecMatch[1];
op = incDecMatch[2];
rhs = incDecMatch[3]; //
if(startsWith(rhs,"@")) rhs=squiffy.get(rhs.substring(1)); // @variable
rhs = parseFloat(rhs); //
value = squiffy.get(lhs);
if (value === null) value = 0;
if (op == '+') {
value += rhs;
}
if (op == '-') {
value -= rhs;
}
if (op == '*') { // multiplication(*)
value *= rhs;
}
if (op == '/') { // division( /)
value /= rhs;
}
We can do the following.
[[Lucky day]]:
The begger has no money. {@b_has=0}
He finds a dollar on the floor and takes it. {@b_has+=1}
Now he has {b_has} dollar.
{if b_has>100:He feels rich.}{else:But, he feels still poor.}
A rich man goes by the begger. {@r_has=100}
He gives a half of money to the begger. {@r_gives=@r_has}{@r_gives/=2}
The begger says,
{if r_gives=@r_has:{@b_has+=@r_has}{@r_has=0} "Thank you very much, sir!"}
{else:{@b_has+=@r_gives}{@r_has-=@r_gives} "Thank you for {r_gives} bucks."}
Now he has {b_has} dollars.
{if b_has>100:He feels rich.}{else:But, he feels still poor.}
The rich man gets back and gives the begger all the money he has. {@r_gives=@r_has}
The begger says,
{if r_gives=@r_has:{@b_has+=@r_has}{@r_has=0} "Thank you very much, sir!"}
{else:{@b_has+=@r_gives}{@r_has-=@r_gives} "Thank you for {r_gives} bucks."}
Now he has {b_has} dollars.
{if b_has>100:He feels rich.}{else:But, he feels still poor.}
The begger has no money.
He finds a dollar on the floor and takes it.
Now he has 1 dollar.But, he feels still poor.
A rich man goes by the begger. He gives a half of money to the begger.
The begger says, "Thank you for 50 bucks."
Now he has 51 dollars. But, he feels still poor.
The rich man gets back and gives the begger all the money he has.
The begger says, "Thank you very much, sir!"
Now he has 101 dollars. He feels rich.
Dear Luis and Alex,
I think the power of Squiffy is the easiness which enables everyone to make IF without any knowledge of programming.
Although the core functionality of IF is processing variables and conditions, current version does not support it well, as such, many users have to repeat a bunch of "squiffy.get(), squiffy.set()".
The advantages of modified Squiffy are
Can handle variables "everywhere".
Even if we place "@set", "@inc", "@dec" statements in the middle of Section, current Squiffy processes it at the beginning and does not allow handling variables in {if:} statement.
In the modified Squiffy, we can set and change variables in everywhere. So, we can write squiffy code according to the flow of storyline. It makes squiffy codes more intuitive and look like annotation of real book. In addition, we can be free from a bunch of squiffy.get(), squiffy.set(). It make squiffy code more slim and readable.
Can calculate complex formula
Modified Squiffy can handle complex formula with a chain of unit calculation.
For. example, c=(a+b)/2 is equal to {@c=@a}{@c+=@b}{@c/=2} or {@c=@a,c+=@b,c/=2}
Don't need to change previous codes
Modified Squiffy does not change any grammar of current Squiffy.
Just add only one new statement {@ }.
The only potential problem may occur when there is variable name starting with @.(But nobody do that. right?)
As such, modified Squiffy can be applied right now.
I expect you agree with my opinion and my suggestion be applied to new release of Squiffy.
Thanks.
:)