I am making a TD without paths.

Discussion in 'Programming' started by WimpyLlama, Feb 22, 2018.

Tags:
  1. WimpyLlama

    WimpyLlama Member

    Joined:
    Feb 9, 2018
    Posts:
    58
    I am trying to make a Tower Defence game without using paths because I want the map to be randomly generated. I have not started work on the random generation yet, but I am trying to get enemy's to walk across the map. In order to do this, I created 4 path objects, each one made to move the enemy object in different directions. The problem is that it is set up so that if the enemy touches the path it moves in the corresponding direction, but when it goes from up to right or down to left, vice versa, the enemy travels on the edge. I know why this is happening, I just don't know how to fix it. Please help. Here is my code.
    Code:
    Step event for enemy.
    
    
    //Set Movement
    x = x + hsp;
    y = y + vsp;
    
    //Move On Path
    if (place_meeting(x,y,obj_path_right)) {
    
        hsp = walksp;
        vsp = 0;
    
    } else if (place_meeting(x,y,obj_path_left)) {
    
        hsp = -walksp;
        vsp = 0;
    
    } else if (place_meeting(x,y,obj_path_up)) {
    
        vsp = -walksp;
        hsp = 0;
    
    } else if (place_meeting(x,y,obj_path_down)) {
    
        vsp = walksp;
        hsp = 0;
    
    }
    
     
  2. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    182
    An array or ds_grid?

    You can spawn the path first. Then set direction by number. Something like:

    0 = keep going/notta here.
    1 = up;
    2 = down;
    3 = right;
    4 = left;

    5 = tower 1;
    6 = tower 2;
    .
    .
    .

    You can also use the above code for enemies too.
     
  3. WimpyLlama

    WimpyLlama Member

    Joined:
    Feb 9, 2018
    Posts:
    58
    I did not think of that. That will help with compacting it down, but it still won't solve my main problem.
     
  4. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    182
    When you create a grid or array you can slice your room up the same and simply draw your enemies to the middle of the path. Should work. You might have to do a change variable to point_direction them the correct way...
     
  5. WimpyLlama

    WimpyLlama Member

    Joined:
    Feb 9, 2018
    Posts:
    58
    How exactly would I do that? I am new to the coding part of game maker. I have been doing drag and drop for a while so I wanted to learn some gml. I don't know anything about arrays. Please don't think I am some noob who knows nothing. I know something's, just not everything.
     
  6. VacantShade

    VacantShade Member

    Joined:
    Aug 8, 2016
    Posts:
    23
    If I understand correctly, the issue is that your "enemies" are touching the "path" objects and then changing direction without being in the center of the path, correct?

    If so, you could do an array/grid system like @Gamebot mentioned. Or, if you want to stick to your current setup of using objects, here is a simple solution:

    Check to see if you are within a specified distance of the Path object's ( x ) or ( y ) before changing direction.
    To do that, we can use instance_place instead of place_meeting. They are nearly the same, but instance_place returns the id of the object found, or noone if there are no collisions.
    It might look something like this:
    Code:
    i = instance_place( x , y , obj_path_right );
    if( i != noone )
    {
        if( abs( y - i.y ) < speed )
        {
              // Change speed/direction here
        }
    }
    i = instance_place( x , y , obj_path_up );
    if( i != noone )
    {
        if( abs( x - i.x ) < speed )
        {
              // Change speed/direction here
        }
    }
    i = instance_place( x , y , obj_path_left );
    if( i != noone )
    {
        if( abs( y - i.y ) < speed )
        {
              // Change speed/direction here
        }
    }
    i = instance_place( x , y , obj_path_down );
    if( i != noone )
    {
        if( abs( x - i.x ) < speed )
        {
              // Change speed/direction here
        }
    }
    
    This certainly isn't a very efficient solution, but it should work with your current setup.
    The code " if( abs( x - i.x ) < speed ) " basically checks to see if the enemy would pass over the center ( x ) of the path on the next frame.

    You might still notice a visual offset of a few pixels, depending on the speed of your enemies. To remedy this, you should snap the enemy's ( x ) or ( y ) to the path object's.

    For example, if changing the enemy's direction to [ RIGHT ] or [ LEFT ], we want his ( y ) position to be equal to the ( y ) position of the path object. You could do that when you change the direction.


    The basic concept is that we only want to change the enemy's direction once it is in the center of the path.
     
  7. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,892
  8. WimpyLlama

    WimpyLlama Member

    Joined:
    Feb 9, 2018
    Posts:
    58
    First thing, thank you all for the help. I still can't figure this out. I tried with ds grids and that didn't work. I tried with if statements, for statements, and even do statements, but I just can't figure this out. All I want is to move an object across a path without having to use the paths in game maker. I am trying to do this because I want to randomly generate the paths. If anyone can help me with trying to get the object to walk across the path, that would be amazing.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice