Destroying and creating objects in the same step

Megax60

Member
Step event for enemy:
Code:
if (collision_circle(x,y,320*vision_size,p,true,false)){//If player is inside the enemy vision
        //Note: p stands for obj_player
        var recover;
        var i;
        var detected = false
            for(i = 0; detected = false; i += 1){
            collision = collision_line(x,y,p.x,p.y,obj_solid,false,false); //Create a line between enemy and player
                if collision != noone{ //If it detected a obj_solid
                    if collision.type = 1{ //If that solid was an untranspasable object, can't see the player
                        detected = true; //End for loop
                        seen = 0;   //Haven't seen the player
                    }else{ //If the solid was a traspasable object, save x,y and image_xscale in a array
                        recover[0,i] = collision.x;
                        recover[1,i] = collision.y;
                        recover[2,i] = collision.image_xscale;
                        with collision{ //Destroy the object
                            instance_destroy();
                        }
                        if collision = noone{   //If no more solids detected, sees the player
                            detected = true;
                            seen = 1;
                        }//Else continue the loop until it detects an untranspasable object or
                         //Deletes all objects between player and enemy
                    }
                }else{ //If there are no solid object between player and enemy
                    detected = true; //End for loop
                    seen = 1; //See the player
                }
            }
            //Recreate destroyed blocks
            //This is where its supposed to recreate the saved objects
    }else{  //If out of range, can't see the player
        seen = 0;
    }
Well, im quite new to arrays, i still don't know how they exactly works

what this code does, is that it creates a collision line between enemy and player, if it detects an untranspasalbe object, the enemy can't see the player, else if it is a traspasable one, it saves (i think it does) the object x,y and image_xscale and then destroys it, finally if there are no objects left between enemy and player, enemy can see the player (if its inside of its vision range), if it sees the player it does magic stuff.

the problem is that i dont know how to recreate the destroyed objects, i already tried, but it gives an error

help?
 

Corey

Member
I don't think a for loop or arrays are necessary since you are checking if something is true or false. You can use a boolean statement in your step event to track when an object is placed, store it's x and y values in an array or variable, and then destroy the object. You can then recreate the object again using the array data values for the x and y.

Using arrays:
Code:
var recover;
else if (collision.type != 1)
{
    recover[0] = collision.x;
    recover[1] = collision.y;
    recover[2] = collision.image_xscale;
    instance_create_layer(recover[0],recover[1],"Instances",obj_solid);
}
Using variables:
Code:
var collx;
var colly;
var collxscale;
else if (collision.type != 1)
{
    collx = collision.x;
    colly = collision.y;
    collxscale = collision.image_xscale;
    instance_create_layer(collx,colly,"Instances",obj_solid);
}
To further conclude arrays: Arrays are just a variable that can hold a tree of information. Think of it as a stack of paper, and each piece of paper has a number on it that you declared. You can pick a piece of paper, at a specific position, and pull out that number you declared by calling the array: paper[14]. This will pull page 14 from the paper stack.
 
Last edited:

Megax60

Member
I don't think a for loop or arrays are necessary since you are checking if something is true or false. You can use a boolean statement in your step event to track when an object is placed, store it's x and y values in an array or variable, and then destroy the object. You can then recreate the object again using the array data values for the x and y.

Using arrays:
Code:
var recover;
else if (collision.type != 1)
{
    recover[0] = collision.x;
    recover[1] = collision.y;
    recover[2] = collision.image_xscale;
    instance_create_layer(recover[0],recover[1],"Instances",obj_solid);
}
Using variables:
Code:
var collx;
var colly;
var collxscale;
else if (collision.type != 1)
{
    collx = collision.x;
    colly = collision.y;
    collxscale = collision.image_xscale;
    instance_create_layer(collx,colly,"Instances",obj_solid);
}
wait, instance_create_layer is only on gamemaker 2 right? i have 1.4
 

Corey

Member
wait, instance_create_layer is only on gamemaker 2 right? i have 1.4
I apologize. I'm used to working with GMS2. The syntax with GML is different in GMS2.

If you're using 1.4, use instance_create(x,y,object);

This will create a new object with a new object unique id.
 
Top