Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Sugarcube] Best Solution For Stat Distribution / Macro?

Hello Guys.

I'm making a mini-rpg, and trying to make a system with some points that the players can distribute.
I'd like to have buttons with + and - that changed the values, (replacing them), and that also stopped working once the player reached 0 points to distribute.

Here's my code:
<<nobr>><<set $player = {
	"name" : "Rodrigo",
	"st" : 1,
	"ag" : 1,
	"en" : 1,
	"in" : 1,
	"ch" : 1,
	"uk" : 1,
	"hptotal" : 10,
	"hpnow" : 9,
	"gold" : 0
	}>>
<<set $points = 6>>
<<set $pointsmax = 6>>

<</nobr>>''$player.name''

Points to Distribute: <span id="points">$points</span>
 
Strength: <span id = "st">$player.st</span><<nobr>>

	<<button "+">>
		<<if $player.st == 5>>
		<<elseif $points == 0>>
		<</elseif>>
		<<else>>
			<<set $player.st = $player.st + 1 >>
			<<set $points = $points - 1>>
			<<replace "#st">>
				<<print $player.st>>
			<</replace>>
			<<replace "#points">>
				<<print $points>>
			<</replace>>
		<</else>		
		<</if>>
	<</button>>
	<<button "-">>
		<<if $player.st == 1>>
		<<elseif $points == $pointsmax>>
		<</elseif>>
		<<else>>
		<<set $player.st = $player.st -1 >>
		<<set $points = $points + 1>>
		<<replace "#st">>
			<<print $player.st>>
		<</replace>>
		<<replace "#points">>
			<<print $points>>
		<</replace>>
		<</if>>
	<</button>>
</span>\
<</nobr>>

There is anyway to make this thing into a JavaScript macro, so I don't have to type all this stuff over and over just so the player can change his stats clicking?
Like <<distplus $points $player.st "#st" "#points">> ?

Thanks for the help!

Comments

  • edited May 2016
    If you are willing to change to the SugarCube 2 story format then you could use the new <<numberpool>> macro set add-on (found in the Downloads section) which TheMadExile created to solve this exact problem.

    If you did then your code would look something like:
    <<nobr>>
    <<set $player = {
    	"name" : "Rodrigo",
    	"st" : 1,
    	"ag" : 1,
    	"en" : 1,
    	"in" : 1,
    	"ch" : 1,
    	"uk" : 1,
    	"hptotal" : 10,
    	"hpnow" : 9,
    	"gold" : 0
    	}>>
    <<set $points = 6>>
    <<set $pointsmax = 6>>
    
    <</nobr>>''$player.name''
    
    Points to Distribute: <span id="points">$points</span>
    
    <<numberpool "$points">>
    |''Strength:''|<<numberbox "$player.st" $player.st 1 5>>|
    |''Ag:''|<<numberbox "$player.ag" $player.ag 1 5>>|
    |''En:''|<<numberbox "$player.en" $player.en 1 5>>|
    |''In:''|<<numberbox "$player.in" $player.in 1 5>>|
    ...
    <<onchange>>
        <<replace "#points">>$points<</replace>>
    <</numberpool>>
    

    But to answer your question, yes it would be possible.
  • Thank you so much greyelf!
    That's exactly what I was hoping for.
Sign In or Register to comment.