[SOLVED]Instance not deleting HELP me pls!

N

NizarPlayz

Guest
You can watch the video to see my problem
Object:eek:bj_finishline
Collision with car
Code:
show_message("You win")

room_goto(room0) {

}

object_delete(obj_flag)
The flag does not deletes even if i finish the race
 

JackTurbo

Member
object_delete is an obsolete function. Use instance_destroy.

If you're unfamiliar with instance_destroy, then look it up in the manual and read up on it, its a pretty vital function.

This may or may not be your problem, I cant be sure as there isnt a huge amount of detail to go on, but its worth changing regardless.
 
N

NizarPlayz

Guest
object_delete is an obsolete function. Use instance_destroy
i have already tried instance_destroy if i write
instance_destroy(obj_flag) it would give error
so only doing instance_destroy() would destroy the finishline
 
J

Jaqueta

Guest
instance_destroy doesn't receive arguments.
Instead, use with.

Code:
with(obj_flag)
{
instance_destroy();
}
 
J

Jaqueta

Guest
Yes, it does, you must have multiple objects then.

If your code is in the collision event of the Player with the flag, you can use "other" instead of "obj_flag".
 
N

NizarPlayz

Guest
Yes, it does, you must have multiple objects then.

If your code is in the collision event of the Player with the flag, you can use "other" instead of "obj_flag".
Have you watched the video? When my player is in the race and collides with the black and white blocks finishline it shows a message you win and then go back to the city and the flag is still there so my code is in the collision event of finishline. Sorry for bad english
 
S

Sami

Guest
what happens if you move

with (obj_flag) instance_destroy();

before room_goto ?
 
N

NizarPlayz

Guest
Thats it no method is working for me since i don't know how to code and i'm learning it so i guess i have to figure it out myself or some of you can help me
 
D

dj_midknight

Guest
The flag in question is part of room0 in the room editor?

Ok this is basically what I see happening here.

room0 initializes and sets up the initial flag.
you drive into the flag and it transports you to the room where the race occurs.
crossing the finish line sends you back to room0 which now reruns its creation code and makes a brand new flag for you to repeat your race.

If you want to make the game world all a single room and want to move that flag to multiple locations you are going to need more overhead. Id say use arrays for this and you will need to make them globals that initialize that in a splash screen or something so the code only runs once.

Add one value to your obj_flag create code called index, and a global called race_index or something.

Code:
///inside create event of launcher room, just for kicks lets assume you have 3 flags in the room.
global.flag_cleared[2] = false;
global.flag_cleared[1] = false;
global.flag_cleared[0] = false;

global.flag_x[2] = 1024;
global.flag_x[1] = 726;
global.flag_x[0] = 128;

global.flag_y[2] = 852;
global.flag_y[1] = 456;
global.flag_y[0] = 963;

global.race_index = undefined;
Now in the room room0 creation code something along the lines of this.

Code:
///room0 creation code
var count = array_length_1d(global.flag_cleared);///you can use any of the arrays for this ass they MUST all be the same size
for(var i = 0; i < count; i++){
    if(!global.flag_cleared[i]){
        var setup = instance_create(global.flag_x[i], global.flag_y[i], obj_flag);
        setup.index = i;
    }
}
Ok so when player collides with the flag to transport to the race you need to set the race index.

Code:
global.race_index = index;
room_goto(whatever room you race in);
Now next thing is when you cross the finish line of the race you need to declare the race completed.

Code:
global.flag_cleared[global.race_index] = true;
room_goto(room0);
There are other ways to accomplish this. The main issue you are running into is that room_goto() re runs the initialization code every time, so if you leave the room and complete progress you have to be able to store that info someplace.

You will notice with my version you never have to destroy the flag, and the reason is when the create runs it will now detect that the flag was cleared, and effectively never gets put into the room.

Alternately you could add the flag and have it change to a completed state with a few more additions and when you drive up to the flag it could give you your past score and the option to replay.
 
Last edited by a moderator:
D

dj_midknight

Guest
Adding a bit of info after PMing with OP a bit.

Ok let me break it down just a little bit for you.

When you call room_goto(room0) it recreates the room from its default state. Thus if you call instance_destroy it will have no effect because once you move to the new room it will make a new copy of obj_flag.

What you need to do is store the information that obj_flag needs to be destroyed. I suggested using an array in the thread, but there are other ways to do it.

One example is to use a global.race_completed value. So the initial state of global.race_completed = 0; What this means is you have not completed any races yet.

