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] testing if a variable is a string or numeric value

Status
Not open for further replies.
Ok, now how do I do this?

Code:
if [i]variable1[/i] is a number and [i]variable2[/i] is a string
  do this
else
  do that
end

This might fix another problem I'm having....
 
use the <object>.is_a?(<class>) method

is_a? checks if <object> is an instance off <class> or a class that inherits from it

for example to check if its a number use this

variable.is_a?(Numeric)

and to check if its a string use this

variable.is_a?(String)

Numeric is the parent class of all numbers Integer or Float and String is the class of strings (characters, words, etc.)
 
ok, that is good info, unfortunately, it did not help. maybe if I was more specific:

Code:
          if $game_map.check_event(x, y).is_a(Numeric)
            return
          else
            $game_player.find_path(x,y)
          end

gives the error:

Code:
undefined method `is_a' for #<Array:0x145f170>
when there is no event there, and
Code:
undefined method `is_a' for 3:Fixnum
when event with the id of 3 is there.

Note: variables 'x' and 'y' are defined in the two lines before the posted code.
 
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