string var in text box to show random numbers

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

string var in text box to show random numbers

Post by marionline »

Hello,

I'm having two problems with this script. It's a mixture of the script from OHRRPGCE feature requests/suggestions thread and my scripts.

The random number seemed to be returning strange results (results within a range it was not supposed to show), so I wanted to check what randomnumber actually it was. That almost immediately lead to the next problem: displaying a number in a textbox.
I took a look at how to show string var in text box but all I get is a textbox (like in the attached picture) that seems to re-appear as soon as I press space to make it go away.

Code: Select all

script, save_string_variable, var, begin 
    variable(num)
    num:=var
    #https://www.slimesalad.com/forum/viewtopic.php?t=7142&highlight=stringvar+textbox
    string sprintf(1, $0="Random Number is %d", var)
    showtextbox (14) #textbox 14 asks for string_id1
    wait for textbox
    end

plotscript, RandomTreeText, begin
  variable(randomnumber)
  randomnumber := random(61-69)
  save_string_variable(randomnumber)
  showtextbox (randomnumber)
  wait for textbox
end

script, RandomStoneText, begin
  showtextbox (random (70-75))
  wait for textbox
end

script, RandomMushroomText, begin
  showtextbox (random (76-79))
  wait for textbox
end

plotscript, on keypress handler, begin
  variable(x, y, d)
  d := hero direction(me)
  x := ahead x(hero x(me), d)
  y := ahead y(hero y(me), d)
  if (keyval(key:space) > 1 || keyval(key:enter) > 1 || keyval(key:ctrl) > 1) then (
        if&#40;x >= 0 && x < map width && y >= 0 && y < map height&#41; then&#40;
            if&#40;read zone&#40;1, x, y&#41;&#41; then&#40;
              # This is a tree!
              RandomTreeText
            &#41;
            #if&#40;read zone&#40;2, x, y&#41;&#41; then&#40;
            #  # This is a stone!
            #  RandomStoneText
            #&#41;
            #if&#40;read zone&#40;3, x, y&#41;&#41; then&#40;
            #  # This is a mushroom!
            #  RandomMushroomText
            #&#41;
        &#41;
  &#41;
end

script, ahead x, x, d, dist=1, begin
  switch&#40;d&#41; do&#40;
    case&#40;left&#41; exit returning&#40;x -- dist&#41;
    case&#40;right&#41; exit returning&#40;x + dist&#41;
  &#41;
  exit returning&#40;x&#41;
end

script, ahead y, y, d, dist=1, begin
  switch&#40;d&#41; do&#40;
    case&#40;up&#41; exit returning&#40;y -- dist&#41;
    case&#40;down&#41; exit returning&#40;y + dist&#41;
  &#41;
  exit returning&#40;y&#41;
end 
Attachments
adventure0025.bmp
adventure0025.bmp (63.55 KiB) Viewed 670 times
Last edited by marionline on Sun Jun 25, 2017 7:32 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

You need to write "random(70, 75)", not "random(70 - 75)". That hspeak doesn't show an error when you write 70-75 is a known bug in hspeak.

And you need to write "${S1}" in the textbox, not "{$S1}".

Also, if you just want to show a number in a textbox, you can use ${V#} instead to show the value of a global variable, instead of using a string:

Code: Select all

global variable &#40;10, randnum&#41;

plotscript, show random, begin
  randnum &#58;= random&#40;0, 100&#41;
  show textbox &#40;5&#41;  # "Random number is $&#123;V10&#125;"
end
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

Thanks a lot for the fast reply! And pointing out all the mistakes. :D
The scripts seem to be working better. The textboxes are displayed correctly.

But for some reason there always is a new random textbox triggered, as soon as the old one is 'deleted'. The player gets stuck in front of a tree.
I'm not sure where that come from. Is it somehow related to the each step 'on keypress handler' script?
Do I need to set a trigger textbox break or something?
User avatar
SwordPlay
Chemical Slime
Posts: 966
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

Post by SwordPlay »

"show value " & "show string" will show the value or string directly on-screen.
Put it in your autorun script and it should update too.
Last edited by SwordPlay on Sun Jun 25, 2017 10:37 pm, edited 3 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Oh, yes, I see why that's happening. It should have occurred to me. Whenever you press a key to close the textbox it immediately opens a new one. Just add the following to the start of the onkeypress script:

Code: Select all

if &#40;current textbox > -1&#41; then &#40;exit script&#41;
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

Thanks for the help! :)
It's working.
Post Reply