When you complete the first race it should change the value to global.race_completed = 1; This indicates you have completed race #1.

Now you will need a piece of code that runs once the room is initialized. What it needs to do is see that global.race_completed = 1 and know that it should call with(obj_flag){ instance_destroy(); }

This method is a bit more rudimentary but should also work at the most basic level to do what you want. I'm happy to help you further on this is the open topic if you still need help.
 
N

NizarPlayz

Guest
Ok everyone the case is solved i have figured it out myself thanks everyone for helping me out :p Have a nice day
 
N

NizarPlayz

Guest
The flag in question is part of room0 in the room editor?

Ok this is basically what I see happening here.

room0 initializes and sets up the initial flag.
you drive into the flag and it transports you to the room where the race occurs.
crossing the finish line sends you back to room0 which now reruns its creation code and makes a brand new flag for you to repeat your race.

If you want to make the game world all a single room and want to move that flag to multiple locations you are going to need more overhead. Id say use arrays for this and you will need to make them globals that initialize that in a splash screen or something so the code only runs once.

Add one value to your obj_flag create code called index, and a global called race_index or something.

Code:
///inside create event of launcher room, just for kicks lets assume you have 3 flags in the room.
global.flag_cleared[2] = false;
global.flag_cleared[1] = false;
global.flag_cleared[0] = false;

global.flag_x[2] = 1024;
global.flag_x[1] = 726;
global.flag_x[0] = 128;

global.flag_y[2] = 852;
global.flag_y[1] = 456;
global.flag_y[0] = 963;

global.race_index = undefined;
Now in the room room0 creation code something along the lines of this.

Code:
///room0 creation code
var count = array_length_1d(global.flag_cleared);///you can use any of the arrays for this ass they MUST all be the same size
for(var i = 0; i < count; i++){
    if(!global.flag_cleared[i]){
        var setup = instance_create(global.flag_x[i], global.flag_y[i], obj_flag);
        setup.index = i;
    }
}
Ok so when player collides with the flag to transport to the race you need to set the race index.

Code:
global.race_index = index;
room_goto(whatever room you race in);
Now next thing is when you cross the finish line of the race you need to declare the race completed.

Code:
global.flag_cleared[global.race_index] = true;
room_goto(room0);
There are other ways to accomplish this. The main issue you are running into is that room_goto() re runs the initialization code every time, so if you leave the room and complete progress you have to be able to store that info someplace.

You will notice with my version you never have to destroy the flag, and the reason is when the create runs it will now detect that the flag was cleared, and effectively never gets put into the room.

Alternately you could add the flag and have it change to a completed state with a few more additions and when you drive up to the flag it could give you your past score and the option to replay.
The flag in question is part of room0 in the room editor?

Ok this is basically what I see happening here.

room0 initializes and sets up the initial flag.
you drive into the flag and it transports you to the room where the race occurs.
crossing the finish line sends you back to room0 which now reruns its creation code and makes a brand new flag for you to repeat your race.

If you want to make the game world all a single room and want to move that flag to multiple locations you are going to need more overhead. Id say use arrays for this and you will need to make them globals that initialize that in a splash screen or something so the code only runs once.

Add one value to your obj_flag create code called index, and a global called race_index or something.

Code:
///inside create event of launcher room, just for kicks lets assume you have 3 flags in the room.
global.flag_cleared[2] = false;
global.flag_cleared[1] = false;
global.flag_cleared[0] = false;

global.flag_x[2] = 1024;
global.flag_x[1] = 726;
global.flag_x[0] = 128;

global.flag_y[2] = 852;
global.flag_y[1] = 456;
global.flag_y[0] = 963;

global.race_index = undefined;
Now in the room room0 creation code something along the lines of this.

Code:
///room0 creation code
var count = array_length_1d(global.flag_cleared);///you can use any of the arrays for this ass they MUST all be the same size
for(var i = 0; i < count; i++){
    if(!global.flag_cleared[i]){
        var setup = instance_create(global.flag_x[i], global.flag_y[i], obj_flag);
        setup.index = i;
    }
}
Ok so when player collides with the flag to transport to the race you need to set the race index.

Code:
global.race_index = index;
room_goto(whatever room you race in);
Now next thing is when you cross the finish line of the race you need to declare the race completed.

Code:
global.flag_cleared[global.race_index] = true;
room_goto(room0);
There are other ways to accomplish this. The main issue you are running into is that room_goto() re runs the initialization code every time, so if you leave the room and complete progress you have to be able to store that info someplace.

