Howdy, Stranger!

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

Problem with textbox in SugarCube

edited April 2014 in Help! with 1.x
Hi guys, just wondering if anyone can tell me what I'm doing wrong here.

OK, I have a combat simulator (well basic version of one anyway) and in SugarCane everything seems to work fine.  However, in SugarCube it's not so.  I have a textbox being called with
<<textbox "$name" "Enter Your Name" "Begin Your Battle">>
On the passage called "Begin Your Battle" I have a check to see if $name is left empty, using
<<silently>>
<<if $name eq "">>
<<set $name = "The Nameless " + $lclass>>
<<else>>
<<set $name = $name>>
<<endif>>
On that actual passage which is like poster for a boxing match (e.g Boxer A vs Boxer B) it works well, so if I chose the Asashin you see your name as "The Nameless Asashin".  When I get into the actual fighting passages the name isn't appearing for the player and just appears as nothing.  If I actually call the player something (Fred for instance) then you see the name Fred, so I'm a bit confused.  Surely the fact that it's displaying on the passage that it's set in means it should persist across the passages?

I know I'm probably making a really noobish mistake somewhere, so if anyone can let me know where I'm going wrong it would be appreciated.

**Edit**  Is there anyway to convert a number such 34.56 to an integer.  I'm sure I saw it somewhere and I can't find it again :(

Comments

  • AntonMuerte wrote:

    OK, I have a combat simulator (well basic version of one anyway) and in SugarCane everything seems to work fine.  However, in SugarCube it's not so.  I have a textbox being called with
    <<textbox "$name" "Enter Your Name" "Begin Your Battle">>


    I would suggest:
    Enter Your Name: <<textbox "$name" "" "Begin Your Battle">>
    For something like this, using a default value which the player is only going to have to delete is kind of silly, especially since them leaving it blank is supposed to be a thing.


    AntonMuerte wrote:

    On the passage called "Begin Your Battle" I have a check to see if $name is left empty, using
    <<silently>>
    <<if $name eq "">>
    <<set $name = "The Nameless " + $lclass>>
    <<else>>
    <<set $name = $name>>
    <<endif>>


    The else clause does nothing useful, so you don't need it.  You'd also be better off trimming the player input, as a general thing, but also, specifically, for the equality check against the empty string.  I'd suggest:

    <<set $name = $name.trim()>>
    <<if $name eq "">>
    <<set $name = "The Nameless " + $lclass>>
    <<endif>>

    AntonMuerte wrote:

    When I get into the actual fighting passages the name isn't appearing for the player and just appears as nothing.


    I can confirm the issue.  It seems to be a problem with the way &lt;&lt;textbox&gt;&gt;'s handler setup in WebKit/Blink-based browsers, which causes shenanigans when you have &lt;&lt;textbox&gt;&gt; setup to forward to another passage upon pressing enter and press enter when the textbox is empty or contains only whitespace.  I'm looking into it now.


    AntonMuerte wrote:

    **Edit**  Is there anyway to convert a number such 34.56 to an integer.  I'm sure I saw it somewhere and I can't find it again :(


    Assuming you mean an actual number, and not a numeric string, the Math object has the following static methods: So, if your floats are positive only, you could use Math.floor() to truncate the fractional part.

    If you wanted an function which simply performs integer truncation, regardless of sign, you could use this:

    /* Call the appropriate method to truncate the fractional part */
    window.truncInt = function (num)
    {
    return (num < 0) ? Math.ceil(num) : Math.floor(num);
    };

    <<print truncInt(34.56)>> Prints 34
    <<print truncInt(-34.56)>> Prints -34
  • May I suggest taking a look at my example RPG? It answers all these questions, and probably a few you'll have later down the road as well: Example Turn-Based RPG (Pre-Alpha v. 0.4)

    To make sense of it, you might want to read my tutorial on JavaScript objects: Objects are Your Friends: How & Why to Use JavaScript Objects for Total Newbies

    The textbox macro is different in Sugarcane than it is in SugarCube.

    Here's the SugarCube textbox macro for naming the player from my example RPG:
    <<textbox "$player.name" "" "Town">> <<button "OK" "Town">><</button>>
    The textbox sets $player.name (which would just be $name in your example since it doesn't appear you are using objects). It's blank by default. Previously, I set $player.name (again, $name in your game) to "Player". You would set $name to "The Nameless". If the player doesn't input anything into the text box, it won't change the previously-defined name. So, you don't need any of that code you have in your "Begin Your Battle" passage. Your <<set $name = $name>> code is particularly redundant. ;)

    In answer to your question about removing the decimals after the whole number, you'll probably want to use the JavaScript floor() method.

    Here's some more code from my example RPG:
    <<set $attackRoll = Math.floor((Math.random()*20)+1)>>
    Again, you'll find the code in my example RPG interesting. It has tons of comments. I'd highly suggest checking it out.

    Hope that helps! :)
  • @The Mad Exile:
    Many thanks for the detailed reply.  The only reason the else was in there was because I've always been taught to include an else to catch any unexpected returns, and to be honest I wasn't aware you could just omit it.  I've learned something new :) .  Might I ask what .trim actually trims?

    What I meant by the number bit was basically what you said by the &lt;&lt;print truncInt(34.56)&gt;&gt; command.  Basically my problem is when the player progresses a level.  Their base health should increase by 10% which is fine by anything that's cleanly divisible by 10, but when it comes to numbers that don't end in a zero you end up with a decimal point, which serves no function as all the damage is a whole number.  Would it be possible to use <<set truncInt ($bhp)>> at all?  I'll give a try anyway.

    @Sharpe:
    I'll have a look at the links you provided over the next day or two.  I'm massively out of touch with programming languages at the moment, and probably even more so with Javascript as the last time I used it was about 14 years ago for some coursework.  I'm always up to learn something that helps better my understanding, and then hopefully contribute back in the future :) .  I'm already using the &lt;&lt;set $attackRoll = Math.floor((Math.random()*20)+1)&gt;&gt;, albeit with a different value to check whether either the player or the enemy has managed to dodge the attack so I'm fairly familiar with that.

    The game itself isn't all that particularly complicated and is really just a game of chance at the moment, although I'm including boss battles which will require certain items to be bought and special skills to be learned, so it may end up complicated LOL.  I did consider posting the source, but you guys would probably find so many noob mistakes that you'd be here all day, plus it's having a laugh at a mates stereotypical view of Japanese pop culture (especially Hentai) so not all that family friendly and most definitely NSFW.

    I'll probably get around to checking stuff out tomorrow as I need to catch up on some sleep I've lost courtesy of messing about with a Chromecast (nice piece of kit to stream stuff from phone/tablet/PC to your TV once you figure it out).

    Many thanks for the replies.
  • AntonMuerte wrote:
    Basically my problem is when the player progresses a level.  Their base health should increase by 10% which is fine by anything that's cleanly divisible by 10, but when it comes to numbers that don't end in a zero you end up with a decimal point, which serves no function as all the damage is a whole number.


    You say you know about Math.floor, so I guess I'm missing something, but that's what I would use there.

    If the player is on level 1 and he has 25 hit points, then raises to level 2 and gains 10% more hit points as a result, you would do:

    <<set $playerHP = $playerHP + (Math.floor($playerHP * 0.1))>>

    Instead of 27.5 hit points, it would return 27 hit points.

    Again, raising levels and stuff is all in the example RPG, though. Good luck! :)
  • TheMadExile wrote:

    AntonMuerte wrote:

    When I get into the actual fighting passages the name isn't appearing for the player and just appears as nothing.


    I can confirm the issue.  It seems to be a problem with the way &lt;&lt;textbox&gt;&gt;'s handler setup in WebKit/Blink-based browsers, which causes shenanigans when you have &lt;&lt;textbox&gt;&gt; setup to forward to another passage upon pressing enter and press enter when the textbox is empty or contains only whitespace.  I'm looking into it now.


    This should be fixed in SugarCube -3100, which I just published.  I tested it in every browser engine I have access to (Gecko, Trident, WebKit/Blink, Presto) and it seems to be working as intended now in all of them.  Let me know if that doesn't solve the issue.


    AntonMuerte wrote:

    Might I ask what .trim actually trims?


    The &lt;String&gt;.trim() method removes whitespace from both ends of the string.


    AntonMuerte wrote:

    Would it be possible to use <<set truncInt ($bhp)>> at all?  I'll give a try anyway.


    Not exactly like that, you'd want to use something like:
    <<set $bhp = truncInt($bhp)>>
    If $bhp is the $variable which your increasing by by 10%, then you could do it all in one step:
    <<set $bhp = truncInt($bhp * 1.1)>>
    You get the idea.  Again though, as long as your number is positive, you can simply use Math.floor().  For example:

    <<set $bhp = Math.floor($bhp)>>
    // or
    <<set $bhp = Math.floor($bhp * 1.1)>>
  • Sharpe wrote:

    Here's the SugarCube textbox macro for naming the player from my example RPG:
    <<textbox "$player.name" "" "Town">> <<button "OK" "Town">><</button>>
    The textbox sets $player.name (which would just be $name in your example since it doesn't appear you are using objects). It's blank by default. Previously, I set $player.name (again, $name in your game) to "Player". You would set $name to "The Nameless". If the player doesn't input anything into the text box, it won't change the previously-defined name.


    Actually, as of SugarCube -3100 that's no longer true.  For consistency with the other input macros (&lt;&lt;checkbox&gt;&gt; & &lt;&lt;radiobutton&gt;&gt;), &lt;&lt;textbox&gt;&gt; now sets the $variable to the default value of the control at initialization.

    You can still achieve the same effect, however, by setting the control's default value to the original value of the $variable.  For example:
    <<textbox "$player.name" $player.name "Town">> <<button "OK" "Town">><</button>>

    Sharpe wrote:

    Here's some more code from my example RPG:
    <<set $attackRoll = Math.floor((Math.random()*20)+1)>>


    In SugarCube, you can also do that as:
    <<set $attackRoll = random(1, 20)>>
Sign In or Register to comment.