Unparent an object

S

Simulacron

Guest
I have an object that is used to decide if an object is solid or not (I don't use the build in solid option) One of my objects is parented to this solid object, but after an animation reached a certain image (image_index = 7) I want my object to unparent, so it isn't solid anymore because the player should be able to walk through the object at this moment. Is there a function to do this? I couldn't find it.
 

FrostyCat

Redemption Seeker
Repeat after me: Instances don't have parents, objects have parents.

If you want to inherit event behaviours, event_perform_object() can already do that.

If you want to group objects/instances by some property and act upon them, a with-if combo like this can easily handle it.
Code:
with (object_or_all) {
  if (insert_condition_or_property_here) {
    /* Actions */
  }
}
Many novices around here want the ability for objects to have multiple parents or change them at runtime. None actually needs it.
 
D

Docker

Guest
Continuing on from FrostyCat, if you do decide to use a parent at any point you can still provide additional functionality within the children for example:

you can have a parent called obj_entities with a create event that contains common variables shared by all living things in your game, you can then have a NEW create event in one of the children and just put event_inherited(); at the beginning which means inherit everything from the parent then anything typed after becomes additional or overriding depending on how you use it.

I'm going to guess you're just using the parent object for collision so this example would be less applicable but an example for this would be:

Code:
inside parent create event:
solid_obj = true;

inside the child step event:
if (image_index == 7){
   solid_obj = false;
}

inside the collision code:
if (place_meeting(x+hspd, y, obj_parent) && solid_obj == true){
   // run collision
}
I'm not saying this is how you should solve your problem, I'm just giving you a bit of an insight into parent/child relationships as inheritance is a powerful tool that can reduce a lot of duplicate code and provide some interesting functionalities.

My advise is to read up on inheritance as a whole.
 
S

Simulacron

Guest
Continuing on from FrostyCat, if you do decide to use a parent at any point you can still provide additional functionality within the children for example:

you can have a parent called obj_entities with a create event that contains common variables shared by all living things in your game, you can then have a NEW create event in one of the children and just put event_inherited(); at the beginning which means inherit everything from the parent then anything typed after becomes additional or overriding depending on how you use it.

I'm going to guess you're just using the parent object for collision so this example would be less applicable but an example for this would be:

Code:
inside parent create event:
solid_obj = true;

inside the child step event:
if (image_index == 7){
   solid_obj = false;
}

inside the collision code:
if (place_meeting(x+hspd, y, obj_parent) && solid_obj == true){
   // run collision
}
I'm not saying this is how you should solve your problem, I'm just giving you a bit of an insight into parent/child relationships as inheritance is a powerful tool that can reduce a lot of duplicate code and provide some interesting functionalities.

My advise is to read up on inheritance as a whole.
While I understand your code, I have problems implementig it in my game. I used your variable solid_obj, but my collision event is in the obj_player, so I tried to use the following code line:
Code:
if (place_meeting(x,y+vspd,obj_wall) && other.solid_obj = true)
But when I start the game it gives me an error message that the variable solid_obj isn't set, however I put it in the create event of my obj_wall.
 
D

Docker

Guest
While I understand your code, I have problems implementig it in my game. I used your variable solid_obj, but my collision event is in the obj_player, so I tried to use the following code line:
Code:
if (place_meeting(x,y+vspd,obj_wall) && other.solid_obj = true)
But when I start the game it gives me an error message that the variable solid_obj isn't set, however I put it in the create event of my obj_wall.
I was only writing an example, it wasn't really suitable for use for little things like you've just mentioned lol

The reason its having a problem is because until the collision event occurs, it has no idea what instance 'other' refers to so naturally it also cant access the variable solid_obj within it which causes a compile error, instead you'd do:
Code:
if (place_meeting(x, y+vspd,obj_wall){
   if (other.solid_obj == true){
    // collision
  }
}
 
G

Gillen82

Guest
Go to the obj_wall create event to initialize the variable

Code:
solid_obj = true;
 
S

Simulacron

Guest
I was only writing an example, it wasn't really suitable for use for little things like you've just mentioned lol

The reason its having a problem is because until the collision event occurs, it has no idea what instance 'other' refers to so naturally it also cant access the variable solid_obj within it which causes a compile error, instead you'd do:
Code:
if (place_meeting(x, y+vspd,obj_wall){
   if (other.solid_obj == true){
    // collision
  }
}
So it's just important that it happens after each other and not in the same if statement?

Edit: I tried it out and it still doesn't work:(.
 
Last edited by a moderator:
D

Docker

Guest
Go to the obj_wall create to initialize the variable

Code:
solid_obj = true;
Hes already done that, the reason its not working is because other isn't referencing an object so the compiler cant access the inner variable of that unreferenced object and gives the error of the variable not being set.

So it's just important that it happens after each other and not in the same if statement?
I believe so but it may not even let you reference 'other' in this manner either, I'm not in a position to check and I always forget whether it can or not. A way I can tell you 100% works is to grab the instances ID:
Code:
if (place_meeting(x, y+vspd,obj_wall){
   collision_object = instance_place(x, y+vspd, obj_wall);
   if (collision_object.solid_obj == true){
      // collision
  }
}
I'm sure there is probably a smarter way of doing it that I am unaware of or not considering at the moment but oh well...

EDIT :: you may even be able to do:
Code:
if (place_meeting(x, y+vspd,obj_wall){
   if (instance_place(x, y+vspd, obj_wall).solid_obj == true){
      // collision
  }
}
Sorry about the half answers :p
 
Last edited:

FrostyCat

Redemption Seeker
There's no point checking twice, just do it once with instance_place() and check it against noone.
Code:
var inst = instance_place(x, y+vspd, obj_wall);
if (inst != noone && inst.solid_obj) {
  //...
}
 
D

Docker

Guest
There's no point checking twice, just do it once with instance_place() and check it against noone.
Code:
var inst = instance_place(x, y+vspd, obj_wall);
if (inst != noone && inst.solid_obj) {
  //...
}
Although there's no point checking twice as you said, from a runtime efficiency they should be the same as both ways are constant right? This is a genuine question as I'm always curious about other ways of doing things and the performance differences.
Code:
if (place_meeting(x, y+vspd,obj_wall){
   if (instance_place(x, y+vspd, obj_wall).solid_obj == true){
     // collision
  }
}
 

FrostyCat

Redemption Seeker
Although there's no point checking twice as you said, from a runtime efficiency they should be the same as both ways are constant right? This is a genuine question as I'm always curious about other ways of doing things and the performance differences.
Code:
if (place_meeting(x, y+vspd,obj_wall){
   if (instance_place(x, y+vspd, obj_wall).solid_obj == true){
     // collision
  }
}
They are both constant time, but you spend twice as much time on collisions as I do. Among operations present in our code, the collision check functions are by far the heaviest of all, and rather disproportionately if you profile them under stress. place_meeting() won't make instance_place() any faster, so in every sense you are duplicating the work.

The fallacy that a lot of people get into with big O notation is equating it directly to actual performance, without paying attention to the multiplier (M in this article section). 1 and 100000000 both count as O(1) due to their lack of dependent variables, but their performances at runtime are miles apart.
 
Top