Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Displaying a Variable in a string

Status
Not open for further replies.
I can't get this right, my friends have been waiting for over an hour for my next demo, and I cant release it because i cant get this to work. What im trying to do is show the number of a variable in a string. Like this:

Code:
    if $game_switches[26] == true
      $game_variables[7] = @vaultcode
      self.contents.draw_text(25, 158, 900, 64, "There is a vault behind the picture in The Bunny Hole bar")
      self.contents.draw_text(25, 182, 900, 64, "the vault code is " + @vaultcode.to_s + ".")
    end

but no matter what i do, i cant figure out the correct way to display the variable's number, which is a random number between 1000 and 9999, so i cant just type in the number...

Please and thank you, hopefully I and my friends wont have to wait much longer :)
 
You need to redo your draw_text method. Most people have no idea how it works.

Code:
self.contents.draw_text(x, y, width, height, text, alignment)

By filling in those values, you are creating a rectangle with your x, y, width and height. The text is then placed within that rectangle, centered along the height (height / 2 - font size / 2) and the text is aligned in the rect by the alignment parameter (0 - left, 1 - center, 2 - right).

Code:
----------------- 
|               |
|Some Text      |
|               |
-----------------

-----------------
|               |
|   Some Text   |
|               |
-----------------

-----------------
|               |
|      Some Text|
|               |
-----------------

Something like that.

I suggest something like
Code:
    if $game_variables[26]
      $game_variables[7] = @vaultcode
      self.contents.draw_text(16, 144, contents.width, 32, 'Line 1 Text')
      self.contents.draw_text(16, 176, contents.width, 32, "Your code is #{@vaultcode}.")
    end

Some other things to consider...

By default, 32 is a common "spacer". I suggest using multiples of 8, 16, 24, 32, etc. when styling windows, sprites, etc. It just makes things look nicer overall.

Double quotes "" should only really be used when placing a variable within them. Otherwise, I suggest using single quotes '' to incase text. It over makes a slight difference on the interpreter.

Code:
a = 'single quotes'
b = 'another text in single quotes'
c = "You can put a variable in double quotes using the #{a} method"

#{variable} is the same thing as variable.to_s but you can do it within and only within double quotes.
 
... this is really ticking me off, ive tried both ways, but it still just returns nothing, not even 0...

here's my current code im working with (i left my method of the text in comments just in case):

Code:
    self.contents.draw_text(32, 130, 100, 45, "Secret Two")
    self.contents.draw_text(140, 130, 100, 45, "Location: ")
    self.contents.draw_text(320, 130, 80, 45, "Status: ")
    if $game_switches[26] == false
      self.contents.draw_text(225, 136, 80, 32, "Unknown")
    end
    if $game_switches[26] == true
      self.contents.draw_text(225, 136, 95, 32, "The Bunny Hole")
    end
    if $game_switches[27] == false and $game_switches[28] == false
      self.contents.draw_text(390, 136, 130, 32, "Uncompleted")
    end
    if $game_switches[28] == true
      self.contents.draw_text(390, 136, 100, 32, "Failed")
    end
    if $game_switches[27] == true
      self.contents.draw_text(390, 136, 125, 32, "Completed")
    end
    #if $game_switches[26] == true
     # $game_variables[7] = @vaultcode
      #self.contents.draw_text(25, 158, 900, 64, "There is a vault behind the picture in The Bunny Hole bar")
      #self.contents.draw_text(25, 182, 900, 64, "the vault code is #{@vaultcode}.")
    #end
    if $game_switches[26] == true
      $game_variables[7] = @vaultcode
      self.contents.draw_text(16, 160, contents.width, 32, 'There is a vault behind the picture in The Bunny Hole bar')
      self.contents.draw_text(16, 184, contents.width, 32, "Your code is #{@vaultcode}.")
    end

this is what happens when the script is run:
http://i92.photobucket.com/albums/l1/sw ... Errors.png[/IMG]


it does the same thing for both ways you said. And yes, there is a value for variable 7, ive double-checked it several times...
 
nil...

odd, i have the variable set in the event i used to call this script... how would i make it the correct value?

EDIT: I used the $game_variable variable in the string, it works!

the reason i had made it = to @vaultcode was because when i used .to_s on the first version i used, it gave me a syntax error, but when i added the @vaultcode variable, it didnt give me an error, it just gave nothing...

thanks for the help, both of you!

EDIT (2): Hey, you should add this to the FAQs ;)
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top