Conditional between/with 2 numbers?

Discussion in 'RGSS3 Script Requests' started by Christopher Riel, Oct 25, 2019.

Thread Status:
Not open for further replies.
  1. Christopher Riel

    Christopher Riel Villager Member

    Messages:
    16
    Likes Received:
    0
    First Language:
    Spanish
    Primarily Uses:
    RMVXA
    Hello, again, investigate more about the conditionals. I wanted to experiment and know ..
    Can you make a conditional that verifies 2 numbers?

    for example, we have this value
    Code:
    $game_actors [1] .face_index = 1 (shows that the Index is equal to 1)
    I want the conditional to work if it is between 2 numbers, like 5 to 10
    or that works, if it is in a specific number like 10 and 20

    Example:
    Code:
    @>Conditional Branch: $game_actors[1].face_index = (5 ~ 8)
          @>Execute Action if Index is between 5 and 8 (numbers like 5, 6, 7, 8)
          @>
    @>Else
          @>Execute Action if Index is different than 5, 6, 7, 8
          @>
    @>Branch End
    and
    Code:
    @>Conditional Branch: $game_actors[1].face_index = (10 & 20)
          @>Execute Action if Index is in 10 or 20 (doesn't include number in between)
          @>
    @>Else
          @>Execute Action if Index is different than 10 or 20 (doesn't include number in between)
          @>
    @>Branch End
    It should also be mentioned that random variables work in this way:

    Example:
    Code:
    $game_variables[1] = rand(10)
    so that the variable can have a random number.
    and I think it would still be possible to verify a variable with 2 numbers.

    well, would this be possible to verify 2 numbers in a conditional?
     
    #1
  2. A-Moonless-Night

    A-Moonless-Night WINTER IS COMING Veteran

    Messages:
    505
    Likes Received:
    362
    Location:
    New Zealand
    First Language:
    English
    Primarily Uses:
    RMVXA
    In Ruby, you have the equals symbol and the 'is equal to' symbol. One sets a variable and one checks if a variable is equal to X.
    Code:
    $game_actors[1].face_index = 1
    The code above sets actor 1's face index to be 1.

    Code:
    $game_actors[1].face_index == 1
    The code above checks if actor 1's face index is equal to 1.

    There is a method in Ruby for integers called between? You can use it like so:
    Code:
    $game_actors[1].face_index.between?(5, 8)
    It's a shorter way of writing:
    Code:
    $game_actors[1].face_index >= 5 && $game_actors[1].face_index <= 8
    To check if a variable is either X or Y, you can do this:
    Code:
    $game_actors[1].face_index == 10 || $game_actors[1].face_index == 20
    You could also do this, which is shorter:
    Code:
    [10, 20].include?($game_actors[1].face_index)
    It basically makes an array of numbers and checks if any of them are equal to actor 1's face index.

    You can view some more on Ruby operators here: https://www.tutorialspoint.com/ruby/ruby_operators.htm
     
    #2
  3. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,640
    Likes Received:
    2,966
    Location:
    Philippines
    First Language:
    Tagalog
    All these years I've been using the long method, not knowing such a thing exists... XD
     
    #3
    A-Moonless-Night and Oddball like this.
  4. Heirukichi

    Heirukichi Veteran Veteran

    Messages:
    1,256
    Likes Received:
    519
    Location:
    Italy
    First Language:
    Italian
    Primarily Uses:
    RMVXA
    Just to add something to this: the "between?" method is defined in the Comparable module. As such, it can be called on any instance of a class that includes the said module, even if it is not an Integer.

    @Christopher Riel Talking about Ruby syntax, there is also a nice documentation inside RPG Maker VX Ace help file as well, you can simply press F1 and read it. It does not come with exercises to aid you in the learning process, but a good portion of the language is still included and properly explained.
     
    #4
    Engr. Adiktuzmiko likes this.
  5. Christopher Riel

    Christopher Riel Villager Member

    Messages:
    16
    Likes Received:
    0
    First Language:
    Spanish
    Primarily Uses:
    RMVXA
    @Heirukichi Yes, I have it a lot in mind, that it is a bit difficult for me to interpret Ruby syntax, because the documentation is in English, and let's say I still don't know the language well. But I try to learn little by little. Thanks anyway c:


    @A-Moonless-Night Really thank you very much, it worked! :D
     
    #5
  6. Kes

    Kes Global Moderators Global Mod

    Messages:
    20,970
    Likes Received:
    10,663
    First Language:
    English
    Primarily Uses:
    RMVXA

    This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

     
    #6
Thread Status:
Not open for further replies.

Share This Page