• 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!

GameMaker Objects Sub_Image * SOLVED *

M

Mallard8

Guest
Hi all,
I have a sprite (Green Dragon) to which I am going to add 9 sub images (same image different colouring)
The object is dragged in to the room 10 times.
If I add the following to the objects draw event I think it would work but how do I get the x and y of each of the objects when they are placed in different positions?
[CODE}
for (var cnt = 0; cnt < 10; cnt++)
{
draw_sprite(obj_Dragon,cnt,x,y);
}
[/CODE]
 

Xor

@XorDev
You could try creating a new object (which I'll refer to as "obj_Draw") then you can have this object draw the dragon sprites. That way you draw all the dragons only once. The obj_Draw's Draw Event code can look something like this:
Code:
for (var I = 0;I < instance_number(obj_Dragon); I++)
{
    var Inst = instance_find(obj_Dragon,I);
    draw_sprite(Inst.sprite_index,I,Inst.x,Inst.y);
}
This loops through all the instances of the dragon in the room and draws it with it's instance sprite, x and y. It also works if you create more or few instances of the dragon.
 

TheouAegis

Member
Your sprite is called obj_Dragon or the dragon's object is called obj_Dragon? You need to first figure out what you're working with, because right now it looks like you're going to be in for a serious world of coding-hurt.
 
  • Like
Reactions: Xor

TheouAegis

Member
Well your draw event had "draw_sprite(obj_Dragon...)". As I said, you're in for a world of hurt if you keep making mistakes like that. You draw sprites, you don't draw objects.

And if you actually have 10 obj_Dragon in the room, then there's no reason to even use the FOR loop, is there? Set the image_index or whatever in the Create Event of your obj_Dragon based on the number of instances of obj_Dragon.

image_index = instance_number(obj_Dragon) - 1;
image_speed = 0;
 
M

Mallard8

Guest
@TheouAegis I think I may have mislead you, draw_sprite is not in my draw event, I was asking if that is how I should do it.
Clearly it is not.
 

TheouAegis

Member
Well aside from using the wrong resource reference, the concept is fine enough if you do like Xor suggested. But as I said, you could just as easily automatically assign the correct image_index for each dragon to use and let them handle their own drawing.
 
D

drowned

Guest
You need to back up a step or two and follow some beginner tutorials. It's clear to me that you aren't at a point yet where you're ready to ask questions like this and have the responses be meaningful.
 
M

Mallard8

Guest
@TheouAegis Thanks again, I do understand what you say about image_index and i can see how it works in the Create event of obj_Dragon.
What I can't see is how it gives each obj_Dragon a different sub image as there is only one obj_Dragon, placed in ten locations within the room.
 
D

drowned

Guest
No, you don't. If you set image_index in the create event without setting image_speed to zero, you will not notice a difference.

What I can't see is how it gives each obj_Dragon a different sub image as there is only one obj_Dragon, placed in ten locations within the room.
It does not give each obj_Dragon a different sub image... you said it yourself, there is only one obj_Dragon. You are literally drawing 10 sprites, manually. For real, go read the manual and follow along with some beginner tutorials first.
 
M

Mallard8

Guest
@Xor Seems your suggestion was the one, I placed the code in the Draw_Event of obj_Dragon and I now have 10 different coloured Dragons.

@drowned I did set image_index in the create event and I did set the image_speed to zero. When run all dragons were the same colour.
I know you are trying to help but the manual isn't always that clear. Sometimes a piece of code pertaining to the question asked is of more help if you step through it to see why it does what it does. I also appreciate just writing code for every question asked is not going to help a user to understand coding much either.
 
  • Like
Reactions: Xor

TheouAegis

Member
image_index = instance_number(obj_Dragon) - 1;

If that doesn't work in GMS2, then I'll just shake my head at YYG because that has worked for 10 years.

The code doesn't care how many objects are in the room, it cares how many instances of said object are in the room. Each instance gets created one at a time, so each instance of obj_Dragon would have image_index of 0, 1 , 2 , 3 , and so on up to 9. Each one would be unique until you destroy one and make a new one, which could result in two or more with the same sprite at that point.

And if that code still isn't work for you, go through your dragon object and make sure you didn't put in a draw event that is drawing a specific image index. It's possible your draw event is overwriting any code that assigns it a unique image index.
 
M

Mallard8

Guest
@TheouAegis Thanks for coming back to me on this thread, maybe you will need to shake your head at YYG.
I have placed all events from Dragon below as there is not a lot of it. The knight collides with the dragon and a net is dropped over the dragon you answer three questions and when you get three right the dragon is placed in a cage and you move on to the next. It's an education project for early years learning.
CREATE EVENT
Code:
/// @description Set variables
obj_dragonet.visible = false;
dragon_array = array_create(10, -1); // initialise an array of values, defaulting to -1 for each array entry
dragon_pos = 0; // This will hold the current position to add a dragon to, and since we haven't got any dragons yet, we set it to 0
vsp = 0;
grv = 0.3;
message= noone;
DESTROY EVENT
Code:
/// @description Place Dragon in cage

with (obj_Cage)
{
if dragon_pos < 10
    {
    dragon_array[dragon_pos] = other.sprite_index;
    ++dragon_pos;
    }
}
STEP EVENT
Code:
/// @description Collision with dragonet

if (place_meeting (x - 100, y, obj_Knight)) ||  (place_meeting (x + 100, y, obj_Knight))
{
obj_dragonet.x = x;
obj_dragonet.y = y;
obj_dragonet.visible =true;
}
    
if (place_meeting (x,y,obj_dragonet))
{
    if (message == noone)
    {       
        message= instance_create_layer(x,y,"Plus",obj_Plus);
    }

}
DRAW EVENT
Code:
/// @description Draw coloured Dragons

for (var cnt = 0; cnt < instance_number(obj_Dragon); cnt++)
{
    var Inst = instance_find(obj_Dragon, cnt);
    draw_sprite(Inst.sprite_index, cnt, Inst.x, Inst.y);

}
 
D

drowned

Guest
So wait... why would instance_number(obj_Dragon) - 1; work at all if there is only one dragon object instance in the room?
 
M

Mallard8

Guest
You need to back up a step or two and follow some beginner tutorials. It's clear to me that you aren't at a point yet where you're ready to ask questions like this and have the responses be meaningful.
So wait... why would instance_number(obj_Dragon) - 1; work at all if there is only one dragon object instance in the room?

This is what I was wondering as well?
Still it's all working now and that's what matters.
Thanks for all the replies.
 
So wait... why would instance_number(obj_Dragon) - 1; work at all if there is only one dragon object instance in the room?
If there is one dragon in the room, instance_number(obj_dragon) returns 1.

Subtract 1 from that gives 0, which is assigned to image_index.

Therefore the first dragon in the room gets assigned the image_index of 0.

This works if you are creating the dragons yourself in code one by one using instance_create_layer.

However, I think if you have placed 10 dragons in the room editor before running the game, instance_number returns 10, which might be what's happening in your case.
 
M

Mallard8

Guest
@IndianaBones Thanks for that, as I am sure that is exactly what is happening.
I did place 10 dragons in the room editor before running the game.
Glad it is finally solved.
 

TheouAegis

Member
@drowned Refer to original post:

The object is dragged in to the room 10 times.
Yes there is one object but there are 10 instances. As I said in every one of my posts, it doesn't matter how many objects there are, what matters is how many instances there are. Since there are 10 instances, it should have been assigning a unique image_index for each instance of obj_dragon. Unless his draw a event was interfering with it, it would appear to be an issue in studio 2 that suggests instances are all created at the same time when the room is compiled. However, that also runs contrary to instances not existing at the start of a room.
 
D

drowned

Guest
ah, I missed the part about it being dragged to the room ten times. Instances, however, are not all created at the same time "when the room is compiled". This of course makes no sense.
 
Did you remember to take out your Draw event ever?
One issue I see with the dragon object draw event is that it seems to be drawing the sprites for all 10 dragons.

So each dragon is drawing 10 sprites, and there are 10 dragons, so that is 100 draw_sprite calls.

Probably not nearly enough to affect the frame rate, but still not something you should be doing @Mallard8

The code you have in the draw event of your dragon as listed above would be better suited to go in a single controller object.

Yes there is one object but there are 10 instances. As I said in every one of my posts, it doesn't matter how many objects there are, what matters is how many instances there are. Since there are 10 instances, it should have been assigning a unique image_index for each instance of obj_dragon. Unless his draw a event was interfering with it, it would appear to be an issue in studio 2 that suggests instances are all created at the same time when the room is compiled. However, that also runs contrary to instances not existing at the start of a room.
ah, I missed the part about it being dragged to the room ten times. Instances, however, are not all created at the same time "when the room is compiled". This of course makes no sense.
Just to re-confirm what I had discovered previously, I made a test object and in its create event printed out the instance_number() for that object. Then I placed 5 of these objects in the room via the room editor.

The debug output is as follows:

id :100007oTest Count: 5
id :100008oTest Count: 5
id :100009oTest Count: 5
id :100010oTest Count: 5
id :100011oTest Count: 5
I don't know if GMS 1.4 behaviour is different. But in GMS 2, instance_number returns the count for all instances that have been placed in the room editor when called from the Create Event.

Which is different than what I would have expected as I thought the docs said in the order of events that the instances are created one by one and as they are created, their Create Event is executed.

But apparently, all the objects are created first, then their Create Event is performed.

EDIT: Here is the quote from the Event Order section of the docs that made me think it is one by one:

Create Event followed by the Instance Creation Code of each instance - so as each instance is created, it will run first its Create event and then its Instance Creation Code before moving on to the next instance to be created
 

TheouAegis

Member
Yeah, that's a shame. Because it worked so well in older versions.

Other idea is:

var n = instance_number(obj_Dragon);
for(var i=0; i<n; i++)
if id == instance_find break;
image_index = i;

But i hate loops like that.
 
M

Mallard8

Guest
Well this thread aroused some interest to say the least, still that's what a community should be about.
I have removed the Draw Event for obj_Dragon and the code is now in a controller object.
All is working fine now on to the next problem.
 
Top