WIth and Other

Hi

I'm a little confused about how with and other work. I wrote the following code:

Code:
if (mouse_check_button_released(mb_right))
{
    sprite_index = WindBatAttack;
    
    if (collision_line( x, y, mouse_x, mouse_y, BigBrainObject, false, true ))
    {
        x += 300;
        sprite_index = WindBatStill;
    }
    
    
}
I want the player to click on a brain and the collision line to push the enemy brain backwards. But instead of that, what is happening is the bat itself moves.
The about event is in the Step Event of the WindBatObjecft.
 
Sorry, originally I used an other before but I erased it before typed this post. And I had tried a with as well. Can I use other in the collision_line code?
 
N

NeZvers

Guest
Code:
// get ID of enemy and save it in variable
var enemy = collision_line( x, y, mouse_x, mouse_y, BigBrainObject, false, true );

//check if you have an ID
if (enemy != noone)
   {
       enemy.x += 300; // access to enemy X variable
       sprite_index = WindBatStill;
   }
But why you need collision_line if all you want is to click on enemy. For that I think better is to use collision_point( mouse_x, mouse_y, obj, BigBrainObject, true);
 
Last edited:
Judging by the number of topics about relatively small/easily fixable problems you've posted about the bat/brain game I'd say this project is beyond your current skill level. Perhaps scale it back and try something a little simpler first.
 
I

icuurd12b42

Guest
They still aren't pushing back. Could it have to do with that they are on a path?
yes, it's because you called path_start… you cant move the instance manually changing x,y or hspeed/vspeed…
 

TheouAegis

Member
Oh yeah, he's using paths in his game. lol I forgot about that.

Stop the path before trying to move it manually. o_O

But as for using with here, there's absolutely nothing wrong with it, contrary to what the others might seem to be suggesting.

with collision_line(x,y,mouse_x,mouse_y,BigBrainObject,false,true) //you don't need to set the notme argument though
{
x += 300;
other.sprite_index = WindBatStill;
}

This and what NezVers posted are nearly identical, although the method he used is a weeeeeeeeee bit faster. It boils down to a style choice. I personally use with() like this because the difference in speed wasn't significant enough to change; it also had some other uses for me that I won't get into.
 
C

CedSharp

Guest
Code:
// get ID of enemy and save it in variable
var enemy = collision_line( x, y, mouse_x, mouse_y, BigBrainObject, false, true );

//check if you have an ID
if (enemy != noone)
   {
       enemy.x += 300; // access to enemy X variable
       sprite_index = WindBatStill;
   }
But why you need collision_line if all you want is to click on enemy. For that I think better is to use collision_point( mouse_x, mouse_y, obj, BigBrainObject, true);
I would rewrite that code to make full usage of "with" and "other":
Code:
with(collision_line(x, y, mouse_x, mouse_y, BigBrainObjet, false, true)) {
  x += 300; // because of with() we are inside the enemy object
  other.sprite_index = WindBatStill; // because we are inside an "with()" we use "other" to access the original object
}
That's how I'd do it :)
 
Ok so this works. Except I'm trying to hop back onto the path and it doesn't like that. The brain flies backward and then disappears. First thought: They are going off screen. But when a brain is in the middle of the room he just disappears. Before I tried starting a new path the brain would simply slide backwards, but then be stationary because its not on a path.

Code:
if (global.push_back_trigger == true)
{
        show_debug_message("Push back trigger true");
        with(global.push_back_enemy)
        {
            global.wind_timer++;           
            show_debug_message("PUshing brains");
            path_end();
            global.push_back_enemy.x += global.wind_timer; // access to enemy X variable
            sprite_index = WindBatStill;
        }
}

if (global.wind_timer > 15)
{
    global.push_back_trigger = false;
    global.wind_timer = 0;
    //start path again
    
    with (BigBrainObject)
    {
    
        my_path = path_duplicate(path6);
        path_start(my_path, 5, path_action_stop, true);
        //alarm[0] = room_speed*20;
    }
}
 
N

NeZvers

Guest
@CedSharp Someone on forum tested with vs instance.variable change and more efficient way is instance.variable, but it all levels out with increased call count.

