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.

String.split method

I am writing a script that takes a string, which would be a sentence or something of the sorts. My script requires me to cut the sentence up into individual words. So I simply use the split method for the string. This puts it into an array, which is fine. However, when I compare a newly created string with a word in the array they never return true. So split is doing something to the string when it cuts it up, but I can't figure out what. Does anyone have any suggestions on what I should do? Should I use another method to cut the string up? Or what? Alright well thanks everyone for your help. If you need me to clarify anything let me know.
 
Well, if you don't post your script we can't help you much, but basically :

Code:
string = "I am just so sexy."
p string.split(' ') # returns : ["I","am","just","so","sexy.]
p string # returns : "I am just so sexy."
that's what the split method does..
 
Ok, here is my script.

Code:
def drawText(x, y, width, height, string)
    #This is a basic drawText. Looping needs to be added so that it wont cut up words and
    #alignment needs to be added(left, center, right)
    r = 0 #r is the current row being written to
    c = 0 #c is the current column being written to
    words = string.split
    rect = Rect.new(0, 0, 8, 16)
    for i in 0...words.size
      specialCharacters = []
      spcounter = 0
      count = 0 #number of special characters in the current word
      counter = words[i]
      counter.gsub!(/[&]([0-9]+)/) do
        count += 1
        specialCharacters.push($1)
      end
      word = words[i]
      word.gsub!(/[&]([0-9]+)/) {Temp.new.specialCharacters[$1.to_i]}
      if word == nil
        word = words[i]
      end
      if height >= r * rect.height #check to see if there is anymore room on the bitmap
        if width < c * rect.width + (word.size - count)#check to see if there is enough room on the current row for the next word
          c = 0
          r += 1
        end
        #for some reason special characters count as 2 when getting the size of the string
        for j in 0...word.size - count
          if word[j] == 195#special character
            point = Temp.new.characters[specialCharacters[spcounter]]
            spcounter += 1
          else
            point = Temp.new.characters[word[j]]
          end
          self.blt(x + (c * 8), y + (r * 16), RPG::Cache.picture("CharacterMap"), Rect.new(point.x, point.y, rect.width, rect.height))
          c += 1
        end
      end
      #end of word. Add a space between the words if it is not the last word
      if i != words.size - 1
        point = Temp.new.characters[32]
        self.blt(x + (c * 8), y + (r * 16), RPG::Cache.picture("CharacterMap"), Rect.new(point.x, point.y, rect.width, rect.height))
        c += 1
      end
    end
  end
Code:
class Temp
  attr_reader   :characters
  attr_reader   :specialCharacters
  def initialize
     @characters = {
  65 => Point.new(0,0), 66 => Point.new(8,0), 67 => Point.new(16,0), 68 => Point.new(24,0),
  69 => Point.new(32,0), 70 => Point.new(40,0), 71 => Point.new(48,0), 72 => Point.new(56,0),
  73 => Point.new(64,0), 74 => Point.new(72,0), 75 => Point.new(80,0), 76 => Point.new(88,0),
  77 => Point.new(96,0),78 => Point.new(104,0), 79 => Point.new(112,0), 80 => Point.new(120,0),
  81 => Point.new(128,0), 82 => Point.new(136,0), 83 => Point.new(144,0), 84 => Point.new(152,0),
  85 => Point.new(160,0), 86 => Point.new(168,0), 87 => Point.new(176,0), 88 => Point.new(184,0),
  89 => Point.new(192,0), 90 => Point.new(200,0), #First line of characters
  97 => Point.new(0,16), 98 => Point.new(8,16), 99 => Point.new(16,16), 100 => Point.new(24,16),
  101 => Point.new(32,16), 102 => Point.new(40,16), 103 => Point.new(48,16), 104 => Point.new(56,16),
  105 => Point.new(64,16), 106 => Point.new(72,16), 107 => Point.new(80,16), 108 => Point.new(88,16),
  109 => Point.new(96,16),110 => Point.new(104,16), 111 => Point.new(112,16), 112 => Point.new(120,16),
  113 => Point.new(128,16), 114 => Point.new(136,16), 115 => Point.new(144,16), 116 => Point.new(152,16),
  117 => Point.new(160,16), 118 => Point.new(168,16), 119 => Point.new(176,16), 120 => Point.new(184,16),
  121 => Point.new(192,16), 122 => Point.new(200,16), #Second line of characters
  192 => Point.new(0,32), 193 => Point.new(8,32), 194 => Point.new(16,32), 196 => Point.new(24,32),
  198 => Point.new(32,32), 199 => Point.new(40,32), 200 => Point.new(48,32), 201 => Point.new(56,32),
  202 => Point.new(64,32), 203 => Point.new(72,32), 204 => Point.new(80,32), 205 => Point.new(88,32),
  206 => Point.new(96,32), 207 => Point.new(104,32), 208 => Point.new(112,32), 209 => Point.new(120,32),
  210 => Point.new(128,32), 211 => Point.new(136,32), 212 => Point.new(144,32), 214 => Point.new(152,32),
  140 => Point.new(160,32), 217 => Point.new(168,32), 218 => Point.new(176,32), 219 => Point.new(184,32),
  220 => Point.new(192,32), 45 => Point.new(200, 32), #Third line of characters
  224 => Point.new(0,48), 225 => Point.new(8,48), 226 => Point.new(16,48), 228 => Point.new(24,48),
  230 => Point.new(32,48), 231 => Point.new(40,48), 232 => Point.new(48,48), 233 => Point.new(56,48),
  234 => Point.new(64,48), 235 => Point.new(72,48), 236 => Point.new(80,48), 237 => Point.new(88,48),
  238 => Point.new(96,48), 239 => Point.new(104,48), 240 => Point.new(112,48), 241 => Point.new(120,48),
  242 => Point.new(128,48), 243 => Point.new(136,48), 244 => Point.new(144,48), 246 => Point.new(152,48),
  156 => Point.new(160,48), 249 => Point.new(168,48), 250 => Point.new(176,48), 251 => Point.new(184,48),
  252 => Point.new(192,48), 223 => Point.new(200,48), #Fourth line of characters
  48 => Point.new(0,64), 49 => Point.new(8,64), 50 => Point.new(16,64), 51 => Point.new(24,64),
  52 => Point.new(32,64), 53 => Point.new(40,64), 54 => Point.new(48,64), 55 => Point.new(56,64),
  56 => Point.new(64,64), 57 => Point.new(72,64), 44 => Point.new(80,64), 46 => Point.new(88,64),
  32 => Point.new(96,64)}#Fifth line of characters
  @specialCharacters = {
  192 => "À", 193 => "ÃÂ
 
Well RGSS uses UTF-8, not pure ASCII... But i´m not sure on why would you draw your text that way... Are you loading text inside txt/whatever files and trying to draw them?
 
I did not know that RGSS used UTF-8. I don't know that much about UTFs honestly...I need to read up on them. Anyways, I do plan on reading text from a text file, but as of right now that is not the case. Is there a better way to go about displaying the text? I'm eager to learn ^^
 
nope, doesn't look like it. I just tried that and then checked to see what the value of changing À into an int. I still got 195 and it is still two bits(I believe that is why "À".size returns 2.
Just did another little test.
Code:
print $KCODE
This prints UTF8. So I tried...
Code:
$KCODE = "ASCII"
  print $KCODE
It printed 'NONE'. So maybe the concept can work, but you cannot set $KCODE like that.
 

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