Progression/Level up system question for dialogue based game in Sugarcube 2.31.1

So in my game you earn certain exp with conversations and actions. I currently have this script running to check for level ups (I am completely new to coding)

  <<set $hugo.persuadeskill to 1>>\
  <</if>>\
  <<if $hugo.fearscore gte 10 and $hugo.fearscore lt 22>>\
  <<set $hugo.intimidateskill to 1>>\
  <</if>>\
   <<if $hugo.detectscore gte 10 and $hugo.detectscore lt 22>>\
  <<set $hugo.detectiveskill to 1>>\
  <</if>>\
\
<<if $hugo.localtrustscore gte 22 and $hugo.localtrustscore lt 37>>\
  <<set $hugo.persuadeskill to 2>>\
  <</if>>\
  <<if $hugo.fearscore gte 22 and $hugo.fearscore lt 37>>\
  <<set $hugo.intimidateskill to 2>>\
  <</if>>\
   <<if $hugo.detectscore gte 22 and $hugo.detectscore lt 37>>\
  <<set $hugo.detectiveskill to 2>>\
  <</if>>\
\
<<if $hugo.localtrustscore gte 37 and $hugo.localtrustscore lt 57>>\
  <<set $hugo.persuadeskill to 3>>\
  <</if>>\
  <<if $hugo.fearscore gte 37 and $hugo.fearscore lt 57>>\
  <<set $hugo.intimidateskill to 3>>\
  <</if>>\
   <<if $hugo.detectscore gte 37 and $hugo.detectscore lt 57>>\
  <<set $hugo.detectiveskill to 3>>\
  <</if>>\
\

It’s obnoxious and takes up a lot of room on my passages, and I know there has to be a better way to code it. or at least more concise or compact.
So I would love suggestions.
Thanks everyone!

I would suggest looking into widgets.
https://www.motoslave.net/sugarcube/2/docs/#macros-macro-widget

I’m not on my PC so I’m not able to write you a widget that’s tested, so this example widget will have to do. It goes something like this:

<<widget "levelup">>
  <<set $args[0].persuadeskill to 1>>\
  <</if>>\
  <<if $args[0].fearscore gte 10 and $args[0].fearscore lt 22>>\
  <<set $args[0].intimidateskill to 1>>\
  <</if>>\
   <<if $args[0].detectscore gte 10 and $args[0].detectscore lt 22>>\
  <<set $args[0].detectiveskill to 1>>\
  <</if>>\
  /% The rest of the level up stuff goes here %/
<</widget>>

Widgets should be defined in a passage that uses the widget tag, just like it says in the reference.

After defining the widget, you can then call it with this in your passage:

<<levelup $hugo>>

As you can guess, this levels up $hugo. This is because $args[0] is set to whatever object you pass as the first argument. Pass a different character if you want to level them up instead. Or, if Hugo is the only character and I assumed incorrectly, you can continue using $hugo instead of $args[0] in the widget, and then you can call it with just <<levelup>> (no argument).

1 Like

Thank you! I’ll try it and tinker with it [: Thank you so much!

1 Like

I cannot seem to get it to work. I put the widget in the story init? is that right?
and then tagged the passage “widget”
and then it tells me macro levelup doesnt exist.

Widgets don’t go into the StoryInit passage; just create a new passage, name it however you like, and tag it “widget”.

1 Like

Thank you! I misunderstood the concept [: