Here's the super-fun binary method.
Arrange your subimages in order (from 0 to 15, I presume) so that when the number is expressed in binary, each bit represents whether or not there's another block next to this one in a certain direction.
0 (0000) = no blocks surrounding this
1 (0001) = a block above this
2 (0010) = a block below this
3 (0011) = blocks above and below this
4 (0100) = a block to the left of this
5 (0101) = blocks above and to the left of this
etc. and so on.
Now when the block is created, you can use those functions to check if there are other blocks around this one, and bit-shift each value.
I'd recommend using
position_meeting() or
place_meeting() instead of instance_position() or instance_place(). The "meeting" functions return only 0 (no) or 1 (yes), whereas the "instance" functions return the
id of the instance it finds (which isn't necessary for this). Also, you might have to wait one step after the Create event (using an alarm) because when an instance is created, all the other ones around it might not be created yet.
image_index = place_meeting(x,y-1,objWall) + (place_meeting(x,y+1,objWall)<<1) + (place_meeting(x-1,y,objWall)<<2) + (place_meeting(x+1,y,objWall)<<3)
That code makes the image_index equal 1 if there's a block above, then adds 2 if there's one below, adds 4 if there's one to the left, and adds 8 if there's one to the right.
example here[edit]
I went back and tried this in the Create event, without an alarm. Seems to work fine.
You will notice the block of 4 in the upper-right has a dot in the center. That's a result of how the sprites were drawn (to include a small dot for corners), not a bug with the code.