SOLVED Problem with objects

Alicja

Member
Hi. I have a problem with the object. I made a door and a button. When you press the button, the door opens.
But: They only open when you can see them on the screen (you can't press the button).
If I go further across the board and see the door and go back to the button, everything works. It looks like it doesn't scratch the door until I go there. If I put the door near the button so that it can be seen, it works. What is she supposed to do? Is this an option in the program?
The door is an object with a sprite.
 

Alicja

Member
obj_switch_door

CREATE
GML:
image_speed = 0;

is_triggered = false;
is_triggered2 = false;

target = noone;
target2 = noone;
STEP
GML:
if (target==noone)
{
for(var cnt=0; cnt<instance_number(obj_door); cnt++)
{
var instance=instance_find(obj_door,cnt);

if(instance.name==door_to_open)
{
target=instance;
break;
}
}
}

if (target2==noone)
{
for(var cnt=0; cnt<instance_number(obj_door); cnt++)
{
var instance=instance_find(obj_door,cnt);

if(instance.name2==door_to_open2)
{
target2=instance;
break;
}
}
}
COLLISION WITH OBJ_PLAYER
GML:
// if I press > D <
if (global.input_key_d_pressed)
{
if(is_triggered==true)
{
exit;
}

if(target != noone)
{
target.state=STATE_DOOR.opening;

image_index = 1;
is_triggered = true;
}
}

// if I press > D <
if (global.input_key_d_pressed)
{
if(is_triggered2==true)
{
exit;
}

if(target2 != noone)
{
target2.state=STATE_DOOR.opening;

image_index = 1;
is_triggered2 = true;
}
}



obj_door
CREATE
GML:
image_speed = 0;

state=STATE_DOOR.closed;
STEP
GML:
switch(state)
{
case STATE_DOOR.closed:
break;

case STATE_DOOR.opening:
image_speed = 4;
if(image_index=4)
{
state=STATE_DOOR.opened;
}
break;

case STATE_DOOR.opened:
break;
}
ANIMATION END
GML:
image_speed = 0;
image_index = 4;
 
Last edited:
Sooooo, from what I understand with your code is the following:

In your obj_switch_door, you are using a for loop finding every door with instance_find() and give each of them a button. The problem, I think, is the instance_find() himself, because this function is not using nearest options, so it will finds a instance in the room with the number (cnt variable in your for) to find. Because of the missing nearest option, you can have a switch on the left of your screen to open the last door on right, one next to following, switch to right to open door to left.

Also, because of GMS2 debug purposes, you need the randomize() in the start of the game to randomize the seed, SO if you do randomize(), your instance_find() will select randomly a switch for a random door, which I think you don't want in your game.

Instance_find() may be not your best choice for your goal, so maybe you could use door1 = false/true door2 = false/true if you are really stuck.

Have a nice day :)


EDIT: to avoid instance_find() to select himself (in step of obj_door to find obj_door, example), you are best to use, for example:

If (target != noone and target != id)

EDIT 2: If there is any error in my programmation explanations, please forgive me, I am not at home now with my computer to be able to test my explanations before post it.
 
Last edited:

Alicja

Member
Sooooo, from what I understand with your code is the following:

In your obj_switch_door, you are using a for loop finding every door with instance_find() and give each of them a button. The problem, I think, is the instance_find() himself, because this function is not using nearest options, so it will finds a instance in the room with the number (cnt variable in your for) to find. Because of the missing nearest option, you can have a switch on the left of your screen to open the last door on right, one next to following, switch to right to open door to left.

Also, because of GMS2 debug purposes, you need the randomize() in the start of the game to randomize the seed, SO if you do randomize(), your instance_find() will select randomly a switch for a random door, which I think you don't want in your game.

Instance_find() may be not your best choice for your goal, so maybe you could use door1 = false/true door2 = false/true if you are really stuck.

Have a nice day :)


EDIT: to avoid instance_find() to select himself (in step of obj_door to find obj_door, example), you are best to use, for example:

If (target != noone and target != id)

EDIT 2: If there is any error in my programmation explanations, please forgive me, I am not at home now with my computer to be able to test my explanations before post it.
There, I also have the settings for which buttons to which doors are in variable. The problem was the above. I used the instance_activate_object (obj_door); option and everything works.
 

Alicja

Member
At least, you problem is solved :) Glad to hear that.
Not all problem :(
This is my pause...

GML:
// pause
if (global.input_key_enter_pressed)
{
    switch (GAME_PAUSED)
    {
        // [UNPAUSE] if it was paused, resume song, activate all instances and delete screenshot
        case true:
            audio_resume_all();
            audio_resume_sound(level_song);
            audio_play_sound(snd_pause_out, 0, false);
            if sprite_exists(GAME_SCREENSHOT) {sprite_delete(GAME_SCREENSHOT);}
            instance_activate_all();
            GAME_PAUSED = false;
            break;
       
       
        // [PAUSE] desactivate all instances, pause song and take screenshot of the game to draw later
        case false:
            audio_pause_all();
            audio_pause_sound(level_song);
            audio_play_sound(snd_pause_in, 0, false);
            GAME_SCREENSHOT = sprite_create_from_surface(application_surface,0,0,surface_get_width(application_surface),surface_get_height(application_surface),0,0,0,0);
            instance_deactivate_all(true);
            instance_activate_object(obj_game_input);
            GAME_PAUSED = true;
            break;
    }
}
When I PAUSE GAME, all enemies where i make this: instance_activate_object(obj_enemy1); - and I did it so that when they leave the board, they will come back, not only when I see them (similar situation to the door, I used the same)|
they do not hold positions, they ignore SOLID BLOCKS and fall off the board.
You can help me with this?

When I don't put this piece of code to enemy "instance_activate_object(obj_enemy1);" pause work normaly (enemy hold position, etc.), but when enemy will go behind the board don't come back (only back when i go and see enemy - like with door problem).

I fix this with this small piece of code but dont hold position now :(:(:(

BEHIND BOARD - I mean camera view.
 

Nidoking

Member
When I PAUSE GAME, all enemies where i make this: instance_activate_object(obj_enemy1); - and I did it so that when they leave the board, they will come back, not only when I see them (similar situation to the door, I used the same)|
they do not hold positions, they ignore SOLID BLOCKS and fall off the board.
Are you also activating the SOLID BLOCKS? If they're not active, they don't exist for any purpose other than being activated.
 

TheouAegis

Member
If you deactivate everything, then reactivate the door, and everything else remains deactivated. Just as you are activating enemies when they get in range of the player, you should be activating solids sooner. So for example if you are activating enemies when they are within the view, activate solids when they are within 32 pixels outside of the view.
 
Top