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

Code block works on first level room and boss room -- but not in second level room?

Nothing changed as far as I can tell.

My player can switch between two objects: ob_player and ob_cat. Neither of the objects has the switching ability in them, instead, it's governed by the persistent CONTROL object.

Here's the relevant code:

CONTROL:

Step:
Controlling views
GML:
if instance_exists(ob_player)
{
view_visible[0] = true;
view_enabled = true;
view_visible[1] = false;

}

else

if instance_exists(ob_cat)
{
view_visible[0] = false;
view_enabled = true;
view_visible[1] = true;

}
Switching player
GML:
if keyboard_check_pressed(vk_space) and instance_exists(ob_player)
{    with ob_player instance_change(ob_cat,true);
}

 else

if keyboard_check_pressed(vk_space) and instance_exists(ob_cat)
{    with ob_cat instance_change(ob_player,true);
}
I'm not entirely why it won't work and I'm sure there's nothing else "disabling" it. At a loss.


Any help appreciated. Thanks.
 

Prrz

Member
If both the ob_player and ob_cat share the same functional code you could have one object represent the player, and change its sprite index.

GML:
if(keyboard_check_pressed(vk_space)) { 
    if(instance_exists(ob_player)) {
        with(ob_player) {
            if(sprite_index = PlayerSprite) { sprite_index = CatSprite; }
            else { sprite_index = PlayerSprite; }
        }
    }
}
If they must be different objects, something like the following should work for you.

GML:
if(keyboard_check_pressed(vk_space)) {
    if(instance_exists(ob_player)) {
        with(ob_player) {
            instance_destroy();
            instance_create_layer(x,y,"Instances",ob_cat);
        }
    } else {
        with(ob_cat) {
            instance_destroy();
            instance_create_layer(x,y,"Instances",ob_player);
        }
    }
}
Edit: Misread the problem slightly, but solutions should still be relevant in other cases :)
 
Last edited:
Hmm, when I first read the code, I accidentally swapped the instance_exists() checks around in my head like this:
Code:
if keyboard_check_pressed(vk_space) and instance_exists(ob_cat)
{    with ob_player instance_change(ob_cat,true);
}
Which means the code should work (or at least, in an ideal world where there's not a bug somewhere it would work). I'd recommend switching over to @Prrz's method. I believe the only reason to use instance_change is if you want a bunch of variables to jump over to the new instance. I'd wager the error isn't in that code block, it's probably somewhere else. What distinguishes the second room from the first?
 
Nothing really. I duplicated it from the first level (there's nothing in the creation code).

Both have the correct views set up. View[0] follows the player and view[1] follows the cat.

I did that because I thought I could do more with each object in a separate view. Is there a way for both to share the same view?
 

trip

Member
I have question. Is necessary use 2 views?
When view for player have same setting like for cat then you can use 1 view.
Maybe you can remove completely Controlling views.

GML:
if keyboard_check_pressed(vk_space){
    if instance_exists(ob_player){
        with ob_player{
            instance_change(ob_cat,true)
        }
        camera_set_view_target(view_camera[0],ob_cat)//this is for following camera. I think you use this.
    }else if instance_exists(ob_cat){
        with ob_cat{
            instance_change(ob_player,true)
        }
        camera_set_view_target(view_camera[0],ob_player)
    }
    camera_set_view_border(view_camera[0],view_wport[0]/2,view_hport[0]/2)
}

//when use 1 view for both. I recommend insert in Control object room star event.
view_set_wport(0,width)// you must define view width
view_set_hport(0,height)// you must define view height
camera_set_view_border(view_camera[0],view_wport[0]/2,view_hport[0]/2)
if instance_exists(ob_player){
    camera_set_view_target(view_camera[0],ob_player)
}else{
    camera_set_view_target(view_camera[0],ob_cat)
}
A question for you. Where is the problem? Can't switch player / cat?
When you enter the second level, is there a cat or a player visible?
What happens when you change players / cats in second lvl room?
Did you set both views correctly for all rooms in the room editor?

What you means " I duplicated it from the first level "?
Are you sure, In second lvl is only 1 CONTROL object? Check this in debug mode.
 
Last edited:
Top