Creating a widget to do a point tally

I’m working on creating a game, called Angel or Devil. you have to make a series of choices and you get either naughty or nice points based on the choices at the end when the real game starts I want to make it so the points are tallied and whichever one has more is what you are. I made a widget but cant seem to get it to do what I want not sure If im doing it right.

<<widget "NoN">>
	<<if $args[0] is $naughty>>
    	<<if $args[1] is $nice>>
        <<if $args[0] >= $args[1]>>
        	<<print '<img src="AD/devil.png" style="width: 50%; height: auto;"/>'>>
        <<elseif $args[0] <= $args[1]>>
        	<<print '<img src="AD/angel.png" style="width: 50%; height: auto;"/>'>>

I want to put the widget so it shows on the sidebar once the initial task of choosing is done and its all tallied up. but when I put the widget on the sidebar or even on a passage nothing shows up.


Did you put the code above inside a widget-tagged passage or inside the Sidebar directly?


There are two parts to using Widgets.

1: Creating (defining) the Widget using the <<widget>> macro.

As explained in the macro’s documentation, this is done within a Passage that has been assigned the special widget Passage Tag.

note: This special Passage Tag should not be assigned to any of the special Passages, like StoryInit or StoryCaption.

eg. the following example defines a custom <<NoN>> widget that displays one of two images, depending on the relative values of the $naughty and $nice Story Variables.

<<widget "NoN">>
	<<if $naughty >= $nice>>
		<img src="AD/devil.png" style="width: 50%; height: auto;">
		<img src="AD/angel.png" style="width: 50%; height: auto;">

note: If you don’t want your custom widget to output the line-breaks included in the definition’s code, then assign the special nobr Passage Tag to the Passage that contains the widget definitions.

2: Calling the Widget

Custom widgets are called using the same syntax as macro calls, and they can be called within any Passage that macros can, including special ones like StoryCaption (which represents the main area of the side-bar.

1 Like

ok that makes sense thanks for that. One last question on that is there a way I can make it so it only works after all the questions have been answered? like once you reach a certain passage within the game it will kick in

That depends on where you are putting it. If you are just using it within a passage, you don’t need to call it until you are ready.

If you are using it in a special passage like StoryCaption, then you should wrap it in an <<if>> macro to hide it until you are ready. The easiest way to do that is to set a variable as a flag.

The following example is in Twee notation (where :: indicates a passage)

:: StoryInit
<<set $showNoN = false>>

:: StoryCaption
<<if $showNoN>><<NoN>><</if>>

:: Passage after questions have been answered
<<set $showNoN = true>>

The Angel/Devil picture will then appear from the next passage after you set $showNoN to true.

ahhh cool. Thank you for that. Will be very helpful!

im not sure if i can do this or not but I want to try something like

<<if NoN is Angel go to this passage>>
    <<elseif NoN is Devil go to this passage>>

I have different sets of avatars to choose for each so if they get the angel I want them to be able to go and choose the angel type avatars and if its devil to choose the devil type avatars

<<if NoN is Angel>>
<<elseif NoN is Devil>>


Manon has shown you the correct syntax for using the <<if>> and <<elseif>> macros to conditionally show one of two possible links.

However, the NoN is Angel and the NoN is Devil expressions being used as the condition for both of those macro calls will likely result in an error. Unless you have JavaScript variables named NoN, Angel, and Devil in your project, which is unlikely.

So your project needs a means to determine (and track?) the current Angle/Devil state of the player. And you have already stated that when $naughty >= $nice they are a Devil, so changing the above conditional expressions to the following should work.

<<if $naughty >= $nice>>