Ruby/RGSSx questions that don't deserve their own thread

Is it okay to combine scripts? By that I mean scripts of similar function but different outcomes that do not satisfy the intended result, but combining them gets the right results.

Not including scripts where they have snippets/fixes/compatibility fix from other scripters (not from the creator), but taking a portion of the script and putting it in another (im not intending to share it without prior permission from the creators, rather for my own use in my games)
in most cases, i'd say your fine as long as the credit is given. But a few scripters have a no modification line in their terms
Hey guys~ I can't seem to recall the call that checks if an event is already on a specific tile, or the call that returns the ID of the event on a specific tile.

Any help would be great!
This code will allow you to use the call on-tile?(x,y) for all characters, such as events..

class Game_Character   def on_tile?(x,y)    self.x == y && self.y == y  end end
You could then use something like this...

$game_map.events.find { |ev| ev.on_tile?(1,2) }Which will return the item it finds, or nil if it cant find any event on the tile in co-ords x=1,y=2.

Not sure if i made that sound more complex than required ^_^
Aaah thanks for the quick reply! I found it right before I saw this x____x

It was in game_interpreter:

$game_map.event_id_xy(x, y)

Thanks again though really appreciate it~
lol tis all good ^_^

I didnt even think to check for a default method lol
Is there a way via a Ruby command to add a feature to an actor once a game has started? If so, how?
Not via a command, because it'd be reset as soon as you closed the game and reopened.


Look for my Dynamic Features script.
This code will allow you to use the call on-tile?(x,y) for all characters, such as events..

class Game_Character   def on_tile?(x,y)    self.x == y && self.y == y  end end You could then use something like this...

$game_map.events.find { |ev| ev.on_tile?(1,2) }Which will return the item it finds, or nil if it cant find any event on the tile in co-ords x=1,y=2.
Not sure if i made that sound more complex than required ^_^
There's already a method that does exactly that.
seita already pointed that out 😉
Is there a more concise way to write this conditional statement?

if @room[1] == 2 && @room[2] == 2 && @room[3] == 2 && @room[4] == 2For example, something like:

if @room[1..4] == 2would be nice if it worked.
(1..4).to_ary.any? {|id| @room[id] == 2}
would probably do it. But I'm not really familiar with using ranges, so there might be a neater way.
Number.between?(1, 100)

is the native ruby way 🙂
How would that solve the "how do I test 4 numbers and ensure they're all 2?" question


Which alerts me to my error - my solution would tell you if any ONE of them were 2, not all of them. Try this instead:

Code:
!(1..4).to_ary.any? {|id| @room[id] != 2}
How would that solve the "how do I test 4 numbers and ensure they're all 2?" question

Which alerts me to my error - my solution would tell you if any ONE of them were 2, not all of them. Try this instead:

!(1..4).to_ary.any? {|id| @room[id] != 2}
I think it is !(1..4).to_a.any? { |id| @room[id] != 2 } 🙂
Another way to do it would be to use:

if @room.include?(2)

What this does is it checks to see if any id's in the array are equal to the value two.

Edit: Just realized you want to check if all those id's values are equal to two. In that case my method won't work. :/
I think it is !(1..4).to_a.any? { |id| @room[id] != 2 } 🙂
Help File said:
Object


The superclass of all classes. Defines the general behavior of objects.


to_ary


Called internally when an object's implicit conversion to an array is required.
to_a might work, but to_ary is what the help file says.
You can't .to_ary a range.
That isn't how #to_ary should be used. It's used to (implicitly) convert array-like objects to an array. Do ranges act like arrays? No. For example, you can't even call #[]. Of course, ranges can be (explicitly) converted to arrays with #to_a.

Also, #to_ary isn't defined as an instance method of Range. You don't even have to call #to_a, since Range includes Enumerable, so #any? can simply be called directly. But, as the method name already says, #any? is also wrong here. Neok wants to know if all four values are equal to 2. 

Code:
(1..4).all? { |i| @room[i] == 2 }# or@room[1..4].all? { |e| e == 2 }# or@room[1, 4].all? { |e| e == 2 }
Forget that the help file documents Ruby and use the official documentation.
They do slightly different things: to_s vs. to_str (and to_i/to_a/to_h vs. to_int/to_ary/to_hash) in Ruby, Why does to_a and to_ary behave differently in subclasses of Array?

Original thread: https://forums.rpgmakerweb.com/threads/ruby-rgssx-questions-that-dont-deserve-their-own-thread.150/