I am trying out a <<click>> and <<replace>> example on the sugarcube documentation page and I am wanting to have a single variable that will be the total skill points a player can spend and it will increase or decrease when they increase or decrease various stats. I haven't accounted for what happens when all of the points have been spent at this point yet.
However i can't work out how to update the variable as per example.
This is what i tried..
Points left to spend:
<<set $Sklpoints = 20>><<id "stats-$Sklpoints">><<print $Sklpoints>><</id>>
<br>
<br>
<p id="p1">
<center>Strength:
<<set $pcStr = 10>><<id "stats-str">><<print $pcStr>><</id>>
<br>
<<click "[+]">><<set $pcStr++>><<set $Sklpoints++>><<replace "#stats-str">><<print $pcStr>><</replace>><<replace "#stats-$Sklpoints">><<print $Sklpoints>><</replace>><</click>> |
<<click "[-]">><<set $pcStr-->><<set $Sklpoints-->><<replace "#stats-str">><<print $pcStr>><</replace>><<replace "#stats-$Sklpoints">><<print $Sklpoints>><</replace>><</click>></center>
</p>
<p id="p1">
<center>Agility:
<<set $pcAgl = 10>><<id "stats-agl">><<print $pcAgl>><</id>>
<br>
<<click "[+]">><<set $pcAgl++>><<replace "#stats-agl">><<print $pcAgl>><</replace>><</click>> |
<<click "[-]">><<set $pcAgl-->><<replace "#stats-agl">><br><<print $pcAgl>><</replace>><</click>></center>
</p>
How can I update the skills left variable? without refreshing the page?
Comments
Don't use
<<id>>
(or<<class>>
) as they're deprecated. Simply use the appropriate HTML tag with either theid
and/orclass
attributes.Don't use characters which are not alphanumerics, hyphens, or underscores as part of a CSS/DOM identifier (i.e. don't do this: "
stats-$Sklpoints
").You're doing the (in|de)crements of your
$Sklpoints
backwards.Your paragraphs incorrectly use the same
id
. Within a page, all IDs must be unique.It would be cleaner to do all of the $variable initialization at the top.
You're probably also going to want some range checks against the point pool, when incrementing stats, and each stat, when decrementing them (in the example, the pool is clamped to
0
while each stat is clamped to1
).Try something like this: (I assume you're doing this with
nobr
)As TheMadExile explained in point 4, the HTML specification itself requires that all tag id's within a page must be unique.
So having two (or more) paragraph tags with the same ID within your page makes that page invalid HTML and there is no way of knowing exactly what the users browser (brand/version) is going to do about it. You telling people to do otherwise Dazakiwi38 is not a good idea.
If you wish to apply that set of CSS rules to more than one paragraph within a page then you should change your code to something like the following: And change your CSS from being ID based to being CLASS based:
Thanks for your post greyelf. I wasn't purposely trying to contradict MadExile, im still learning. But your words have cleared up the differences between and ID and classes, I thought they did the same thing more or less. Sorry if i was misleading people with my previous post. The reason i mentioned it was i said nothing about the stylesheet originally and that's why i mentioned it. I misunderstood what Exile meant by all ID's have to be unique. I thought he only meant per the code he wrote up.
Okay, so now i know duplicate ID's can make a html page invalid, use classes instead for something like this.
Well at least others in the learning boat, will pick something up from this thread.
Thanks for your suggestion with the class example.