Restricting yourself by only following the default code from the engine is a fine way of limiting your own potential in coding.
You can do a search for
.merge in the default code too, that's not used there either, yet it's still a wonderful method of the
Hash class and is ready to be used for those who find it useful. I could list tons of other great methods never used in the default scripts, but I think my point is clear here.
😛
The class of a property object got nothing to do with how you can set it or what you can set it to. It's just that, a property that can be changed with the
property_name=(value) method. You can even set that
src_rect property to an integer number if you want to, but of course, it will break the
Sprite class, because that expects a
Rect object.
You seem to mix two things here.
The
src_rect property is a property of the
Sprite class, which happens to expect a
Rect object as it's value. The
set method is a method in the
Rect class. They are completely separate things. You can use the
set method on the
Sprite class'
src_rect property if you want, obviously, since it's a
Rect object. But that doesn't mean that you can't directly set the
src_rect property of the
Sprite class to a new
Rect object.
You surely didn't work with these properties, because if you would have, you would know how they work.
You may ask why didn't I just use that
set method on the property itself, and the answer is simple: because I don't have to, it's easier this way, and my code is much shorter and way more dynamic this way. Here is an example:
Spoiler
Code:
module ExSetup
Cmds = {
:back => {
:img => "item1",
:x => 87, :y => 12, :ox => 87, :oy => 12,
:src_rect => Rect.new(0,0,0,24),
:tone => Tone.new(0,0,0,255), :blend_type => 1,
:color => Color.new(255,255,255,181),
},
:name => {
:txt => "Command <index>", :w => 175, :h => 24,
:x => 87, :y => 12, :ox => 87, :oy => 12,
:src_rect => Rect.new(0,0,0,24),
:tone => Tone.new(0,0,0,255),
},
}
end
class CmdVis
def initialize(data)
@data = data
@index = 0
init_commands
end
def init_commands
@cmds = {}
10.times do |i| # Just some random way of making more commands
@data.each do |sym,props|
ikey = [i,sym]
@cmds[ikey] = Sprite.new
apply_props(@cmds[ikey],props,i)
@cmds[ikey].y += i * 32
end
end
end
def apply_props(sp,props,i)
props.each do |prop,val|
case prop
when :w, :h
# Non-existent properties used in other parts of the code can be skipped
# easily here.
next
when :img
# Image loading, simplified.
sp.bitmap = Cache.picture(val)
when :txt
# Text drawing, simplified.
sp.bitmap = Bitmap.new(props[:w],props[:h])
sp.bitmap.draw_text(0,0,props[:w],props[:h],process_txt(val.clone,i),1)
else
# And this part lets the user of the script setup any valid properties
# while making the code way shorter and easy to read.
sp.send(prop.to_s+"=",val)
end
end
end
def process_txt(tx,i)
tx.gsub!("<index>") { i }
return tx
end
def update
if Input.trigger?(:UP)
@index -= 1
@index = @cmds.size - 1 if @index < 0
elsif Input.trigger?(:DOWN)
@index += 1
@index = 0 if @index > @cmds.size - 1
end
if Input.trigger?(:LEFT)
trgs = @cmds.select {|ky,sp| ky[0] == @index }
trgs.each {|ky,sp| sp.src_rect.width = 0 }
elsif Input.trigger?(:RIGHT)
trgs = @cmds.select {|ky,sp| ky[0] == @index }
trgs.each {|ky,sp| sp.src_rect.width = 175 }
end
if Input.trigger?(:C)
dispose
end
end
def disposed?
return @disposed
end
def dispose
@cmds.each_value {|sp| sp.bitmap.dispose; sp.dispose}
@disposed = true
end
end
test = CmdVis.new(ExSetup::Cmds)
loop do
Input.update
Graphics.update
test.update
break if test.disposed?
end
Barely 100 lines (most of it is not even related to the command images made with it), yet it can make a full command list with any amount of images and with any type of image property settings.
It's just a quick example made in a few minutes. There are many more situations where setting these properties directly is much easier for the coder and for the code too.
Try to test this snippet with and without the RGD, and check the difference yourself, now for real.
Just change that image setting to an image from the "Graphics/Pictures/" folder, and it's ready for testing.