Widget Problems

Twine Version: 2.3.16
SugarCube Version: 2.36.1

Hi Guys,
I hope one of you might be able to help out, because I’ve hit a little speed bump: I am working on a number of assets for a game, among them a few widgets to handle things like experience points, skill- and attribute- increases, and weekly accumulated work-hours. The first couple works like a charm. I can increase the protagonist’s skills and attributes, and my widgets tracks the progression perfectly. The one I designed to track the protagonist’s dayjob and accumulated hours, however, is causing me some grief. I have designed it in exactly the same manner as they previous ones, but with other story variables of course. All my variables are defined in the StoryInit passage, and I call the widget normally with it’s name but then I get an error saying that Twine can’t find a closing tag for the macro.

My first version of this widget looked like this:

<<widget "work">><<silently>>
<<set $Wage += ($CharismaTotal + $PhysiqueBase) * $Bartend>><<set $Bartend to 0>>
<<set $Wage += ($PhysiqueTotal + $IntelligenceBase) * $Restock>><<set $Restock to 0>>
<<set $Wage += ($WillpowerTotal + $CharismaBase) * $OpenClose>><<set $OpenClose to 0>>
<<set $Cash to $Wage + 50>><<set $Wage to 0>>
<</silently>><</widget>>

It was simple and straight forward, or so I thought. Track a variety of work-related tasks, calculate wage based on composite attributes and attribute components, and adding wage to cash before resetting everything for the coming week. It has since gone through a couple of iterations in an attempt to solve the issue myself. The current state looks like this:

<<widget "work">><<silently>>
<<if $Bartend gte 1>><<set $Wage += $Bartend * ($CharismaTotal + $PhysiqueBase)>><<set $Bartend to 0>><</if>>
<<if $Restock gte 1>><<set $Wage += $Restock * ($PhysiqueTotal + $IntelligenceBase)>><<set $Restock to 0>><</if>>
<<if $OpenClose gte 1>><<set $Wage += $OpenClose * ($WillpowerTotal + $CharismaBase)>><<set $OpenClose to 0>><</if>>
<<if $Wage gte 1>><<set $Cash to $Cash + ($Wage + 50)>><<set $Wage to 0>><</if>>
<</silently>><</widget>>

I have now included a lot of if-statements to ensure that it only calculates what it needs to before resetting my parameters, and I have switched around in the math a little. But, still, no cigar. Including a closing tag for my widget macro in the passage where I call it can silence the error message, but that hasn’t been necessary for my other widgets, and it still doesn’t add any money. Any help is greatly appreciated…

You didn’t include an example of what values the related variables have, but I will assume that all of them have been assigned a Number of some kind. Especially the $Wage variable which you are applying a += assignment operator to, because that type of assignment will fail if the $Wage variable doesn’t already contain a value.

I added your Widget definition to a new project, then used the following Passage code to test it…

<<set $CharismaTotal to 1; $PhysiqueBase to 2; $Bartend to 3>>
<<set $PhysiqueTotal to 1; $IntelligenceBase to 2; $Restock to 3>>
<<set $WillpowerTotal to 1; $CharismaBase to 2; $OpenClose to 3>>

<<set $Cash to 100; $Wage to 0>>

<<work>>

Cash: $Cash

…and the $Cash variable contained an expected Number.

My apologies, I should clarify. Yes, all variables have been assigned numeric values, 0 as default. The attributes used in the widget (charisma, physique, etc.) are composites, hence there’s a base and a total. But I have increased my variables appropriately for various tests of each widget, and this is the only that doesn’t work so far. Is there a chance my math is messing it up? And if so, is there a different way to achieve a similar function?

For example, would it solve anything to split the equations and include additional variables like this:

<<set $Shift1 to $CharismaTotal + $PhysiqueBase>>
<<set $Wage += $Shift1 * $Bartend>><<set $Bartend to 0>>
<<set $Cash += $Wage + 50>><<set $Wage to 0>>

After several attempts to rewrite the widget, I have finally solved the problem. Thank you :wink: