 Manage the inventory of your game with binary variables

There are many ways to manage the inventory for your game. The most common is to create an ‘array’ or a list of all your data. But it could be made in a more optimal way, simply by using a numerical variable, treating it as if it were binary. How do you do that? Read on!

Use of binary variables when we code

Using simple arrays is still an option, explained with a good example in the official Game Maker forums. But we could also use binary variables. YoYo Games offered an introduction about them in their tech blog. If such post didn’t help you, here’s a brief explanation, without going into too technical details.

How does the binary system work?

We know the system works with ones and zeros. To represent numbers, we put them in a specific order, like this:

`000` = 0
`001` = 1
`010` = 2
`100` = 4

`1000000` = 64

And so, incrementally. How do we represent number 5? In binary it would be 101. If we look at it, it’s the sum of 1 (001) and 4 (100). Makes sense, right? ^_^

An example of binary variables in use

So, using the powers of 2 that binary numbers provides us, we have a number of properties and characteristics that serve us for many purposes. One of them is exemplified by YoYo Games using keys and doors.

We have doors with a binary variable called `door_id`, and we assign Create event ( ) to its own identification number. That number must always be a power of 2. For example: `door_id=1`, `door_id=2, 4, 8, 16, 32, 64,` etc., for each different door-object.

Now, imagine we have another key-object able to open one or more doors. How do we associate them? Creating a `key_id` variable, where its value would be the sum of all the desired `door_id`. For example, if the variable is `key_id=43` it will open doors with `door_id 32, 8, 2, and 1`. And best of all, with a value of 43, it will open those doors ONLY. How would we manage it? With a simple ‘if’ condition.

If we write:

`if( (key_id & door_id) !=0 ) {`

It will be sufficient to check if it is included. As an example, it can be used in the Collision event ( ) between both objects.

Inventory with a binary variable

And now, your inventory example. How to use it? In a very similar way to the example of the doors and the keys. We will have multiple objects with a unique id. Remember that they must be power of 2 (1, 2, 4, 8 …). Now the object containing all the active elements `obj_inventory_controller`, for example, or the same player object `obj_player` will have a `total_inventory` variable. What do we do when adding a new item? Simply add the `id_inventory` to `total_inventory`. For example, if you have a sword with id=1, and a potion with a 64 value, inventory will be 65. What if we add another? We sum its value. What if we eliminate or use an element? We subtract it.

Do you want to display all the items we have? You can show all the possible elements, and activate the desired ones with a simple `if (id_inventory)` and deactivate those not included in the condition (or not show them).

Thus, with a variable and some simple operations we can manage complete inventory in a simple way. If you have other suggestions on the use of binary variables, they are welcome in the comments!

1 Comment
© 2019 DB&R Game Studio — GoDeeper's creators!