Confusion with coding

Hi so I am very new to twine and coding and ive come up on a bit of a block. I am trying to make stats. Like oposing stats and a little window to see the stats. I have no idea how to do it. I am using the most recent version of twine and sugarcube. I am also trying to get a bar to show what direction the stats are leaning if that makes sense. Thank you

Welcome EJA!

First of all, as you say you’re new to Twine and Sugarcube, here’s a link to the section about variables: https://www.motoslave.net/sugarcube/2/docs/#twinescript-variables.

  1. The good news about opposed variables: you basically only want one variable.
    In a passage you name StoryInit:
<<set $Force to 5>>

Anytime you’re variable goes up or down:

<<set $Force to +=1>>
<<if $Force gt 10>>
<<set $Force to 10>>
<</if>>

or

<<set $Force to -=1>>
<<if $Force lt 0>>
<<set $Force to 0>>
<</if>>

The opposed variable is always equal to (in this example 10- $Force), so anytime you think you need the opposed variable to be greater than 5, you actually need $Force to be lesser than 5.
Note you can set any maximum you want, if you want the opposed variables to go from 0 to 100 you can, or even from 3 to 6 if you wish so.

  1. With the StoryCaption message you can set a link to the character stats, this link will appear in the left sidebar.
<<link "Player statistics" "Stats">><</link>>

In the Stats passage you can show any characteristics you want:

Strength: $Force
Weakness: <<print 10-$Force>>

It will show as
Strength: 5
Weakness: 5

And you might want to add a <<back>> macro in the Stats passage in order to allow the reader to go on.

  1. About the bar … I don’t know how to, someone else will have to step on this one!
1 Like

The stats page was something i was having alot of trouble with for some reason so thank you!

1 Like

Additional to souppilouliouma excellent advice on how to use an <<if>> macro to stop the value of “stat” variable going outside a pre-determined range (eg. 0 to 10)…

You can use the Math.clamp() function when you’re change the variable’s value using the <<set>> macro to achieve a similar outcome.

/* Increase "force" by one, making sure value doesn't go above 10 */ 
<<set $Force to Math.clamp($Force + 1, 0, 10)>>

/* Decrease "force" by one, making sure value doesn't go below 0 */ 
<<set $Force to Math.clamp($Force - 1, 0, 10)>>

The following is a simple test that shows the above works as expected.

<<set $Force to 9>>
before: $Force
<<set $Force to Math.clamp($Force + 1, 0, 10)>>
after 1st: $Force
<<set $Force to Math.clamp($Force + 1, 0, 10)>>
after 2nd: $Force

<<set $Force to 1>>
before: $Force
<<set $Force to Math.clamp($Force - 1, 0, 10)>>
after 1st: $Force
<<set $Force to Math.clamp($Force - 1, 0, 10)>>
after 2nd: $Force
1 Like

Ok cool so looking all this over I think ill probably stick with normal variables instead of opposed. When creating my variables though say Charisma do i put that in my stats page? And how do I start it with 0?

To create your variables, make a passage named StoryInit and set the starting value of all your stats there (i.e. <<set $Charisma to 0>>).

Also, if you want to display ChoiceScript-style opposed stat bars, I wrote a bit of code for that over in this thread.

Awesome! Thank you! I actually moved to twine from choice script so its a bit of a learning curve especially since i had everything choice script pretty much sorted.

1 Like