The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: shintashi on May 07, 2011, 09:01:35 PM

Title: Text input for loop?
Post by: shintashi on May 07, 2011, 09:01:35 PM
I haven't tested this yet, but I was wondering if someone could check it for errors, because I'm not even sure I'm doing this right.


for i in 0..25
a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
g = i + 65
if Input.repeat? (g)
@my_variable += a[i]
end
end


I never trust for loops to work.
Title: Re: Text input for loop?
Post by: ForeverZero on May 08, 2011, 01:23:30 AM

a =%w { a b c d e f g h i j k l m n o p q r s t u v w x y z }
(65..90).each {|i| @my_variable += a[i] if Input.repeat?(i) }


This does the same thing. There are a few subtle differences between for..do loops and using brackets, but the bracket form is how I have always scripted.
Title: Re: Text input for loop?
Post by: Zeriab on May 08, 2011, 04:58:52 PM
Assuming you have a non-standard Input.repeat? method which gets the key press according to... it's Virtual-Key Code (http://msdn.microsoft.com/en-us/library/dd375731%28v=vs.85%29.aspx)?... it looks ok (ugly due to lack of proper indention though)

I would suggest changing the range to 65..90 as ForeverZero suggest and use the .chr method. Here is an example which hopefully can help you on your way:
str = ''
for i in 65..90
  str += i.chr
end
print str




@ForeverZero:
It definitely does not do the same thing.
Using a literal array is a good idea as you get a tighter and easier to read code.
Note that %w does not interpolate while %W does so using capital W would be a functional equivalent. As interpolation is not required %w is a better choice.

Syntax wise parentheses () are used for the array rather than brackets {} and no spaces must be between %w and (.
Fixing the literal array syntax problems the snippet can look like this:
a = %w( a b c d e f g h i j k l m n o p q r s t u v w x y z )
(65..90).each {|i| @my_variable += a[i] if Input.repeat?(i) }

A word of advice is to always test run code snippets before presenting them as you'll find errors such as these which you can be fixed easily and quickly while they may cause more costly confusion for the people you are trying to help. ;)

The second line is more troublesome because the logic rather than the syntax is wrong. Variable g had a purpose because the number check for in Input.repeat? is different from the index. Maybe adding nil will give an error, maybe it won't. That depends on the type @my_variable is.

*hugs*
Title: Re: Text input for loop?
Post by: ForeverZero on May 08, 2011, 11:38:28 PM
I didn't test it, for that I am sorry. I'm sure shintashi could have figured it out with one second that I added a space where there shouldn't have been, and get the basic idea.

Thank you for your lesson.  I need to learn Ruby.
Title: Re: Text input for loop?
Post by: shintashi on May 14, 2011, 04:22:39 AM
oh, I got a version partially working a while ago, although I'm only sometimes able to find things like "spaces". I make errors all the time, especially in SDK.

Right now I'm trying to cope with "C" interference. If I want to use the spacebar for a command, and not have the letter "C" act like a spacebar, what do I have to do?
Title: Re: Text input for loop?
Post by: pacdiggity on May 14, 2011, 05:06:20 AM
Really, you'd either have to define your own input module and refer to that or alter the existing input module - which you shouldn't do.
I'm currently on a computer that doesn't have RMs, so I can't show you what I mean. I saw a command in a script somewhere that has exactly what you want in it. I'll find it later.
Title: Re: Text input for loop?
Post by: ForeverZero on May 14, 2011, 05:51:13 AM
You could use the Win32API class to get keyboard input. I am can't remember all the exact ranges off the top of my head, but I'm sure google could answer you quickly enough. Here would be a quick example though just to show the basics how it works. Just place this little scriptlet anywhere in your editor outside of a class. I'm sure you can figure out how to simply include it in your loop.


    @input = Win32API.new('user32','GetAsyncKeyState','i','i')
    loop {
      Graphics.update
      (0..500).each {|i|
        if @input.call(i) & 0x01 == 1
          p i.chr
          # Obviously instead of "p" to show the letter, you can add the letter to an array
          # in your script.
          break
        end
      }
    }
Title: Re: Text input for loop?
Post by: pacdiggity on May 14, 2011, 06:07:54 AM
Alternatively, you could do that ^.
You should do that.