As to OP problem, you could have invisible object thats tied to path and brains are following it and when you want to kickback stop path object and kickback the brain, after that allow brain to approach() and when it reaches path object resume walking.
 
Last edited:

TheouAegis

Member
I don't like adding extra instances, but that's a decent idea by NeZvers. If you're using relative positioning of paths, then his idea would probably the simplest for you.


global.push_back_enemy.x += global.wind_timer; // access to enemy X variable
This is just
x+=global.wind_timer;
You don't need global.push_back_enemy. there because you are already inside global.push_back_enemy.


Lastly, I don't think you need reduplicate your path when you start the path back up. Using path_end() should just stop the path you are currently on. It doesn't delete it, so if anything you are creating a memory leak. Just resume the path you already were on and set path_position to where you were before the knockback.
 

spe

Member
Wait... do you just want to knock the object back along the path? Or do you need a more complicated knockback? If not, you could just use this:
Code:
path_position -= 0.1;
If this works, don't bother stopping and starting the path. This just sets the object back 10% of the way of the path. You can change that amount based on how long your path is. If the path is very long, you might want to use 0.01 instead. If it's very short, maybe 0.5 would be more appropriate. It's a percentage between 0 and 1 (where 1 represents the end of the path).

Alternatively, you can simply set the object to reverse at high speed on the path for a split second, then resume.
Code:
//Knockback
path_speed = -(path_speed * 10);
alarm[0] = 5;

//Resume following path
if alarm[0] < 0 {
    path_speed = abs(path_speed) / 10;
}
 
To start, spe, I'm reversing them directly backwards, not along a path. Although that might become useful later on.

@ThouAegis
Code:
I don't think you need reduplicate your path when you start the path back up. Using path_end() should just stop the path you are currently on. It doesn't delete it, so if anything you are creating a memory leak. Just resume the path you already were on and set path_position to where you were before the knockback. [/QUOTE]
What I want to do is knockback the brain and then have him start up on path again.
What they are doing is flying backwards (like they are supposed to) but then instead of hopping on a path when they run into the start point,
they just sit where they got knocked back to.


@ThouAegis: Ok. I'm saving path_position as global.old_path = path_position; before the path_end(). Then when the timer runs out, I do a
Code:
my_path = path_duplicate(path4);
            path_start(my_path, 5, path_action_continue, true);
            path_position = global.old_path;
            path_speed = 5;
Which is supposed to return them to the start path.

The full code is WindBatObject:

Code:
if (mouse_check_button_released(mb_right))
{
    var enemy = collision_point( mouse_x, mouse_y, BigBrainObject, false, true );
     if (enemy != noone)
     {
         with (enemy)
         {
            global.push_back_trigger = true;
            global.push_back_enemy = enemy;
            sprite_index = WindBatAttack;
         }
    }

}



if (global.push_back_trigger == true)
{
        sprite_index = WindBatAttack;
        //show_debug_message("Push back trigger true");
        with(global.push_back_enemy)
        {
            global.wind_timer++;           
            //show_debug_message("PUshing brains");
            global.old_path = path_position;
            
            path_end();

            if (x < 1500)
            {
                
                show_debug_message("Pushing back x value");
                x += global.wind_timer; // access to enemy X variable
                
            }
        }
}

if (global.wind_timer > 300)
{
    show_debug_message("Wind Timer: " + string(global.wind_timer));
    global.push_back_trigger = false;
    global.wind_timer = 0;
    //start path again
    sprite_index = WindBatStill;
    
    with (BigBrainObject)
    {
        //choose a random path
        var pathtype = irandom_range(0, 4);
        

        if (pathtype == 0)
        {
            my_path = path_duplicate(path4);
            path_start(my_path, 5, path_action_continue, true);
            path_position = global.old_path;
            path_speed = 5;
        
        }
        
        if (pathtype == 1)
        {
            my_path = path_duplicate(path5);
            path_start(my_path, 5, path_action_continue, true);
            path_position = global.old_path;
            path_speed = 5;
        
        }
        
        if (pathtype == 2)
        {
            my_path = path_duplicate(path6);
            path_start(my_path, 5, path_action_continue, true);
            path_position = global.old_path;
            path_speed = 5;
        
        }
        if (pathtype == 3)
        {
            my_path = path_duplicate(path7);
            path_start(my_path, 5, path_action_continue, true);
            path_position = global.old_path;
        path_speed = 5;
        
        }
        if (pathtype == 4)
        {
            my_path = path_duplicate(path8);
            path_start(my_path, 5, path_action_continue, true);
            path_position = global.old_path;
            path_speed = 5;
        
        }
        
        
        
        
    }
}
The
 

