Jump to content

- - - - -

A Simple Trick To Orientating Blocks

  • Please log in to reply
9 replies to this topic

#1 Blake


    GMC Member

  • GMC Member
  • 604 posts
  • Version:GM:Studio

Posted 21 February 2012 - 07:39 AM

  • Title: A Simple Trick To Orientating Blocks
  • Description: Automatically orientates your blocks depending on the surrounding blocks.
  • GM Version: GM8
  • Registered: No
  • File Type: .gmk
  • File Size: 13.3kb
  • File Link: Download
  • Required Extensions: None
  • Required DLLs: None

When creating platform or top-down games, you often want your blocks (or platforms or walls etc.) to show different sprites or tiles depending on the surrounding blocks. Some solutions to this problem that I have seen involve essays of code that repeat place_meeting functions over and over for every different possibility of surrounding blocks. With this simple trick that I will show you though, this is not necessary. If we arrange the sprite subimages in a particular order, we can then check all four sides of each block for another block, and increase the image_index by certain amounts depending on the surrounding block positions, and this results in the correct subimage. Similarly, if we arrange the tiles in a particular horizontal order, and instead increase a variable by certain amounts depending on the surrounding block positions, we achieve the correct tile. This may be difficult to comprehend in writing, so take a look at the following scripts and see if that helps to make it more clear.

The .gmk file shows how to achieve this with both sprites and tiles. You are free to use the scripts/example in your game. When doing so, please note that in order for it to work correctly your sprite subimages must be in the same order as mine (if using sprites) and your tiles must be in the same order as mine and in one single horizontal row (if using tiles).

For sprites, we can use the following script:

name = argument0;
width = argument1;
height = argument2;

image_speed = 0;
image_index = 0;

if place_meeting(x-width,y,name) image_index += 8;
if place_meeting(x+width,y,name) image_index += 4;
if place_meeting(x,y-height,name) image_index += 2;
if place_meeting(x,y+height,name) image_index += 1;

For tiles, we can use the following script:


name = argument0;
bg = argument1;
width = argument2;
height = argument3;
count = 0;

if place_meeting(x-width,y,name) count += 8;
if place_meeting(x+width,y,name) count += 4;
if place_meeting(x,y-height,name) count += 2;
if place_meeting(x,y+height,name) count += 1;


Here is a screenshot of the example in action. On the left we have sprites and on the right we have tiles.

Posted Image

I hope this example is useful! (It better be... it took so damn long to put together! Oh, what the hell, I have no life anyway :tongue:)
  • 2


#2 icuurd12b42


    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 21 February 2012 - 11:41 PM

We have a few of those, but this has a tile method. Very nice.
  • 0


ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

Now I'm squirming not to hit that reply button

#3 @Alex@


    GMC Member

  • GMC Member
  • 4734 posts
  • Version:Unknown

Posted 22 February 2012 - 12:19 AM

I'd hate expanding this for smooth blends of terrain types. The combinations grow far to quickly for my tastes, then again it would save a bunch of time on a large scale project. Nice and simple example.
  • 0

2mma14z.jpg 2nu6efl.jpg 28whvdt.jpg

#4 Thagrahn


    GMC Member

  • GMC Member
  • 33 posts
  • Version:GM8

Posted 27 February 2012 - 05:46 AM

Seems to be designed around being just one tile or sprite set of 16 images, so seems to have limits on what it can handle.

I thing having to draw out and figure out the number of tiles and images for other types of things would take a lot longer to begin with.

One thing that would help this go further would be if there was a way to apply tile sets to multiple layers, and only check that layer.

Other wise, one tile set and several terrain sprites would be needed.

Example Idea: Lets say you wanted to do a room with an autumn theme.

First would be the grass tile that actually covers the full room.
Second would be a tile set for a different type of grass.

If you couldn't use tile sets on other layers, you would have to use sprites with high layer numbers for things like the Leaves, Paths, and Dirt.
  • 0

#5 Desert Dog

Desert Dog

    GMC Member

  • GMC Elder
  • 6409 posts
  • Version:Unknown

Posted 27 February 2012 - 06:52 AM

Good job on this, thanks for sharing.
  • 0
HTML5 games for mobile:
HexDogs Bugz Burn! Captain George Golfing Block Memory

Games for Androids
*NEW* Word Dog - Published by Dangerous_Dave

Code: General Array Functions - GM-S friendly. sorting, shuffling. Includes a quicksort.
Use the quicksort to sort ds_lists 10-18 times faster than ds_list_sort()!

#6 Blake


    GMC Member

  • GMC Member
  • 604 posts
  • Version:GM:Studio

Posted 27 February 2012 - 07:27 AM

Thanks for all the positive comments! :happy:

I'm well aware that there is limitations to this with regard to having more than 16 subimages in order to achieve more smooth edges etc. I've thought about expanding this to cover that, but to be honest, a grand total number of downloads that barely scrapes into the double figures isn't enough motivation for me :tongue:
  • 0


#7 icymx


    Patrick Brett

  • GMC Member
  • 800 posts
  • Version:GM8

Posted 18 March 2012 - 03:17 AM

Worked great for me. Thanks Blake! :D
  • 0
Security is mostly a superstition. It does not exist in nature,
nor do the children of men as a whole experience it.
Avoiding danger is no safer in the long run than outright exposure.
Life is either a daring adventure, or nothing.
To keep our faces toward change and behave like free spirits
in the presence of fate is strength undefeatable.

- Helen Keller

#8 The Rock Abdo

The Rock Abdo

    GMC Member

  • GMC Member
  • 82 posts
  • Version:GM:Studio

Posted 02 April 2012 - 10:42 AM

great tutorial .. thanks :thumbsup:
  • 0


#9 CptGT99


    GMC Member

  • New Member
  • 3 posts
  • Version:GM8

Posted 25 April 2012 - 04:04 PM

works good, thanks!
  • 0

#10 trianglegames


    GMC Member

  • GMC Member
  • 36 posts
  • Version:Mac

Posted 16 May 2012 - 11:36 PM

I'm gonna try it out!
  • 0