HTML Sugarcube Help

Uncle Iroh

Member
Jun 15, 2017
229
551
I'm trying to use a stat system for NPC's in a twine sugarcube game. Snippet of NPC code:

Code:
<<widget "AddStat">>
  <<set _npc = $args[0]>>
  <<set _stat = $args[1]>>
  <<set _app = $args[2]>>
  <<set _limit = $args[3]>>

  <<if _npc != "mc">>

    <<if _stat == "corruption">>
      <<if _limit == 0>>
        <<set $NPCstat[_npc].corruption += _app>>
     
        <b><i>$NPCstat[_npc].name's corruption was increased _degree</i></b>
      <<elseif $NPCstat[_npc].corruption < (_limit - _app)>>
        <<set $NPCstat[_npc].corruption += _app>>
     
        <b><i>$NPCstat[_npc].name's corruption was increased _degree</i></b>
      <<elseif $NPCstat[_npc].corruption < _limit>>
        <<set $NPCstat[_npc].corruption = _limit>>
     
          <b><i>$NPCstat[_npc].name's corruption was increased _degree</i></b>
      <<else>>
        <b><i>This action can't increase $NPCstat[_npc].name's corruption further</i></b>
      <</if>>
    <</if>>
  <</if>>
The code I have works when I call on it to change the mc's stats using <<AddStat mc "willpower" 5 100>> , but when I call an NPC stat change using <<AddStat 0 "love" 5 100>>. I get this error " Error: <<AddStat>>: error within widget contents (Error: <<if>>: bad conditional expression in <<elseif>> clause (#1): Cannot read property '0' of undefined) ".

I already defined the NPCstat array (shown below) So it has to be something to do with the locating that array or something. Any help? I'll attach the HTML if anyone wants to see all of the code. The parts I'm having problems with is in "NPC System" and "Stat Widgets".

Code:
<<set $NPCstat = []>>


/*                                 Mother                                 */

<<set $NPCstat[0] = {
    name: "Alexis",
    lastname: "$mc.lastname",
    realname: "Alexis Fawx",
    corruption: 0,
    obedience: 0,
    love: 30,
    lust: 30
    style: "Mother",
    label: "Mother",
}>>
 
Last edited:

Sopra

Newbie
Jul 30, 2017
72
383
You forgot to <<include "NPC System">> in StoryInit, so the array wasn't being initialized. And if you do it, you will notice it will throw an error because you forgot a comma after lust:30 when you define the mother stat in the array. I'm not sure the one after label:"Mother" is necessary too as it's the last entry but I may be wrong on that one.

Hope that helps !
 

Uncle Iroh

Member
Jun 15, 2017
229
551
You forgot to <<include "NPC System">> in StoryInit, so the array wasn't being initialized. And if you do it, you will notice it will throw an error because you forgot a comma after lust:30 when you define the mother stat in the array. I'm not sure the one after label:"Mother" is necessary too as it's the last entry but I may be wrong on that one.

Hope that helps !
Thanks that was it, I also had to change the mother's array from 0 to 1. Guess the created array doesn't include 0