0
\$\begingroup\$

Game Engine: Gamemaker Studio

I am making a game similar to bejeweled. I decided to use an array to control everything. Placing instances of objects themselves in to the array seems to be the best way to accomplish this because then I won't have to check where the square is in the room, and then compare it to where in the array that lines up, and so on and so forth.

However, I can't figure out how to do that. How do I do it?

\$\endgroup\$
1
\$\begingroup\$

While languages such as C and C++ have pointers, Object-Oriented programming language like Java have instance identifiers to reference existing class instances.

Game Maker: Studio provides any existing instance with a built-in variable: id. An instance id is used to "talk to" a particular instance of an object among the others currently in the room.

I'm assuming that by assigning an instance to an array you mean to save a reference to a given object within an array. As you create your gems - for example in a while or for statement - you can add a line to the code block to save the id of the new instance:

var my_inst;
for (var i=0; i<n; i++)
{
    // Other code of yours //
    my_inst = instance_create(dx,dy,obj_Jewel);
    array_id[i] = my_inst.id;    // OR: array_id[i] = my_inst;
    // Even more code
}

Of course, you can expand this code to make it work with 2D arrays if you need to, even with data structures.

You can save an instance id by asking for its variable, but remember you get its id right when it is created by using the function instance_create(), as it is the return value of the function.

Once you get all your ids in the array, you can perform all the game logic as you will.

\$\endgroup\$
  • \$\begingroup\$ I cannot stress more the issue of gamemaker.info/en/manual/403_05_instances and the quote "Note that the assignment of the instances to the instance id's changes every step so you cannot use values from previous steps.". A far better way to do this is to have an array that the jewel types sit within. It will also be more memory efficient. I don't have a manner of implementing this though, so feel free to take it as a free thought and run with it if it inspires you. Great answer though! If it weren't for that "feature", this would be awesome! \$\endgroup\$ – The Great Duck Jun 8 '17 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.