Legacy GM Turning solid on and off?

A

Ann

Guest
I want to create a door that only my obj_player can pass through and not any enemies. How do I do that?
Please explain things very easily. Explain as if I am 5 years old, because I am new to gamemaker. Thank you!

My problem:
When my obj_player runs through my door (obj_door), the enemies also follow. They should not be able to do that.
 

Llama_Code

Member
You can change the solid state of an object, so the door could be solid and the change it non solid when the player gets close and change it back after the player passes through. However some enemies could still slip through in that small window. This can lead to other issues as well, like enemies getting stuck in the door, etc.

Your best bet is to avoid the solid/non solid mess all together and learn how to create your own collisions.

A quick Google search brings up tutorials on this.
 

TheouAegis

Member
What if you used a variable in the door like "open" and put in the collision event:

if other.open==true x+=hspeed;

?
 

spe

Member
Here's what I'd do:
First, it depends on what kind of collisions you are using for your game. Whatever it is, just make the enemies collide with the door like it's a wall, but give no collision event for the player. I'm going to give a very simple example, because you're new to this. (I'm pretty new as well, only been coding in GML for a few weeks... It gets much easier when you get the hang of it. :) )

First, create a collision event in your enemy object. If you have multiple enemies, you can do it for all of them, or (this is what I recommend) you can do it for one enemy, and make it the parent of all the other enemies. Just be aware that making one object a parent of the others will make all your child objects take on the same behavior only for each event they don't have. So if your parent has a step event, all the code in the step event will be run by the children, but only if they don't have their own step event. If the child has its own step event, it overwrites the parent. You can get around this by using the drag-n-drop function called 'call the parent event' in the event in which you want to use both the parent code and the child code. Anyways, for a very basic collision system, you could put in something like 'moveSpeed = 0' (or whatever variables you use to move the object) in the collision event with the wall, or use the drag-n-drop button for instance movement, set it to the square in the middle and speed to 0. There are certainly potential problems with this collision system, and I wouldn't recommend it for any sort of intermediate use. However, seeing as you're just starting out, it should work fine for your purposes. So if you have your collisions with walls set up (both your player and enemies should probably collide with walls), just do the same thing for the enemy colliding with the door. This way, they will hit it just like a wall. Now, for the simple part: do nothing. Just omit that particular collision event with the player, and the player should be able to walk right through the door. Hopefully that works for you. If not, I probably either badly misunderstood what you were asking, or I'm lacking very basic Game Maker knowledge. :p Anyways, good luck!
 
A

Ann

Guest
Here's what I'd do:
First, it depends on what kind of collisions you are using for your game. Whatever it is, just make the enemies collide with the door like it's a wall, but give no collision event for the player. I'm going to give a very simple example, because you're new to this. (I'm pretty new as well, only been coding in GML for a few weeks... It gets much easier when you get the hang of it. :) )

First, create a collision event in your enemy object. If you have multiple enemies, you can do it for all of them, or (this is what I recommend) you can do it for one enemy, and make it the parent of all the other enemies. Just be aware that making one object a parent of the others will make all your child objects take on the same behavior only for each event they don't have. So if your parent has a step event, all the code in the step event will be run by the children, but only if they don't have their own step event. If the child has its own step event, it overwrites the parent. You can get around this by using the drag-n-drop function called 'call the parent event' in the event in which you want to use both the parent code and the child code. Anyways, for a very basic collision system, you could put in something like 'moveSpeed = 0' (or whatever variables you use to move the object) in the collision event with the wall, or use the drag-n-drop button for instance movement, set it to the square in the middle and speed to 0. There are certainly potential problems with this collision system, and I wouldn't recommend it for any sort of intermediate use. However, seeing as you're just starting out, it should work fine for your purposes. So if you have your collisions with walls set up (both your player and enemies should probably collide with walls), just do the same thing for the enemy colliding with the door. This way, they will hit it just like a wall. Now, for the simple part: do nothing. Just omit that particular collision event with the player, and the player should be able to walk right through the door. Hopefully that works for you. If not, I probably either badly misunderstood what you were asking, or I'm lacking very basic Game Maker knowledge. :p Anyways, good luck!

Thank you for the good answer :D I did try some of this. I did this:
1. I went into my obj_enemy and made a collission event with the door.
2. I dragged some code into the collission event and wrote speed= 0.

This is supposed to stop the enemy from moving when he touches the door, but it did not work.
 
A

Ann

Guest
What if you used a variable in the door like "open" and put in the collision event:

if other.open==true x+=hspeed;

?
I did not understand that, sorry : o
You want me to make a new variable in the obj_door? So inside of the create event of the door you want me to write open=true
and then you want me to make a collision event where? please specify what you want me to write and where.
 
A

Ann

Guest
You can change the solid state of an object, so the door could be solid and the change it non solid when the player gets close and change it back after the player passes through. However some enemies could still slip through in that small window. This can lead to other issues as well, like enemies getting stuck in the door, etc.

Your best bet is to avoid the solid/non solid mess all together and learn how to create your own collisions.

A quick Google search brings up tutorials on this.
I'd love to do that, but it might be a bit too difficult since I am new to gamemaker. And also the thing that I am working on needs to be finished by tomorrow....
 

spe

Member
Thank you for the good answer :D I did try some of this. I did this:
1. I went into my obj_enemy and made a collission event with the door.
2. I dragged some code into the collission event and wrote speed= 0.

This is supposed to stop the enemy from moving when he touches the door, but it did not work.
Well, speed is a built-in variable in Game Maker. If you're not already using speed for everything, you should make sure you're using your own variable here. I've never really tried actually using the variable speed for anything, so I don't know what problems it may cause for something like this. If you're still having problems, you may have to set up a more serious collision system. Although I think the problem is something minor hiding in the code somewhere, perhaps you can post the code you have and we can take a look at it? :)
 
Z

zircher

Guest
Look at your enemy's player chase code, it might be overriding you setting the enemy's speed to 0.
 
A

Ann

Guest
Well, speed is a built-in variable in Game Maker. If you're not already using speed for everything, you should make sure you're using your own variable here. I've never really tried actually using the variable speed for anything, so I don't know what problems it may cause for something like this. If you're still having problems, you may have to set up a more serious collision system. Although I think the problem is something minor hiding in the code somewhere, perhaps you can post the code you have and we can take a look at it? :)
Alright, which code do you want to see? The one for my door?
Inside of my obj_enemy I have a collision event with the door. Inside I have just written speed=0. Thats it;


This is the code that I have in my obj_dog (an enemy): It's an AI.

//Start stationary
if mode=0
{
if distance_to_object(obj_player_collission)<64
{
mode=1
}
}

//Follow if too close
if mode=1
{
mp_potential_step(obj_player_collission.x, obj_player_collission.y,spd,0)

}


//Police run away if you hide
if global.hide = true
{
mode=2
}

if mode =2
{
mp_potential_step(obj_player_collission.x, obj_player_collission.y,-spd,0)
if distance_to_object(obj_player_collission)>100
{
mode=0
global.hide=false
}

}


//if distance_to_object(obj_player_collission)>128
// {
// mode=0
// }

I dont think anything here disrupts anything else. It's kinda weird how it didnt work anyways with the collission and stuff. I will try asking my teacher tomorrow :)
 

spe

Member
I feel like, whatever the problem may be, you may already have a simpler solution available in your code. Something like this, in the collision event for the enemy against the door:
Code:
if mode != 0
    {
   mode = 0;
    }
If I understand what you're doing correctly, then 'mode 0' means the object is stationary. If you simply want the object to stop when it hits the door, I think this would work.
 
Top