Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Request: Which tile?

Shin!

Member

Hi.
I would like a script that could inform me what tile is "used" on the current character position.

I will try to explain better.
Let's say I make a simple map (17x13) using the default RTP. Just filled half with that green grass and the other half with that yellow grass.
Now, I want some event to happen only when the hero is on any yellow grass.

Then, you probably think: "Just add some events on the yellow grass part."
Well... I could do it like that but what about huge maps? With huge areas? Putting many events would cause a great lag and I can't use the anti-lag script.

Another option: "Add a common event that checks the hero position and fires a event when on specific coordinates."
Well... that would work and wouldn't cause any lag but I would have to change the specific coordinates every time I re-draw the map.

So, my best option? I script that tells me which tile the hero is on.

Well, I hope I was clear enough.
 

poccil

Sponsor

That's not actually as easy as it sounds.  It involves first checking the tile ID of the player, and checking the tile ID against some value. 

The script below checks the tile ID of the tile that the player is currently standing on.

Code:
TOPLAYER = 2
MIDDLELAYER = 1
BOTTOMLAYER = 0
def getCurrentTile(layer)
 if $game_map.map_id != 0
    tile_id = data[$game_player.x, $game_player.y, layer]
    tile_id = 0 if !tile_id
 end
 return 0
end

However, tiles in RGSS 2 are not exactly intuitive.  In your case, in the default tileset, green grass can have tile IDs anywhere from 0x0BC0 to 0x0BEF (in hexadecimal), and yellow grass can have tile IDs anywhere from 0x0D40 to 0x0D6F.  Furthermore, grass is placed on the middle layer of the map (above the ground but below objects).  Thus, to check which grass the player is on, you use this script below:

Code:
def isOnGreenGrass?
  currentTile=getCurrentTile(MIDDLELAYER)
  return (currentTile>=0x0BC0 && currentTile<=0x0BEF)
end
def isOnYellowGrass?
  currentTile=getCurrentTile(MIDDLELAYER)
  return (currentTile>=0x0D40 && currentTile<=0x0D6F)
end

The functions "isOnGreenGrass?" and "isOnYellowGrass?" return true if the player is on green or yellow grass, respectively.

(Please note that the scripts below were not tested.)

I hope this helps.
 

Shin!

Member

Yeah... I imagine... I never thought it was easy...
Although I am a programmer myself I know nothing of RGSS so thank you for trying to help me out...

I tried to test your code... well I got a error undefined local variable or method 'data' for data[$game_player.x, $game_player.y, layer].
I don't know what to do... :(

By the way... how do you know the codes for tiles ID? How do you know which hexa range represents each tile?

Thanks again...
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top