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

@Rikifive

Why don't you simply move the fill's X position to the right depending on the (bar's width - fill width)?

That is the "effective left filling gauge" you wanted.

Admiralac was close enough, but he forgot to subtract the fill width from the gauge's width. That is what needs to be added to the X position of the fill gauge, not the fill's width directly.

In case you want to try it out:

Spoiler
class Window_Base < Window def draw_gauge_left(x,y,w,h,rate,color1,color2) fill_w = [(w * rate).to_i, w].min # Maxing the fill width to the gauge width set. new_x = x + [w - fill_w, 0].max # Setting the fill's X pos based on it's fill rate. contents.fill_rect(x,y,w,h,Color.new(0,0,0,80)) # Gauge back. contents.gradient_fill_rect(new_x,y,fill_w,h,color1,color2) # Gauge fill. endend
This is just a very simple gauge method which fills the gauge from the right to the left with gradients.

Can be used in any window class (if it is put in Window_Base, that is).

Based on the fill rate, we shift the X position of the fill gauge to the right, so that it seems that the gauge fills from the right to the left, but behind the scenes, it still fills from the left to the right, just it's position changes.

You can alter it however you want, to add borders for it by simply playing around with the X, Y width and height properties of the back and fill gauges if you want.

I also have no idea why would anyone alter the Y in that way (meaning that "gauge_y" thing I see everywhere, even in the default methods) inside the gauge method...

There is absolutely no point doing that, adding line_height to it and all those stuffs. It actually breaks the Y position you want to set your gauge at, and you will always have to account for all those stuffs going on with the Y position in the method itself, which makes it pretty annoying.
Oh I see how it was supposed to work - that's clever. Sorry didn't realized --- *late hours*.

Hmm... I'll draw the border via image (in UI) and simply draw these gauges on it - it'll make everything simpler...

Okay, I think everything is clear - and don't worry - this method is going to be one-use only as I don't need that anywhere else...

But I'm wondering - My method allows me to create standard bar ~ you know, something like - if you have HP bar that look like this:

[RED][YELLOW][GREEN], then if you have low HP, then it should be only something around [RED], not [RED][YELLOW][GREEN] squashed into a tiny bar.

So I think I'll stick to my method and changing everything each time isn't a problem for me. Though if it's really bad and you have suggestions, then please tell me about this.

About "gauge_y", well the whole thing worked perfectly for me... I know I have stupid methods of making scripts, but they still work, unless it's really bad and the performance drops. =P

It's seems I'm doing every single thing wrongly... Somehow works, yet wrongly... That tells me - I should never make public-use scripts, nor help anybody with scripting.

Thank you for clarifying, I'll get to that.
That "gauge_y" thing is in the default methods too, so it's not really your fault if you use that.

I used to copy/paste things from the default methods too when I started coding, that's not a problem.

But the more you learn, the more flaws will you see in the default methods. Not really game breaking flaws, mostly just unnecessary things put in some methods, or badly designed methods overall (some of them are damn impossible to edit without a direct overwrite >.>).

I haven't tried your method, I just saw that you wanted right to left bars, and shared a way to do it.

If your method works for you, than it's okay to use it, of course. 🙂

For the color issues, simply putting in some conditions like these:

Spoiler
if rate <= 0.25 # 1/4 or lower fill #set your colors up hereelsif rate <= 0.5 # Half or lower fill #set your colors up hereelse # More than half fill #set your colors up hereend
Into the method should add you some control over the colors.
Well, if you want to draw the gauge color depending on the current HP, you can use :

LOW_HP_COLOR = Color.new(255, 0, 0)NORMAL_COLOR = Color.new(0, 255, 0)SECOND_COLOR = Color.new(0, 0, 255)draw_gauge(x, y, width, rate, rate > 0.25 ? NORMAL_COLOR : LOW_HP_COLOR, SECOND_COLOR)And yeah, if you want to learn more about scripting in RGSS3, there are some quite good tutorials here.Although I learned RGSS3 by looking at video tutorials, which are also made by a guy from these forums, you may prefer text tutorials, because from the video you may not see everything, and it's harder to try things on-the-fly.
I know I can change colors of the bars via conditions. =P But I was saying about the gradient one ~ to make a normal gradient, for example [RED] with [GREEN] so it will give something like this [RED][YELLOW][GREEN] and to not make it squashed when the value is low. You probably know how it looks with these gradients - when the bar is tiny, then it makes a multicolored tiny bar ~ I was saying to make it not move - when the value decreases - then the bar length is decreasing, but the colors stay at place, so when it will go to ~50%, then the [green] part is not moving, but it just end up on the yellow part...

I mean the two colors always stay to the borders of the bar, so if it's short, then one color is very close to the other one. =P

A things to compare, that comes to my mind now are gauges in "Tomb Raider" games. xD

But it's not that important ~ I'll 'use' both' methods and create the appropriate one to my liking.

Thank you for suggestions and help, they're surely useful for me.
Yeah, for that you might want to try out using an image for both the background and for the fill rate.

You make your images, the background will be a simple sprite/bitmap (you can use separate sprites for the back and fill if you need the background to be visible under the fill bar, for example), but the fill bar gets it's rect changed depending on the fill rate.

That way, you can make gradient bars (with infinite colors, if you want) in an image editor, and those will not be shrunk, but instead will be cut down to the correct fill rate. You can also use any shapes this way too.

I actually used a right to left MP bar with images in one of my battle HUDs I made (http://postimg.org/image/bv2qrr525/).

If you want something similar, here is the basic method:

Spoiler
back = Cache.picture("gaugeback") rect1 = Rect.new(0,0,back.width,back.height) contents.blt(x1,y1,back,rect1,opa1) fill = Cache.picture("gaugefill") mpw = fill.width * rate mpx = fill.width - mpw rect2 = Rect.new(mpx,0,mpw,fill.height) contents.blt(x2+mpx,y2,fill,rect2,opa2)
There is one important thing to note here thou:

The fill rate image can NOT contain any empty pixels on the right or left side! It will mess up the fill rate display, since the empty pixels are still part of the picture, and the picture's width is used to calculate the correct fill width here.

For that reason, you might need to use a different X and Y position for the background and the fill rate, that's why I used different variables in the code above.

This uses bitmap drawing in a window to draw the background and the fill rate afterwards on top of it. Replace the variables with actual data (or make a method for it and call it with arguments), and it should work the way you want to with the additional bonus of using your own images instead of the plain old and boring default gauges.
OR you can reverse the process of draw_gauge, that means, you call gradient_fill_rect with normal width, and then fill the part of it with the background color, with rate of 1 - original rate, if you know what I mean.
Yeah, for that you might want to try out using an image for both the background and for the fill rate.

You make your images, the background will be a simple sprite/bitmap (you can use separate sprites for the back and fill if you need the background to be visible under the fill bar, for example), but the fill bar gets it's rect changed depending on the fill rate.

That way, you can make gradient bars (with infinite colors, if you want) in an image editor, and those will not be shrunk, but instead will be cut down to the correct fill rate. You can also use any shapes this way too.

I actually used a right to left MP bar with images in one of my battle HUDs I made (http://postimg.org/image/bv2qrr525/).

If you want something similar, here is the basic method:

Spoiler
back = Cache.picture("gaugeback") rect1 = Rect.new(0,0,back.width,back.height) contents.blt(x1,y1,back,rect1,opa1) fill = Cache.picture("gaugefill") mpw = fill.width * rate mpx = fill.width - mpw rect2 = Rect.new(mpx,0,mpw,fill.height) contents.blt(x2+mpx,y2,fill,rect2,opa2)
Spoiler
There is one important thing to note here thou:

The fill rate image can NOT contain any empty pixels on the right or left side! It will mess up the fill rate display, since the empty pixels are still part of the picture, and the picture's width is used to calculate the correct fill width here.

For that reason, you might need to use a different X and Y position for the background and the fill rate, that's why I used different variables in the code above.

This uses bitmap drawing in a window to draw the background and the fill rate afterwards on top of it. Replace the variables with actual data (or make a method for it and call it with arguments), and it should work the way you want to with the additional bonus of using your own images instead of the plain old and boring default gauges.
Oh that's really useful! I was wondering how to do these things, I love customizing, so the images is the best way, thank you for that information!

That important thing isn't a problem, I know it works like this. 😉

OR you can reverse the process of draw_gauge, that means, you call gradient_fill_rect with normal width, and then fill the part of it with the background color, with rate of 1 - original rate, if you know what I mean.
Yup, that's what I did in my method. (When you suggested using "1 - original rate" before, then when I saw how that worked, it gave me that idea.) =P
@sokita:

module KOCKA  module SNIPPET_ENCOUNTER    MAPS = {      1 => [      # On map with iD 1        [1],       # When the switch is ON those troops will encounter        [2],       # Otherwise, those.      ],    }    SWITCH = 1     # Switch indicating is it day or night  endend class Game_Map  include KOCKA::SNIPPET_ENCOUNTER  #--------------------------------------------------------------------------  # * Get Encounter List  #--------------------------------------------------------------------------  def encounter_list    MAPS.keys.include?($game_map.map_id) ? MAPS[$game_map.map_id][$game_switches[SWITCH] ? 0 : 1].collect{|t|@map.encounter_list[t - 1]} : @map.encounter_list  endendSo, if you have an encounter list with :

1. Slime - Day

2. Bat - Night

3. Wolf - Day

4. Sponge - Night

on the map iD 10 you would set up the MAPS array like:

MAPS = {  10 => [    [1, 3],    [2, 4],  ],}And, if the switch is ON that should signify the day.
Oh, there it is! Why we need to substract t from @map.encounter_list? "t" is refer to index, isn't it?
@sokita

If I didn't subtract 1 from t, you would need to write [0, 2] instead of [1, 3]

Does it work?
Nope, it still not working when the switch ON and give "undefined method for region_set..." when the switch is OFF.
Nope, it still not working when the switch ON and give "undefined method for region_set..." when the switch is OFF.
It didn't work for me without adding the troops to the map in "map properties". When I added them, it worked perfectly, try doing that.

The one thing that would be nice would be the possibility to select map AND region. But I think that would be a complete script request.

I'll see if I can do that 😀
@sokita

Yep, you should add troops to map properties first.

Thrown the same error for me.

First add to the troops something like:

1. Slime (Slime should appear when switch is ON)

2. Bat (when switch is OFF)

Then set up the MAPS hash like:

MAPS = {  your_map_id => [    [1],  # Because Slimes appear at day    [2],  # Because Bats appear at night  ]}Do you understand now?

@Torqus

It's already possible.
@Torqus

It's already possible.
Oh you mean, in map properties? Oh, right, common sense, it's a built-in config. I didn't realize it would work with this. Sometimes the solution is where you least expect it to be.
I found a script a while back that lets you have text boxes pop up as parallel processes with icons for which character was saying it, what script was it?

edit: found it.
Hey, I want to find out what entry a class is in an array.

So lets say I create a class;  class = Class.new

Then I add that class to an array;  array.push(class)

I then call a def in the class that checks the array to see if it exists in the array, then assigns the number the class is in that array.

def what_array_number_am_i?

  array.each{ |class| if class = self ; @number_of_class = numerical_entry_of_class_in_array ; end }

end

How would I achieve this?
Not quite certain on if this is what you want but:

Lets say you have an array like so:

@a = ["Class_A", "Class_B", "Class_C"]@index = 0def class_index(arg)   @index = @a.index(arg)endclass_index("Class_B")puts @index #=> 1*note that when pushing a class into an array there wont be any "" marks.
array.index(object)
@sokita

Yep, you should add troops to map properties first.

Thrown the same error for me.

First add to the troops something like:

1. Slime (Slime should appear when switch is ON)

2. Bat (when switch is OFF)

Then set up the MAPS hash like:

MAPS = {  your_map_id => [    [1],  # Because Slimes appear at day    [2],  # Because Bats appear at night  ]}Do you understand now?
Oh, yes, it working when the switch ON, I forget I've set same troops for switch ON and map properties, so it looks same, ahaha, I am sorry. How about region ID?

This is what I got when I am trying to puts $game_map.encounter_list

encoun10.png


BTW, is it possible to be done without using any script or snippet?
How about region ID?
I already asked him about that, here's an example:

In map 3 I have 4 encounters: slimes and bats for day (switch off) and rats and spiders for night (switch on), when you set the troops in "map properties", don't select whole map, choose regions. I have slimes and rats in region 1 and bats and spiders in region 2. That's it. The snipped will work.

And no there's no way to do that without the script, but it's a very light one, you shouldn't be afraid of using it (I know I will).

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