You will notice with my version you never have to destroy the flag, and the reason is when the create runs it will now detect that the flag was cleared, and effectively never gets put into the room.

Alternately you could add the flag and have it change to a completed state with a few more additions and when you drive up to the flag it could give you your past score and the option to replay.
Okay sorry dude for bothering you again so i tried my own method but it was too difficult and didn't worked well now i tried to use yours
so in the obj_player object i did the collision event with ob_flag
and the code
if keyboard_check(vk_enter)
{
global.race_index = index;
room_goto(race1);
}
now in the obj_player i added a new collision with obj_finishline and here is the code
show_message("You win")
global.flag_cleared[global.race_index] = true;
room_goto(room0)
if file_exists(working_directory + "\textboxes\flag.png") sprite_replace(spr_flag, working_directory + "\textboxes\flag.png", 1, false, false, 0, 0)
score = 100
show_message("Congratulations for winning your first race!")
Now i went in the room creation code and pasted this code
///room0 creation code
var count = array_length_1d(global.flag_cleared);///you can use any of the arrays for this ass they MUST all be the same size
for(var i = 0; i < count; i++){
if(!global.flag_cleared){
var setup = instance_create(global.flag_x, global.flag_y, obj_flag);
setup.index = i;
}
}
and it gave me an error
Error at line 2 pos 14 unknown function or script:array_length_1d
can you solve this? And the next thing
i don't know where to put this code in which object in which event or i don't understand
Code:
///inside create event of launcher room, just for kicks lets assume you have 3 flags in the room.
global.flag_cleared[2] = false;
global.flag_cleared[1] = false;
global.flag_cleared[0] = false;

global.flag_x[2] = 1024;
global.flag_x[1] = 726;
global.flag_x[0] = 128;

global.flag_y[2] = 852;
global.flag_y[1] = 456;
global.flag_y[0] = 963;

global.race_index = undefined;
so can you help me out with these 2 codes? The room creation code and this code and i apologize for bothering you again and by the way i am using GAME MAKER 8.1 and how am i gonna make this that when the obj_flag gets destroyed and obj_flag2 places in the room0 so it would transfer the player to race2
 
D

dj_midknight

Guest
The error you are getting is because you are asking it for the length of an array you never created.

Are you wanting the flag to go in the exact same place as the first one or are these located in different areas of the map?
 
N

NizarPlayz

Guest
The error you are getting is because you are asking it for the length of an array you never created.

Are you wanting the flag to go in the exact same place as the first one or are these located in different areas of the map?
Ok so this is difficult for me so what i did is go into the obj_player and in the create event i added code races_completed = 0 so after that in the collision event with obj_finishline i did
show_message("You win")
room_goto(room0)
if file_exists(working_directory + "\textboxes\flag.png") sprite_replace(spr_flag, working_directory + "\textboxes\flag.png", 1, false, false, 0, 0)
score = 100
show_message("Congratulations for winning your first race!")
races_completed = 1
and DnD draw variable race_completed = 1
relative check
and then i did some coding myself i don't remember it in which object it got deleted by me so what i did was
if races_completed = 1 {
with(obj_flag)instance_destroy();
with(obj_flag2)place_free(1400,400)
}
else
if races_completed = 0
//do nothing
so but when i win the race the number doesn't changes it only stays at 0
 

Yal

🐧 *penguin noises*
GMC Elder
If you want a variable to persist between rooms, make sure it's global.
 
N

NizarPlayz

Guest
If you want a variable to persist between rooms, make sure it's global.
you meen i have to do global.races_completed? Since i am learning to code so i don't know a lot and in which object i have to put global.races_completed?
 

Yal

🐧 *penguin noises*
GMC Elder
you meen i have to do global.races_completed? Since i am learning to code so i don't know a lot and in which object i have to put global.races_completed?
Every object that uses it.

I personally always make a titlescreen early on, so I can have an object in that room initialize all variables to their starting values, and that they only get set to their starting values once (so they won't get re-set so you lose progress).
 
N

NizarPlayz

Guest
I figured it out guys! By using variables you all helped me a lot thanks!
Every object that uses it.

I personally always make a titlescreen early on, so I can have an object in that room initialize all variables to their starting values, and that they only get set to their starting values once (so they won't get re-set so you lose progress).
Thanks to you also you helped me know how to use variables!
 

Yal

🐧 *penguin noises*
GMC Elder
Cool :3

And yeah, there's a lot of things that get easier to do with variables, to say the least... :p
 
Top