Legacy GM Create Event vs instance id

N

NeonBits

Guest
in create event, is it ok to write something like
Code:
 br = inst_A0FFC24;
 bry = br.y;
inst_A0FFC24, an instance already existing in the room when created. Another object is created later after room start. I thought using its create event to store the original position of one instance.
 

chamaeleon

Member
in create event, is it ok to write something like
Code:
 br = inst_A0FFC24;
 bry = br.y;
inst_A0FFC24, an instance already existing in the room when created. Another object is created later after room start. I thought using its create event to store the original position of one instance.
Sure, it's fine, if you want to, they're just numbers. If you don't like the assigned name in the room, you know you can change it to whatever you want, like, say "br" (which in this case would eliminate the need for the additional variable)?
 

TheouAegis

Member
An instance doesn't actually exist until it has run its create event. So even if it was placed in the room manually, it wouldn't matter.
 
N

NeonBits

Guest
Sure, it's fine, if you want to, they're just numbers. If you don't like the assigned name in the room, you know you can change it to whatever you want, like, say "br" (which in this case would eliminate the need for the additional variable)?
Ah, yeah, didn't came to my mind. Maybe because I think that if I change the name, the other parts of the codes won't know anymore what it is.

An instance doesn't actually exist until it has run its create event. So even if it was placed in the room manually, it wouldn't matter.
Ah, I think it's ok. I use an objectA for an intro and an objectB in the end for when the action begins. The instance must have been activated. I was tired when I coded that level and it works fine. But when the game arrives there, bug, when the new object is made. GM thinks the instance is not there. I changed the code a little; same error. But GM now said it's on line9 when now it refers to "br = inst_A0FFC24". Something must be duplicated. Or invisible. Or missing. I don't know. Ah, yeah, after editing the code, even when loading the room, sometimes, the error reappears. Kinda confusing.
 

chamaeleon

Member
Ah, yeah, didn't came to my mind. Maybe because I think that if I change the name, the other parts of the codes won't know anymore what it is.


Ah, I think it's ok. I use an objectA for an intro and an objectB in the end for when the action begins. The instance must have been activated. I was tired when I coded that level and it works fine. But when the game arrives there, bug, when the new object is made. GM thinks the instance is not there. I changed the code a little; same error. But GM now said it's on line9 when now it refers to "br = inst_A0FFC24". Something must be duplicated. Or invisible. Or missing. I don't know. Ah, yeah, after editing the code, even when loading the room, sometimes, the error reappears. Kinda confusing.
Have you tried using show_debug_message() in create and destroy events to ensure that you see messages coming out in the order you expect?
 
N

NeonBits

Guest
I've made a new object and pasted the codes; nothing; at least GM points the right line. After last night, I've remade the code piece by piece; it works until one object is created with instances_Id in its create event. I don't know if the problem is because it's a duplicated room bigger than the first room made in the project and this is 1.4.1772. I could use show_debug but events follow correctly.

Is it ok to write in an object's create event:
Code:
if (place_meeting(objA.x, objA.y, inst_B0AAEF8C))
{y = objB.y + 20;
 x = objB.x;}
else
if (place_meeting(objA.x, objA.y, inst_A1BBCD9E))
{y = objB.y + 40;
 x = objB.x;}
else
if ((objA.y < inst_C2F434G4.y) && (objA.y > inst_DF573J2.y)) edit: yes ".y" is in the code; sorry, was tired and forgot to add it in this post.
{y = objB.y + 45;
 x = objB.x;}
else
etc
Worked perfectly at first.
Unable to find any instance for object index '101022' name '<undefined>' at gml_object_objA_create_event_1(line35)
 
Last edited by a moderator:
N

NeonBits

Guest
Well, I was going "yay, finally" when checking again the code, I was about to write a message here, then took another look; nope, it's all good; "place_meeting" doesn't require ".y" heh, I've almost added it; the other part has it. Sorry, I've forgot to add it in this post; must have been tired. So it's ok to add "place_meeting" and "else" in "create" event. K, thanks, that point is now clear.
 
Last edited by a moderator:

TheouAegis

Member
place_meeting() is just fine in the create event... if there's something already there to meet. When an instance is created by another instance using instance_create(), then more than likely any instances it could look for a collision with using place_meeting() are already in the room. But if you had 100 instances all put in the room manually using the room editor, then at the start of the room there's no guarantee that place_meeting() would work properly in the create event -- not because there's anything wrong with place_meeting() being used in the create event, but because there's no guarantee by the time the place_meeting() code is run that any other instances have already been created in time for the place_meeting() check to find anything.
 
N

NeonBits

Guest
Thanks. But the room has already started and the event happens in the middle. After editing the code, I've found that the same problem returns with another object. At one point, one objectA is created; once it exists and, if it exists, objectB is created; if objectB exists, objectC is created. ObjectB looks for a position based on specefic instances that come created with the room (bug; can't find them). Then objectC can move away from objectB's position. I got tired last night and renamed the specefic instances_id in room editor. GM reacted to it; when I couldn't see anything, now I see the new instances (A,B,C) deleted just after they come. I don't understand why when it worked fine at first. It's not the original bug but it has the same problem with the instances_id in create event. And the original bug only create one new instance in the room.

Is it ok to write in step event:
Code:
var a1, a2;
      a1 = objectA;
             a2 = objectB;
even if there's no instances of objA and objB in the room?
 
Last edited by a moderator:

chamaeleon

