• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Picking and grouping instances of objects

Farflame

Member
I'm coming from Construct 3, where the idea of picking and grouping instances is fairly common. I'm trying to work out if GMS2 has something similar, but from the tutorials and documents I've read, I don't see anything other than manual solutions.

So the idea is fairly simple, say I had a tank body and a turret. Each tank has one turret. When you 'pick' the tank, you automatically 'pick' the turret, so in C3 you do something like..

Pick tank..... (by position / order / whatever)
Rotate turret.

You don't have to find / pick the turret because it's picked when the tank is picked.

If there's no built-in option like this, then I assume what I'd do is to create one of each and do something like, in the creation of the tank, store the ID of a turret. But even then, I'm not sure how I link the two together, i.e, even when I'm working with the tank body, I can get the ID of the turret, but how do I work with that turret, just from it's ID?

I'm clearly in the C3 flow and I need to adjust my thinking to the new style.
 
D

DarthTenebris

Guest
What exactly are you trying to achieve? Are you trying to move things around in the room editor, or in game? Either way, my approach would be to have 2 subimages in the tank sprite, separating the body from the turret. The tank object would use the body, and in the draw event, draw itself and then the turret, therefore alleviating the need for a turret object. The draw functions allow for rotating the sprite, just make sure that you set the sprite origin correctly.

Hope I helped :)
 

TailBit

Member
well, you could just tell the tank to create the turret on top in its create event:

create event:
GML:
turret = instance_create_layer( ... );
turret.owner = id; // give it a owner variable in case it need to refer some info back to the tank
turret.image_angle =180;
so after placing the tank in the room you could access its "creation code":
Code:
turret.image_angle = 20;
if you needed it to have a specific angle
 

Biscotto

Member
I don't know if I understand what you're asking, but if you want to change values of one instance by interacting with another instance, you can solve it in many ways.
If you have the instances id, you can use one of these simple methods:
Taking as an example that: other_instance = id of the other instance
GML:
other_instance.x = x;

//or

with (other_instance) {
    x = other.x;
}
In the first case, set the x value of the instance mentioned, equal to the x value of the instance that is executing the code.

In the second case, perform the same operation, but in a different way (you can look in the "with" and "other" manual for more details).
 

Farflame

Member
Thanks guys. Lots of helpful information here.

It's really that I just need to adjust my thinking away from the C3 style. I keep trying to solve problems in a C3 way, by grouping objects together. But then I realise that I don't know how to access one when I'm working with the other. But I think the examples above will get me there. It's one of those where I'm likely to go off in the wrong direction and then come back a few hours later and have to scrap everything. But I think I just need to experiment a bit with the above ideas until it clicks properly.

Oh, and I just noticed that there's a parent/child relationship that will probably solve some of it :)
 

Farflame

Member
The biggest thing that's not working in my head right now is how to pick ONE instance from a bunch. I'm trying to use a pool of instances. I set them off screen and then when I want one, I look for instances at a certain location (off-screen) and then return ONE (or create a new one if there isn't one there). But I think it sends a list of all instances at that location and if I then start setting values to 'it', I'm actually setting values to all of them?

I think I'm getting there, luckily the debugger is pretty nice so I'm following my code through step-by-step to see what's going on and I think I'm getting the hang of it.
 
Last edited:
D

DarthTenebris

Guest
The biggest thing that's not working in my head right now is how to pick ONE instance from a bunch. I'm trying to use a pool of instances. I set them off screen and then when I want one, I look for instances at a certain location (off-screen) and then return ONE (or create a new one if there isn't one there). But I think it sends a list of all instances at that location and if I then start setting values to 'it', I'm actually setting values to all of them?
You can use instance_place_list() to check if there are any instances left in your pool. If there is, then pick a random one from the list. Otherwise, create a new instance.

Hope I helped :)
 

Farflame

Member
Yeah I ran it through the debugger and apparently if you do a 'return varinstance' from a function, it does only return one? i.e I run through all instances, if one is available for selection, I just return the instance. The debugger does seem to be suggesting that that only returns the single instance. So that works fine :)
 
Top