YoYo Games Wiki

Flexible Coding

From YoYoGames Wiki

This article (or section) may need to be wikified to meet The YoYo Games Wiki quality standards.
Please help improve this article, especially its categories, and wiki-links.

This tutorial works with...

Game Maker

Most people have not heard of the phrase "Hard Coding" or what it means. However, it is relevant wherever there is a programming language, such as Basic or GML. Hard coding is when your code is not flexible when other changes are made.

Examples

If a game is made where 10 objects are created on the right hand side of the screen throughout your game, the screen is 640 pixels wide by 480 pixels high, the objects are 32 x 32 pixels, and you created the objects like so:instance_create(x,y,object1), instance_create(x,y,object2), instance_create(x,y,object3)... etc...

Because the creator wants it on the right, then x would be 640 (the width of the screen), but they also need to take the width of the sprite into consideration (32 pixels), so they would need to subtract 32 from 640 and this will leave 608, so you would get:

instance_create(608,y,object1)
instance_create(608,y,object2)
instance_create(608,y,object3)
...
...

This will create the objects on the right-hand side. However, if the width of the screen is changed or the width of one of the objects, because they've hard coded (or put an absolute value (608) in x, they'd have to go through your code changing those 10 values. If they later don't like the changes they've made and decide to change something again, they would once again have to go back through their code.

Since they made a slight change in one place, they need to go through their entire program changing other values, therefore hard coding is not recommended for certain things, it would be much better to have something like this:

instance_create(room_width - sprite_width,y,object1)
instance_create(room_width - sprite_width,y,object2)
instance_create(room_width - sprite_width,y,object3)
...
...

or even:

rightside = room_width - sprite_width
instance_create(rightside,y,object1)
instance_create(rightside,y,object2)
instance_create(rightside,y,object3)
...
...

So, no matter the width of the screen, or the width of the sprite (object), you'll never need to change the code as room_width - sprite_width will automatically position the object when changes are made!

Content from GMKB, was used to expand this article. The content was contributed by nickydude.