Run Script only Once

P

Parker

Guest
When I try to run this script of placing tiles around the player in the step event it doesn't do it only once. Here is my code, I have no idea what is happening. Thanks for any of your help.
Create Event:
Code:
do_once = true;
Step Event:
Code:
if (global.turn == "player") {
    if (do_once == true) {
        scr_avaliable_movements();
        do_once = false;
    }
}
Script:
Code:
up = place_empty(x, y-GRID_SIZE);
right = place_empty(x+GRID_SIZE, y);
down = place_empty(x, y+GRID_SIZE);
left = place_empty(x-GRID_SIZE, y);
        
if (right) instance_create(x+GRID_SIZE, y, obj_pawn_dir);
if (down) instance_create(x, y+GRID_SIZE, obj_pawn_dir);
if (left) instance_create(x-GRID_SIZE, y, obj_pawn_dir);
if (up) instance_create(x, y-GRID_SIZE, obj_pawn_dir);
 

Slyddar

Member
So if you add a line such as
Code:
show_debug_message("test");
into the loop, right after do_once = false; it will show lots of outputs of the word "test" ? If so, where else are you resetting the variable do_once?
 
P

Parker

Guest
It only displays test once like it's working but it keeps creating obj_pawn_dir. I use the variable do_once to do something once in the step event. Is their a better way of doing this?
 

Tsa05

Member
From just that code, not sure, but if you're wondering what's happening, click on the line that says do_once = false and press F9 (or click in the left margin beside the line number. When you run your code, it will pause your game at the moment when scr_avaliable_movements() has already happened and do_once is about to become false.

The debugger will pop up with that line highlighted, and you'll be able to move one line of code at a time through all the things that happen next; you can hover the mouse over a variable to see its value, or check the All Instances window in the debugger to see the values changing line-by-line. Somewhere, do_once is changing, or there's multiple player objects, or something. Stepping through will help.
 
P

Parker

Guest
I did that and nothing happened I am pretty sure there is nothing wrong with the do_once because when I debugged it saying test it only said it once.
 

Slyddar

Member
Well least you know it's running once. So you create an obj_pawn_dir object. Are you then destroying those objects again, and then they are being created again? How do you know it keeps creating them still? Are they all being created again every step, or just the one that got created the step before?
 

Tsa05

Member
Just to be clear, if you "did that and nothing happened" then, there *is* a problem.
It definitely should pause your game and permit you to step one line at a time through your code from that moment. If nothing happens, then your do_once = false statement is never being evaluated.
 
P

Parker

Guest
When I said nothing happened I mean that it was only called once so it said "test" once. Sorry it took me awhile to reply
 
P

Parker

Guest
I figured it out I renamed the obj_pawn_dir to obj_pawn_dir1 and it gave me an error in the player moment script that was getting called every step to create these around it. Thanks for all of the help I really appreciate it.
 
Top