TheouAegis

Member
Your last part got cut off, or something. So what exactly is happening now?

Stop using path_duplicate. Use path_assign or delete my_path when the brain gets knocked back. Every time you call path_duplicate(), you are causing a memory leak.
 
TheouAegist:

Right now when I click on the bains they fly backwards back to their castle like they are supposed to. However, the bat, once it collides with the castle and stays still instead of following a path like they are supposed to. Then after about ten seconds they
are leaping into random paths


Create Event:

Code:
//new_path = path_duplicate(path4);


my_path = path_duplicate(path6);
mppath = noone;
path_start(my_path, 5, path_action_continue, true);
alarm[0] = room_speed*20;
Alarm 0

Code:
if (still == true)
{
    path_speed = 0;
    sprite_index = BigBrainStill;
    //alarm[1] = random_alarm*room_speed;
}

alarm[1] = 50;
Alarm 1

Code:
if (!under_attack)
{
//random_num = irandom(10);
//random_num = 3;
//
random_num = random_range(0, 10);
//random_num = 3;
if (random_num <= 10 && random_num > 5)
{
 mppath = path_assign(my_path,  path4);
}
else if (random_num <= 5 && random_num > 3)
{
 mppath = path_assign(my_path,  path5);
}
else if (random_num <= 3 && random_num >= 2)
{
 mppath = path_assign(my_path, path6);
}
else
{
 mppath = path_assign(my_path,  path7);
}


path_speed = 5;
sprite_index = BigBrainWalking;
//alarm[2] = random_alarm*room_speed;
//alarm[2] = 50;
}

Alarm 2

Code:
//Reset Brain's path and set an alarm to 3

//still=false keeps the brain from a traffic jam


if (still == true)
{
    path_speed = 0;
    sprite_index = BigBrainStill;
    //alarm[3] = random_alarm*room_speed;
}

//alarm[3] = 50;
Alarm 3

Code:
//append my_path to mppath
//append random path

if (!under_attack)
{
//random_num = irandom(10);
random_num = random_range(0, 10);

//random_num = 3;

//path_start(my_path, 5, path_action_stop, true);

path_speed = 5;


if (random_num <= 10 && random_num > 7)
{
    my_path = path_assign(mppath,  path4);

}

else if (random_num <= 7 && random_num > 4)
{
    my_path = path_assign(mppath,  path5);
}

else if (random_num <= 4 && random_num > 2)
{
    mppath = path_assign(my_path, path6);
}

else
{
    my_path = path_assign(mppath,  path7);
}

sprite_index = BigBrainWalking;

alarm[0] = 50;  //go to alarm 0 and reset paths
}
 
Update on the Create Event:

Code:
/new_path = path_duplicate(path4);


my_path = path_add();

path_assign(my_path, choose(path4, path5, path6, path7, path8));
mppath = noone;
path_start(my_path, 5, path_action_continue, true);
alarm[0] = room_speed*20;
 

TheouAegis

Member
Are they colliding with the castle or just moving back a bit toward it? I don't see anything outside the Create Event pertaining to anywhere near 10 seconds, unless all you're seeing is that alarm[0] finally going off.

Wait, your bat is supposed to be on a path too? The only code directly affecting the bat that I've seen here just sets its sprite_index. Or did you mean the brain and accidentally said "bat"?
 
They are flying backwards and colliding with the castle stopping when they collide. After approximately ten seconds (not in the code) hey jump to a random spot on the map. I would like it to begin a new path from where they flew back and collided. I misspoke earlier. These are brains not bats. And yes they are on paths.
 
Top