GML Bring original Instance back once gone

Hey friends. Im stumped again. I have tried so many different things. Again, I am sure the solution is somewhat simple, just need a more seasoned coder to help me see it. So here is what I have and what I am after.

Once someone types the pet command this code runs...
Code:
if (pet != false)
{
    with (oFloof)
    { 
        var floofX = oFloof.x;
        var floofY = oFloof.y;
  
        instance_destroy(oFloof);
        instance_create_layer(floofX,floofY,"World",oFloofPet);
        pet = false;
    }
}
Inside of the oFloofPet object I have an Animation End event that destroys the instance.
This works fine, my issue is that one it destroys itself I want to redraw the original oFloof object in its place. Sounds simple to do to me, but I have tried everything from instance_create_layer to instance_change to sprite change. Sprite change works but then its tagged as oFloofPet and the original code no longer works cause its looking for oFloof.

Pulling my hair out. Any help is greatly appreciated friends.
 
Last edited:

samspade

Member
Short answer is, most literally, you can't. You would need to create a new one or not actually destroy the old one. If the variables don't need to transfer this is very straight forward. Simply repeat the process of destroying the current instance and creating a new one. If you need to carry variables over, it makes it a little more complicated, but not by much.

What does the destroy event for oFloofPet look like? Or maybe a better question, what did your instance_create_layer look like?

A couple notes on this code. This code appears to work with an object name (oFloof) and is not a good idea if you ever want to have more than one oFloof at the same time. You really should be using an instance id. However, if there will never be more than one oFloof at a time, this is fine.
 
Short answer is, most literally, you can't. You would need to create a new one or not actually destroy the old one. If the variables don't need to transfer this is very straight forward. Simply repeat the process of destroying the current instance and creating a new one. If you need to carry variables over, it makes it a little more complicated, but not by much.

What does the destroy event for oFloofPet look like? Or maybe a better question, what did your instance_create_layer look like?

A couple notes on this code. This code appears to work with an object name (oFloof) and is not a good idea if you ever want to have more than one oFloof at the same time. You really should be using an instance id. However, if there will never be more than one oFloof at a time, this is fine.
Yes, there should only ever be one instance of oFloof at a given time. That will never change. Essentially what needs to happen is when the !pet command is given then oFloof should change to oFloofPet then once finished change back to oFloof. I figured a simple sprite swap would work but for some reason doesnt.
 

samspade

Member
Yes, there should only ever be one instance of oFloof at a given time. That will never change. Essentially what needs to happen is when the !pet command is given then oFloof should change to oFloofPet then once finished change back to oFloof. I figured a simple sprite swap would work but for some reason doesnt.
What does it mean that a sprite swap doesn't work? You can definitely do that if that's all you need (e.g. code doesn't need to change).
 
What does it mean that a sprite swap doesn't work? You can definitely do that if that's all you need (e.g. code doesn't need to change).
Well this is what I am trying now...
Code:
if (pet != false)
{
    with (oFloof)
    {   
        sprite_index = sFloofPet;
        if (image_index > image_number - 1) {
            sprite_index = sFloof;
        }
        pet = false;
    }
}
But what happens is when triggered, the sprite swaps fine (but starts at the wrong frame/image_index) runs the animation, and when I try to swap back at the end of the animation it just loops the swapped in sprite. Additionally if I try to set the image_index to 0 when it triggers so the animation begins where it needs to the image just stops on the 0 frame and doesnt animate. Even if I tell it to with image_speed = 1;
 

samspade

Member
That doesn't sound right as I do things like that all the time. You might have other code which effects it. Or perhaps your code is in the wrong place.

The following code would work in some situations and I use variations of it all the time:

Code:
///inside oFloof
if (pet == true) {
    pet = false;
    sprite_index = sFloofPet;
    image_index = 0;
    image_speed = 1;
}


///animation end
if (sprite_index == sFloofPet) {
    sprite_index = sFloof;
}
So other questions for you would be, where is the above code you've posted and is pet an global, instance, or locally scoped variable?
 
That doesn't sound right as I do things like that all the time. You might have other code which effects it. Or perhaps your code is in the wrong place.

The following code would work in some situations and I use variations of it all the time:

Code:
///inside oFloof
if (pet == true) {
    pet = false;
    sprite_index = sFloofPet;
    image_index = 0;
    image_speed = 1;
}


///animation end
if (sprite_index == sFloofPet) {
    sprite_index = sFloof;
}
So other questions for you would be, where is the above code you've posted and is pet an global, instance, or locally scoped variable?
As much as I wanted that to work it just doesnt. Something somewhere has to be affecting it. If I set the image_index to 0 the animation sits still at frame 0. Refusing to animate, even if I set animation speed to 1.

additionlly I tried using an instance_create_layer and it seems to be drawing the instance every single frame so if there are 20 frames in its animation then there are 20 instances of it redrawing and redrawing.
 

samspade

Member
That is how instance_create works. It will run every step if you let it. Also saying "as much as I wanted to that to work it just doesn't" isn't very helpful. You might want to search your code (control + shift + f) for things that could mess with animation speed and so on. Also check your sprites.
 
That is how instance_create works. It will run every step if you let it. Also saying "as much as I wanted to that to work it just doesn't" isn't very helpful. You might want to search your code (control + shift + f) for things that could mess with animation speed and so on. Also check your sprites.
Oh apologies, I didnt mean that in a rude way. I actually was REALLY hoping it would work. Its kicking my butt :)
 
okay to update you. Thanks for your help BTW. I got it working finally. If you want to know how I would be happy to share. Thank you Samspade
 

woods

Member
is good practice to always share your final fix... it helps other in the future when they run into similar issues ;o)

also am curious as to what the issue was and how you solved it
 
is good practice to always share your final fix... it helps other in the future when they run into similar issues ;o)

also am curious as to what the issue was and how you solved it
Truthfully I am still not sure what the issue was, I know one issue I had was the multiple instances being create but after some research I learned how to solve it simply with a true/false variable. But this is the final setup that works properly. I create a script to help facilitate the further influx of commands that will be coming in, but I will put that below as well.

oMain - Create
Code:
global.markX = oMarker.x;
global.markY = oMarker.y;
global.floof = false;

pet = false;
oMain - Step
Code:
// Draw Floof
if (!global.floof) {
    instance_create_layer(global.markX,global.markY,"World",oFloof);  
    global.floof = true;
}

// Pet Event
if (pet != false) {
    with (oFloof) {
        scEvent(0,1,oFloofPet,true);
    }
}
oFloofPet - Step
Code:
if (image_index > image_number - 1) {
    with (oMain) { pet = false; }
    scEvent(0,1,oFloof,true);
}
scEvent - Script
Code:
/// scEvent(index, speed, obj, run?)
/// @param index
/// @param speed
/// @param obj
/// @param run?

image_index = argument0;
image_speed = argument1;
instance_change(argument2,argument3);
Thank you all for your help and direction friends. Means a ton to a newb like meh-self.
 
Top