Member
Thanks. But the room has already started and the event happens in the middle. After editing the code, I've found that the same problem returns with another object. At one point, one objectA is created; once it exists and, if it exists, objectB is created; if objectB exists, objectC is created. ObjectB looks for a position based on specefic instances that come created with the room (bug; can't find them). Then objectC can move away from objectB's position. I got tired last night and renamed the specefic instances_id in room editor. GM reacted to it; when I couldn't see anything, now I see the new instances (A,B,C) deleted just after they come. I don't understand why when it worked fine at first. It's not the original bug but it has the same problem with the instances_id in create event. And the original bug only create one new instance in the room.

Is it ok to write in step event:
Code:
var a1, a2;
      a1 = objectA;
             a2 = objectB;
even if there's no instances of objA and objB in the room?
Of course it's ok to write a1 = objectA, etc., if objectA is an object in your resource tree. a1 will not have any connection to any specific existing or future instance of objectA, though, if that's what you're hoping for. All you're doing is assigning the number represented by objectA, perhaps 1, if it's the first object in your resource tree (2 if it's the second, and so on) to a local variable a1, which will then contain the number 1. Writing to or reading from variables through a1 or objectA then generally follows the pattern of writing to all instances' variable and reading from one of them, respectively. If only one instance exist this may work fine, but as soon as you have more than one instance, you may end up with unintended behavior due to modifying multiple instances at the same time or not knowing which instance you're getting a value from. Given the assignment of objectA to a1 you could of course use a1 in an instance_create_layer() or instance_create_depth() call instead of objectA.
 
N

NeonBits

Guest
All you're doing is assigning the number represented by objectA, perhaps 1, if it's the first object in your resource tree (2 if it's the second, and so on) to a local variable a1, which will then contain the number 1.
Ah, the first time at codes, result returned something like that. But in what situation it is useful to know the number behind an object in the ressource tree?
 

chamaeleon

Member
Ah, the first time at codes, result returned something like that. But in what situation it is useful to know the number behind an object in the ressource tree?
I can't speak for when it would be useful to know the number in particular. I try not to care about the numbers at all, I consider them an implementation detail that I don't really look at. It can be useful to know that they are numbers (which lets you know it's something that can be assigned to variables and passed around, as arguments to functions and scripts, etc.), but what the actual value is shouldn't matter one bit.
 

TheouAegis

Member
The problem with knowing the object_index (or any other resource's index) is that's only going to do you any good in a final build. As soon as you make any changes to the resource tree, you risk breaking the program if you base anything explicitly on the resource indices. Saving object_index to an INI file, for example, is a case where your code is based explicitly on a resource index. If the first time the program is run it writes 32 for an object_index, you could load the INI file, fetch that object_index, and recreate an instance of that same object in the room. However, if in your next build you patched it so that a new object was added to the resource tree, when that INI file is loaded, it might create an instance of the wrong object.

Now, you can code around resource indices indirectly just fine as long as you avoid using an explicit basis. For example,

sprite_index = spr_cat_bw + breed;

In this example, spr_cat_bw is the first sprite in a series of sprites and breed is a value from 0 to whatever. Each of the sprites are in order with no other sprites between them. As long as breed is not negative and as long as breed is not larger than the number of sprites in the series, then the sprite_index will be fine. However, this should only be used in a final build or in a program that has no save features which incorporate the breed. If the user could save the value of breed, then you would have an explicit basis and changing the order of sprites in the series around would result in a different sprite being shown for the breed that was saved. It should be noted that "final build" in this case merely refers to the series of sprites; you could patch the hell out of the project without affecting what sprite gets shown as long as you don't change the order of the sprites in the series.

When running little tests where I decide it's better to just toggle between rooms, I indirectly refer to the room IDs.

room_goto(room ^ 1);

That will let me jump between the same two rooms to my heart's content with just that one line.

But in all of these cases, you don't actually ever know the NUMBER behind the resources. That's the nice thing about Game Maker -- you pretty much never need to know the actual numbers. Every resource has its number assigned to a constant -- the resource name. So in most cases, you just need to know the resource's name. In the case of the codes I gave, you also just need to know the relationships between the resources (e.g., which sprite comes first, how far apart the sprites are).

object_index = obj_fatcat + global.breed;
show_debug_message(object_get_name(object_index));

GM needs to know the number, but you don't.
 
N

NeonBits

Guest
Hey, it was instructive and interesting. I know only a bit of gml but feels as if GM saves alot of details. Thanks for the lesson. And thanks to you both for the explanations.

Last night has been active. I went through a few scripts and corrected a few things. Like giving a unique name to each local variable even when not in same condition; found one persistent object had no reset on its counters before changing room; and I could separate in the end one chain of events. Now the game doesn't crash. Great. But now the last level has the same problem that the second level had after editing its scripts to adapt them on the last one (just added "if (room == roomX){} else if (room == roomY){}"); I don't understand when everything was ok for the last level; had to fix things on the second level a few days ago and now the same bug is in the last one when I haven't edited their properties. And the balance of another level seems weaker than before. Ahah, it's a challenge... *!facedesk!*
 

TheouAegis

Member
Typically if a bug exists in one spot, then changing it causes the bug to appear farther down, then the error is usually somewhere else, such as an external/global variable being set incorrectly.
 
N

NeonBits

Guest
Ahah, I believe you on that. That's why sometimes it feels as if GM is alive and trying to help; after hours, the compiler must get tired and just deliver what I'm trying to make.
 
Top