Jump to content


Photo

Instance_change how can I get id of new object?


  • Please log in to reply
8 replies to this topic

#1 Jasher

Jasher

    GMC Member

  • GMC Member
  • 65 posts
  • Version:GM:Studio

Posted 02 July 2012 - 02:04 AM

Hello,

Sorry if someone has already asked this question. I looked for topics like this but couldn't find anything that helped.

I want to change one object into another object using instance_change

I also want the new object to have all the variables and sprite_index of the old object.

instance_change(new_object,perf); automatically passes all the new variables I created and the image_blend, image_xscale,image_yscale,ect.
But for some reason it doesn't pass on the old sprite_index to the new object.

I tried to assign the old sprite_index to the new object using this code;


with(instance_change(object2,false))sprite_index = other.sprite_index;

But it doesn't work.

So how can I get the id of the new object that the old one changed into?
  • 0

#2 PrinceOthman

PrinceOthman

    GMC Member

  • GMC Member
  • 434 posts
  • Version:GM8

Posted 02 July 2012 - 02:37 AM

I'm not sure about that. But what are you trying to do? Maybe there's some other way to approach this.
  • 0

#3 _226097

_226097

    GMC Member

  • GMC Member
  • 76 posts
  • Version:GM:Studio

Posted 02 July 2012 - 02:54 AM

How many variable are we talking about? If it's just a few variables you can create & destroy instead of just change:
var ii;
ii = instance_create(x,y,obj_new); //  !!! Change the object name !!!
ii.sprite_index = sprite_index;
// Add more yourself
instance_destroy();

  • 0

#4 Debels

Debels

    GMC Member

  • GMC Member
  • 2018 posts
  • Version:GM:Studio

Posted 02 July 2012 - 03:15 AM

Hello,

Sorry if someone has already asked this question. I looked for topics like this but couldn't find anything that helped.

I want to change one object into another object using instance_change

I also want the new object to have all the variables and sprite_index of the old object.

instance_change(new_object,perf); automatically passes all the new variables I created and the image_blend, image_xscale,image_yscale,ect.
But for some reason it doesn't pass on the old sprite_index to the new object.

I tried to assign the old sprite_index to the new object using this code;


with(instance_change(object2,false))sprite_index = other.sprite_index;

But it doesn't work.

So how can I get the id of the new object that the old one changed into?

Just make a variable like:

sprite=sprite_index;//or the name of the sprite

and in the new object put in the create event:

sprite_index=sprite;

So your final code should look like this:

sprite=sprite_index;
instance_change(object,1);

Create event of object:

sprite_index=sprite;

  • 0

#5 Jasher

Jasher

    GMC Member

  • GMC Member
  • 65 posts
  • Version:GM:Studio

Posted 02 July 2012 - 03:57 AM

I already knew all that.

I just didn't wan't to have to create a new variable to store the original sprite index of the old version.
And I didn't want to have a create event inside the new object because there are a lot of these objects on screen at the same time and I want to conserve as much memory and code as possible.

Currently I am doing it this way.

Step event
//increase size of orb.
image_xscale += 0.1;
image_yscale += 0.1;

if (image_yscale > 0.9)//If size is back to normal.
{
    with(instance_create(x,y,orb_stat_obj))
    {   //Turn the orb into static orb.
        hspeed = other.hspeed;
        vspeed = other.vspeed;
        sprite_index = other.sprite_index;
        mask_index = other.mask_index;
        image_speed = other.image_speed;
        image_blend = other.image_blend; 
        img_bl = other.img_bl;
        dc = other.dc;
    }
    instance_destroy();
}

I want to find a way to change a lot of objects into different objects at the same time using as little code as possible.

The game runs fine on my computer but I want to optimize it as much as possible.

I need to often change these objects into new objects becuase sometimes I need them to have collision detect events but I don't want all of them to collision detect.

Edited by Jasher, 02 July 2012 - 04:04 AM.

  • 0

#6 _226097

_226097

    GMC Member

  • GMC Member
  • 76 posts
  • Version:GM:Studio

Posted 02 July 2012 - 04:18 AM

Variables takes really really really low memory and cpu. What is may slow down the computer are objects, as they need to collision detect every step, as well as all the drawings on the screen. Creating a temporarily variable to hold the id of the new object, and throw over all the nessesary variable from obj_old to obj_new and then destroy obj_old is the least consuming method i can think of. See my previous post for the code.
  • 0

#7 Jasher

Jasher

    GMC Member

  • GMC Member
  • 65 posts
  • Version:GM:Studio

Posted 02 July 2012 - 06:22 AM

Variables takes really really really low memory and cpu. What is may slow down the computer are objects, as they need to collision detect every step, as well as all the drawings on the screen. Creating a temporarily variable to hold the id of the new object, and throw over all the nessesary variable from obj_old to obj_new and then destroy obj_old is the least consuming method i can think of. See my previous post for the code.



So creating a temporary variable and assigning it to the new id is faster than placing the instance_create event inside a with() event?

If so, I should make some slight changes to my code.
  • 0

#8 _226097

_226097

    GMC Member

  • GMC Member
  • 76 posts
  • Version:GM:Studio

Posted 02 July 2012 - 06:35 AM

I never tried changing objects the way you do it but it's definitely not slower in terms of CPU.

Anyhow, wasn't the problem that your current code were not working?
  • 0

#9 Jasher

Jasher

    GMC Member

  • GMC Member
  • 65 posts
  • Version:GM:Studio

Posted 02 July 2012 - 02:41 PM

Well yes and no.

I just wanted to know if I was not getting something right in terms of using the change_instance function.

But it turns out I am not so thank you for your help. :)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users