0 votes
by (130 points)
edited by

Hello to the someone who reads this,

I am writing my first Twine 2 SugarCube 2 Sci-Fi Game.

Can you or anyone answer a few questions I have? Thank you in advance for your support.

1: For a quiz I have a <<set $qsolved = false>> and <<set $q1 = 11>> in StoryInit, then at a passage 

<<link "SOMETHING QUESTION">><<set $q1 = 1>><</link>>

<<button "SOMEHING CHECK ANSWERS">><<if $q1 == 1>><<set $qsolved = true>><<replace "#qsolvedreplace">>SOMETHING NEW<</replace>><<endif>><</button>>

<<if $qsolved == true>>SOMETHING NEW<<endif>><span id="qsolvedreplace">BLANK at first</span>

Why doesn't the second if resolve as true and displays its content if I go <<back>> and on the passage again? The second if and "replace content" that goes to span have the same content so the quiz should "stay" solved.

2: Then is <<set $Variable used across the entire game/passages?

3: And can I set and change a $Variable in a function in Story Javascript that I <<run myFunction()>> in a passage?

 

Thank you very much for your time and sorry for not making a new thread.

2 Answers

0 votes
by (68.6k points)

SUGGESTION: It's probably best if you try to ask one question per thread.  Questions with multiple facets are fine, but asking multiple questions, even seemingly related ones, is problematic for this format.


1: […] Why doesn't the second if resolve as true and displays its content if I go <<back>> and on the passage again?

Because the <<back>> macro explicitly undoes the moment, it rewinds the history—the state modifications you made within the passage are undone.  If you want to return to the previous passage while retaining the history, you'll want to use either the <<return>> macro or some kind of link using the previous() function.  For a few examples of the latter:

→ Links using the previous() function
[[Return|previous()]]
<<link "Return" `previous()`>><</link>>
<<button "Return" `previous()`>><</link>>

 

2: Then is <<set $Variable used across the entire game/passages?

When you set a story variable (variables starting with a dollar sign) it exists, with the value you set, from that moment until the end of the game, unless you either change its value to something else or unset it.

Temporary variables (variables starting with an underscore) exist from the moment they're set until the next turn/moment.

SEE: SugarCube's TwineScript document.

 

3: And can I set and change a $Variable in a function in Story Javascript that I <<run myFunction()>> in a passage?

Yes, using the State API—specifically, State.variables.  Although, how you may do so depends on exactly what you're doing and how.  The most significant hurdle/gotcha is scope.  Your function needs to be within a scope where it can be accessed in later <<run>> or <<script>> macro invocations.  The two foolproof ways to go about this is to either make your function an auto-global (or a method of an auto-global object) or a method of SugarCube's setup object.

Using the setup object is safer, as there's no chance of clobbering or shadowing a host object or other built-in, which is possible using auto-globals.

 

Example 1A: Auto-global function

/* Add your function as an auto-global. */
window.yourFunctionName = function () {
	/* Set $qsolved to false. */
	State.variables.qsolved = false;
};

Usage:

<<run yourFunctionName();>>
	/* OR */
<<script>>yourFunctionName();<</script>>

 

Example 1B: Method of an auto-global object

/* Set up the Game object as an auto-global. */
window.Game = {};

/* Add your method to the Game object. */
Game.yourMethodName = function () {
	/* Set $qsolved to false. */
	State.variables.qsolved = false;
};

Usage:

<<run Game.yourMethodName();>>
	/* OR */
<<script>>Game.yourMethodName();<</script>>

 

Example 2: Method of the setup object

/* Add your method to the setup object. */
setup.yourMethodName = function () {
	/* Set $qsolved to false. */
	State.variables.qsolved = false;
};

Usage:

<<run setup.yourMethodName();>>
	/* OR */
<<script>>setup.yourMethodName();<</script>>

 

0 votes
by (159k points)

EDIT: It seems you got two answers for the price of one. *smile*

warning:  for simplicity sake some of the following information is conceptually correct but not 100% technically correct.

1. ... if I go <<back>> and on the passage again

Background about History and story variables:
Each time the story transitions from one Passage to another (can be to same as the current one Passage) a new Moment is created, and a clone/copy of the current state/values of all known story variables is made. This new Moment is assigned the name of the new Passage (about to be processed/shown) as well as the original state of the story variables, this new Moment is then added to the story's History sub-system. The clone of the story variable state is made available to the new Passage being processed.

Handling History (and story variables) this way allows for the ability for the story to unwind/go backwards to a previous Moment in the story's History, and for the variable state to be reverted back to as it was at that Moment in time.

As explained in the <<back>> macro documentation that macro does such an unwinding of History, which is why all your variables are being reverted back to their earlier state/values. You should used the <<return>> macro if you don't want History to be unwound.

2. Then is <<set $Variable used across the entire game/passages

Yes

3  And can I set and change a $Variable in a (Javascript) function. (paraphrase mine)

Yes. I suggest you read the State API, it explains among many things how to access and change the value of story variables (for the current Passage as well as for history) using Javascript code. (like that found within your (Javascript) function.

/* Accessing the current value of a story variable named $health %/
var health = State.variables["health"];

/* Assigning a numeric value to a story variable named $health for current Passage %/
State.variables["health"] = 100;

 

...