Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Help with understanding and modding a script

  1. #1

    Default Help with understanding and modding a script

    Ok, so I'm working on understanding and writing a couple of simple scripts. I'm getting close enough to writing and modding my own scripts that this is getting exciting, but I'm running into tons of errors when I try to do things myself because I'm making assumptions and guesses without really understanding and it's causing me to run into very frustrating blocks.

    As an example, I downloaded a script that by itself looks very simple and very easy to understand. I have a few questions though and I'm hoping to get a hand in figuring it out: If this is someone's script and you don't want me posting it up as an example and/or modding it, please send me a PM and I'll delete this post. I'll post the script and ask my question next to the lines I have questions about.

    Here's the script, called "Instrumentskilllevel":

    # Will level your instrument skill while you rest
    # If you leave the room you started the script in, script will kill on the next PLAY attempt
    # Edit to config instrument

    ##########
    # Config #
    ##########

    instrument = "flute" <----I understood this, no problems same with styles

    styles = ["Joyful","Mournful","Soft","Fast","Slow","Intense" ,"Jaunty","Flamboyant","Aimless","Somber","Inspiri ng"]

    ##########
    ##########
    ##########

    echo "################################################# ##########"
    echo "################################################# ##########"
    echo "Leveling your #{instrument} skill until you leave this room"
    echo "################################################# ##########"
    echo "################################################# ##########"

    restRoomId = Room.current.id <----this is just to set the room I'm in as the "rest room" right?
    fput "stow all"
    fput "get my #{instrument}" <---Is the reason the # is there is because it's a variable?
    style = 0; This is to set the style at 0, so that later on when it goes "Style +1" it will start at joyful, right?
    while Room.current.id == restRoomId This is what stops the script if I move, but why two ='s?
    fput "play my #{instrument} #{styles[style]}" <---Why is it #{styles[style]}?
    style = (style + 1) % styles.length <---what does % and styles.length tell you? I assume it somehow tells the script to wait until the style is finished playing?
    sleep 330
    end

    So if I wanted this script to end after a specified time or after two or three styles, could you point me in the right direction for changing it efficiently? I know a lot of you who are experienced at this would want me to learn, and so do I. I appreciate your help.

  2. #2

    Default

    this is just to set the room I'm in as the "rest room" right?
    Yes.

    Is the reason the # is there is because it's a variable?
    Yes, when using variables in a (f)put command you have to enclose the variable in #{}.

    This is to set the style at 0, so that later on when it goes "Style +1" it will start at joyful, right?
    Close, it starts at 0, not 1. So at 0 it will start at Joyful and when it does style + 1 it will move on to Mournful.

    This is what stops the script if I move, but why two ='s?
    When you're comparing a variable you use ==, when you're setting a variable you just use one =.

    Why is it #{styles[style]}?
    styles is the variable where it lists joyful and mournful and all of that, so if you wanted to play "soft" it would be styles[2] (because remember it starts at 0.) style (no 's' so it's a different variable) is basically the 2 in the previous example, as style is increased it changes how you're playing the instrument.

    what does % and styles.length tell you? I assume it somehow tells the script to wait until the style is finished playing?
    I'm not sure about this one. I'm still a noob when it comes to ruby scripting
    Last edited by Tgo01; 06-15-2013 at 09:00 PM.

  3. #3

    Default

    Yes, restRoomId is setting a variable equal to the room ID of where you are.

    Yes, the #{instrument} is because it refers to a variable.

    My best guess is that Ruby uses 0 as a first index, i.e., styles[0] is actually joyful, because it plays #{styles[style]} before it adds 1 to the style index.

    You need to use == because that's how you test for equality. x = 5 assigns the value 5 to x. x == 5 is a logical condition -- it returns 1/true if x is 5, and 0/false is x is not 5.

    #{styles[style]} means take the nth (or, more appropriately, the style-th) value from the styles array variable.

    I don't know Ruby enough to understand what that exact % syntax does, but styles.length is literally a value that tells you how many elements are in the styles array (in this case 11 [actually I don't know if the length attribute returns number of elements or highest index value, which would be 10 because 0 is used]).

    As far as "telling the script to wait" or whatever, no, that's what the sleep 330 is for. It just pauses for 5.5 minutes and continues. That line is definitely there to say something like, "add 1 to the style counter, BUT don't go beyond how many styles there actually are," so that the script can run forever without breaking (it should go back to style=0 after style=10). I just don't know precisely how it behaves.
    Last edited by Bobmuhthol; 06-15-2013 at 09:08 PM.
    Razzle them. Dazzle them. Razzle dazzle them.

  4. #4

    Default

    I'm not sure about exiting the script after a certain amount of time but to end the script after 3 styles you could do this:

    Code:
    exit if style == 3
    And add it right before fput "play my #{instrument} #{styles[style]}"
    Last edited by Tgo01; 06-15-2013 at 09:23 PM.

  5. Default

    % is the symbol for a modulo operation. It basically means divide the two and give the remainder. In this script, 0 to 10 are valid indexes of the styles array. After using style 10 (Inspiring), the next line evaluates like so:
    style = (style + 1) % styles.length
    style = (10 + 1) % 11
    style = 11 % 11
    style = 0
    Get Lich - Vote for Gemstone (topmudsites.com)

  6. Default

    To end after a certain time, you could do something like this:

    Code:
    ...
    end_time = Time.now + 3600 # 3600 seconds = 1 hour
    while Room.current.id == restRoomId This is what stops the script if I move, but why two ='s?
         fput "play my #{instrument} #{styles[style]}"
         style = (style + 1) % styles.length
         sleep [ 330, (end_time - Time.now) ].min
         break if Time.now >= end_time
    end
    # You can add something to do here before the script exits
    Get Lich - Vote for Gemstone (topmudsites.com)

  7. #7

    Default

    Quote Originally Posted by Tillmen View Post
    % is the symbol for a modulo operation. It basically means divide the two and give the remainder. In this script, 0 to 10 are valid indexes of the styles array. After using style 10 (Inspiring), the next line evaluates like so:
    style = (style + 1) % styles.length
    style = (10 + 1) % 11
    style = 11 % 11
    style = 0
    I'm confused how this script works then. What does the % command do if the first number is smaller than the second number? Does it just ignore it?

  8. Default

    The #{ } syntax is more general than fput and variables. It's used to insert code into any double quotes. In Ruby, all code "returns" something, so just placing the variable name inside the #{ } gives the value of the variable. You could also do something like this:

    foo = "Good #{if Time.now.hour < 12; 'morning'; else; 'evening'; end}, I #{['hate', 'love'][rand(2)]} you."
    echo foo
    put "say #{foo}"
    send_to_script 'lnet', "chat #{foo}"
    Get Lich - Vote for Gemstone (topmudsites.com)

  9. Default

    Quote Originally Posted by Tgo01 View Post
    I'm confused how this script works then. What does the % command do if the first number is smaller than the second number? Does it just ignore it?
    3 / 11 is 0 with a remainder of 3, so 3 % 11 is 3
    Get Lich - Vote for Gemstone (topmudsites.com)

  10. #10

    Default

    Quote Originally Posted by Tillmen View Post
    3 / 11 is 0 with a remainder of 3, so 3 % 11 is 3
    Ohhhh I think I get it now. Since 11 doesn't go into 3 the answer would be 0 and the remainder is whatever the first number is.

    I was trying to figure this out with a calculator but apparently that's the wrong way to go about it.

Similar Threads

  1. Replies: 0
    Last Post: 10-06-2019, 05:42 PM
  2. Understanding
    By SellerOfSouls in forum Wanted
    Replies: 8
    Last Post: 06-08-2017, 07:28 AM
  3. Failing to temper and not understanding why.
    By sperry85 in forum Wizard
    Replies: 13
    Last Post: 05-30-2014, 07:35 PM
  4. Something im not understanding
    By SolitareConfinement in forum Bard
    Replies: 1
    Last Post: 06-18-2008, 02:34 AM
  5. Understanding Help Please
    By FinisWolf in forum Game Mechanics
    Replies: 11
    Last Post: 02-03-2006, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •