Well, one useful skill that you can learn is how to trace the definition of a variable back to its source. So, to use your example:
@tile_id is set on line 80 of Sprite_Character:
@tile_id = @character.tile_id
@character is set on line 24 of Sprite_Character:
@character = character
The local variable character is an argument passed when initializing a new instance of the Sprite_Character class (line 22):
def initialize(viewport, character = nil)
The character = nil means it will default to nil if no argument for character is passed. When it is passed, however, we see from line 20 that it is a Game_Character object
# character : character (Game_Character)
Going in to Game_Character, you find several instances of the line:
@tile_id = 0
Now, it looks like a dead end, since obviously if tile_id were always only 0, it wouldn't be a variable. So the first thing to check is if @tile_id is writable from without the class. Going to line 17, we see:
attr_reader :tile_id # tile ID (invalid if 0)
This means that you cannot alter @tile_id from without the class.
Spoiler for Side Note :
(Aside: having attr_reader is essentially equivalent to including a method in the class like this:
def tile_id return @tile_id end
attr_writer is essentially:
def tile_id= (value) @tile_id = value end
attr_accessor is both)
Ok, so this seems like a dead end, but it isn't. Game_Character is a superclass after all. Game_Event, Game_Vehicle, and Game_Player are all subclasses of Game_Character, which in its simplest reduction means that each of the subclasses includes all the methods of the superclass. This means that if @tile_id is altered in any of those, then that is what is passed from that instance of the class. So @tile_id is not mentioned in either Game_Player or Game_Vehicle, but it is in Game_Event. At line 90, we see this again:
@tile_id = 0
But at line 99 we see:
@tile_id = @page.graphic.tile_id
So what is @page. As we see on line 143:
if new_page != @page # Event page changed?
@page is an event page object. Here we turn to the help file. We search event page, and it comes up with RPG::Event::Page. There we look up graphic:
graphic The event graphic (RPG::Event::Page::Graphic).
We then go to RPG::Event::Page::Graphic
Finally we come to:
tile_id The tile ID. If the specified graphic is not a tile, this value is 0.
Thus, if the graphic of an event is from the tileset, the tile_id refers to which tile it is.
Note: This is actually a much longer trace than would normally occur. In truth, the first step you should take when you come across something foreign is to search in the Help File, and trace only if that doesn't answer your question. Tracing is useful for things that aren't in the help file though.
So now that we know that, we can return to the primary problem.
sx = (@tile_id / 128 % 2 * 8 + @tile_id % 8) * 32; sy = @tile_id % 256 / 8 % 16 * 32; self.bitmap = tileset_bitmap(@tile_id) self.src_rect.set(sx, sy, 32, 32)
If you count the number of tiles in any of the TileB and up tiles, you will find there are exactly 256 tiles in each. They don't take much care to separate order of operation in these lines, so the order for the sx calculation is basically:
a = @tile_id / 128 b = a % 2 c = b * 8 d = @tile_id % 8 e = c + d f = e*32
for sy:
g = @tile_id % 256 h = g / 8 i = h % 16 j = i * 32
So let's say we take a @tile_id of 2, which we know should be the sign of a sword and shield on TileB.
for sx:
a = 2 / 128 = 0 b = 0 % 2 = 0 c = 0 * 8 = 0 d = 2 % 8 = 2 e = 0 + 2 = 2 f = 2*32 = 64
for sy:
g = 2 % 256 = 2 h = 2 / 8 = 0 i = 0 % 16 = 0 j = 0 * 32 = 0
So sx = 64, and sy = 0. Thus, these are the x & y coordinates of the tile with that ID. You can try this with others if you like, but the point is that the two lines in question retrieve the x & y coordinates of the tile with that ID.
Then:
self.bitmap = tileset_bitmap(@tile_id) self.src_rect.set(sx, sy, 32, 32)
That retrieves the bitmap of TileB from the cache (you can look at the method tileset_bitmap in Sprite_Character to see how) and the second line takes only the tile as identified by @tile_id.
So there, that's what those lines do. It seems like it takes a long time typing it out like this, but in reality it takes a couple minutes max.
I hope that helps. So yeah, that's what those lines do, and they have no relevance to what you want to do, but hopefully that will help you in the future.