GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-06-17 07:29:12

You know, I had sort of that same idea after I posted here. In detecting correct key press, if pressed, change variable to true, if variable is still false increase by one. I will give yours a try.

Thanks again for the help.

#2 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-06-16 14:28:19

Ok, I guess it didn't bold or whatever,


    else {
        global.mistakes += 1;
    }

#3 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-06-16 14:22:26

One quick minor issue that I can't understand. This is the code that I am using for the movement based on character pressed. I am needing to count the number of incorrect keys.

Expandif (isMoving == false)
{
    // Disable keys by default.
    var key_up = -1;
    var key_down = -1;
    var key_left = -1;
    var key_right = -1;
    // Enable keys for neighboring instances, if any.
    // I'm assuming object_instance is a key code.

    key_up = collision_point(x, y-32, obj_letter, false, false);
    key_down = collision_point(x, y+32, obj_letter, false, false);
    key_left = collision_point(x-32, y, obj_letter, false, false);
    key_right = collision_point(x+32, y, obj_letter, false, false);

    if (instance_exists(key_up))
    {
        if (keyboard_check(key_up.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = 0;
            speedY = -moveSpeed;
            movingToward = key_up;
        }
    }
    if (instance_exists(key_down))
    {
        if (keyboard_check(key_down.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = 0;
            speedY = moveSpeed;
            movingToward = key_down;
        }
    }
    if (instance_exists(key_left))
    {
        if (keyboard_check(key_left.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = -moveSpeed;
            speedY = 0;
            movingToward = key_left;
        }
    }
    if (instance_exists(key_right))
    {
        if (keyboard_check(key_right.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = moveSpeed;
            speedY = 0;
            movingToward = key_right;
        }
    }
    else {
                global.mistakes += 1;
            }
}
if (isMoving == true)
{
    x += speedX;
    y += speedY;

    moveTimer -= moveSpeed;
    if (moveTimer == 0) isMoving = false;
}

In the bolded/italiced area, I just added a global.mistakes +=1 and it counts but it counts more than just wrong letters pressed. Any idea how I can count the wrong letters properly?

#4 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-06-05 14:24:23

Figured it out. I switch the Bounding Box on the sprites from Automatic to Full Image and that fixed it.

#5 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-06-05 10:42:30

I ended up kind of combing the two scripts. irandom works better than what they were using.

Anyway, again I appreciate the help. I am looking through the code. After some experimenting, if I have ghost_speed = 1, one of the enemies will move around the screen, 2 and two will move around the screen, 4 and all of them.

#6 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-06-04 21:24:32

You know, I didn't even look at the comments. I went a little different method but mostly the same. I pasted their code in place of mine and it works with the speed set at 4. I think the other issue I was having looking at it was technically my grid was 16 x 16 because I had an odd spacing at the top. Once I changed those two things, speed at 4 worked again. Unfortunately for my game it is very fast and need it slower. Based on what you are saying though I should be able to set speeds at 1 or 2 as well? They both divide into 32 cleanly (and 16) but they still make the player freeze.

Update: Switched back to my code with your fixes and the ghosts act much better than the other code though the setting of 4 for speed is still required..

#7 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-06-04 12:37:19

I followed a tutorial online by Ghazworks on Youtube. They did everything in Actions and my window was freaking out because of all of the actions so I converted it to GML. If I kept the speed at 4 for the enemies, they would move around fine with the Actions, but if it was less, it would get locked when hitting a wall. When I converted to GML, they just get stuck when hitting the first wall. So when enemy_speed = 4, it ran fine in actions, but not in GML.

Expandif (place_snapped(32,32)) {
    if (global.can_move = true) {
        direction = direction;
        speed = enemy_speed;
        which_way = 0;
        //horizontal movement
        if vspeed = 0 {
            //check for open position
            if (!place_meeting(x + hspeed, y, obj_wall)) {
                which_way = 3;
            }
            if (!place_meeting(x, y + hspeed, obj_wall)) {
                which_way = 1;
            }
            if (!place_meeting(x, y - hspeed, obj_wall)) {
                which_way = 5;
            }
            // check which way enemy is going
            
            // if going forward       
            if (which_way = 3) {
                exit;
            }
            
            // if going down
            if (which_way = 1) {
                direction = 270;
                exit;
            }
            
            // if going up
            if (which_way = 5) {
                direction = 90;
                exit;
            }
            
            // if going forward & down
            if (which_way = 4) {
                if irandom(1) = 0 {
                    if (!place_meeting(x, y + hspeed, obj_door)) {
                        direction = 270;
                        exit;
                    }
                }
            }
            
            // if going forward & up
            if (which_way = 8) {
                if irandom(1) = 0 {
                    direction = 90;
                    exit;
                }
            }
            
            // if going up & down
            if (which_way = 6) {
                if irandom(1) = 0 {
                    direction = 270;
                    exit;
                } else {
                    direction = 90;
                    exit;
                }
            }
            
            // if doing a three way :)
            if (which_way = 9) {
                if irandom(1) = 0 {
                    exit;
                }
            } else {
                if irandom(1) = 0 {
                    direction = 90;
                    exit;
                } else {
                    direction = 270;
                    exit;
                }
            }
            
            // if not moving
            if (which_way = 0) {
                direction = 180;
                exit;
            }
        }
        // end horizontal moving
        else {
        //vertical movement
            //check for open position
            if (!place_meeting(x, y + hspeed, obj_wall)) {
                which_way = 3;
            }
            if (!place_meeting(x + hspeed, y, obj_wall)) {
                which_way = 1;
            }
            if (!place_meeting(x - hspeed, y, obj_wall)) {
                which_way = 5;
            }
            // check which way enemy is going
            
            // if going forward       
            if (which_way = 3) {
                exit;
            }
            
            // if going right
            if (which_way = 1) {
                direction = 0;
                exit;
            }
            
            // if going left
            if (which_way = 5) {
                direction = 180;
                exit;
            }
            
            // if going forward & right
            if (which_way = 4) {
                if irandom(1) = 0 {
                    direction = 0;
                    exit;
                }
            }
            
            // if going forward & up
            if (which_way = 8) {
                if irandom(1) = 0 {
                    direction = 180;
                    exit;
                }
            }
            
            // if going up & down
            if (which_way = 6) {
                if irandom(1) = 0 {
                    direction = 0;
                    exit;
                } else {
                    direction = 180;
                    exit;
                }
            }
            
            // if doing a three way :)
            if (which_way = 9) {
                if irandom(1) = 0 {
                    exit;
                }
            } else {
                if irandom(1) = 0 {
                    direction = 0;
                    exit;
                } else {
                    direction = 180;
                    exit;
                }
            }
            
            // if not moving
            if (which_way = 0) {
                direction = 180;
                exit;
            }
        // end vertical moving
        }
    }  
}

#8 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-29 16:49:30

One more question, I am looking for something that makes enemies move around a maze moving towards the player. I have tried implementing some basic ones but they all just quickly move to were the player is. Do you know of one that could be implemented that has variations of random movement while progressing towards the player? IE, there are two close paths for the enemy to get to the player, instead of taking the shortest path, take the optional path instead. Or even just randomly move around the screen.

Thanks again for the help.

#9 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-28 20:43:21

Thank you, that worked from what I see. I will do some more testing. Sent you a PayPal as well.

#10 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-28 18:53:01

Thanks for all your help. Do you take donations? I am thankful for your help and it has helped me progress quicker while learning a few advanced features of GML.

Also, is there a bug with random/irandom? Whenever I run a test, it keeps the same letter objects. There is an area that does have repeated letter objects spaced accordingly. See attached image of screenshot section of game. And I color coded the repeats.

p3em.png

#11 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-27 16:49:02

That worked, thanks!  I did have to use object_instance = ord("A"); to have it typed as lower case a. Based on your description, I should have had to have ord("a") instead. Would I have to put something like object_instance = ord("A") && vk_shift; or something similar?

One last question I guess on the typing aspect, would it be possible to cache inputted letters? So if the user types in one the the next characters in line instead of waiting for the player object to stop, it would just continue right away moving?

Now I will look at the randomization of characters on screen.

Again thanks for the help!

#12 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-27 16:01:39

Ok, and again, thank you for the help!

Here are two images of what I am trying to do,

1st Image: Player (obj_player) starting, surrounded by letters (obj_letter). There is a letter underneath (J) the player as well in case they have to get back to that spot. So the player can hit either Z to go up, I to go left, K to go right, or T to go down. No other keys are available.

hqhx.png

2nd Image: Player presses Z to move up. They now have the option to press Y to go left, A to go right, or J (where the player was) to go back down. The blue = is a wall and they can't move that way so no option to move up.

smbw.png

That is the the main goal of the player.

Here is the code so far:

obj_player - Create

ExpandgridSize = 32;     // Should be power of 2 (...8,16,32...)
isMoving = false;  // Keeps track of when player is moving
moveTimer = 0;  // Counts down from grid size each step
moveSpeed = 1;     // Do not set higher than grid size!
speedX = 0;
speedY = 0;
image_speed = 1 / (room_speed / 10);

obj_player - Step

Expandif (isMoving == false)
{
    // Disable keys by default.
    var key_up = -1;
    var key_down = -1;
    var key_left = -1;
    var key_right = -1;
    // Enable keys for neighboring instances, if any.
    // I'm assuming object_instance is a key code.

    key_up = collision_point(x, y-32, obj_letter, false, false);
    key_down = collision_point(x, y+32, obj_letter, false, false);
    key_left = collision_point(x-32, y, obj_letter, false, false);
    key_right = collision_point(x+32, y, obj_letter, false, false);

    if (instance_exists(key_up))
    {
        if (keyboard_check(key_up.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = 0;
            speedY = -moveSpeed;
            movingToward = key_up;
        }
    }
        if (instance_exists(key_down))
    {
        if (keyboard_check(key_down.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = 0;
            speedY = moveSpeed;
            movingToward = key_down;
        }
    }
        if (instance_exists(key_left))
    {
        if (keyboard_check(key_left.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = -moveSpeed;
            speedY = 0;
            movingToward = key_left;
        }
    }
        if (instance_exists(key_right))
    {
        if (keyboard_check(key_right.object_instance))
        {
            isMoving = true;
            moveTimer = gridSize;
            speedX = moveSpeed;
            speedY = 0;
            movingToward = key_right;
        }
    }
 }   
if (isMoving == true)
{
   x += speedX;
   y += speedY;
   
   moveTimer -= moveSpeed;
   if (moveTimer == 0) isMoving = false;
}

No other code in obj_player.

obj_letter (Parent) - Create

Expandvar object_instance;
image_speed = 1 / (room_speed / 10);

obj_letter_A (child) - Create

Expandevent_inherited();
object_instance = "a";

obj_letter_B = "b" and so on through the alphabet. I have no other code outside of generating the random letters.

#13 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-27 07:37:29

That is exactly what I was looking for. One issue I am not sure how to resolve.

Expand    with (collision_point(x, y-32, obj_letter, false, false)) key_up = object_instance;
    with (collision_point(x, y+32, obj_letter, false, false)) key_down = object_instance;
    with (collision_point(x-32, y, obj_letter, false, false)) key_left = object_instance;
    with (collision_point(x+32, y, obj_letter, false, false)) key_right = object_instance;

You are correct that object_instance refers to the letter that the player has to type. How do I reference it with the specific obj_letter objects depending on the direction it is going? I did this similarly with the instance_nth_nearest but again it was looking at all positions, not just up,down,left,right.


Here is one other question on something I need help on. With the letters being created in the grid, I am randomly generating them in the grid using irandom. My problem is how can I look at the surrounding other objects to make sure they aren't the same so if I press say S, there aren't multiple S' in different direction?

EDIT: Running as is, the player moves automatically on start with no keyboard control of movement. If I hit a key it will pause when it reaches the 32x32 cube defined in code, but once I release it just continues moving again. The only thing I can think of is that I am defining the object_instance variable in each letter object. I don't see anything wrong with the code. I have a parent object (obj_letter) being referenced in each child object. Each child object under the create is defining object_instance = "?" where the ? is the letter as defined in each obj_letter child (a,b,c,d,...).

TSV
S*N
RUP

Player is the asterisk, so in the example, pressing S would either take me up or down but I want to avoid having duplicates. Here is how I am filling the screen:

Expand//randomly display alphabet in room
var random_x
var random_y
var object_random
random_x = 32
random_y = 32
object_random=0
while (random_x <= 608 && random_y <= 736){
    object_random = irandom_range(1,26)
        if object_random = 1{
            instance_create(random_x,random_y,obj_letter_A)
            if random_x < 608{
                random_x += 32
            } else {
                random_x = 32
                random_y += 32
            }
        }
        (repeats for each letter (A - Z), object_random(1 - 26)
}

Thanks!

#14 Re: GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-26 21:55:30

Thanks for the reply, Ok I have it implemented ( I tried collision_line_list previously) but the character doesn't move when the appropriate key is pressed.

Here is the obj_player - collision -w/ obj_letter

Expand    inst_right = collision_line_first(x,y,x+32,y,obj_letter,true,false);
    inst_left = collision_line_first(x,y,x-32,y,obj_letter,true,false);
    inst_up = collision_line_first(x,y,x,y-32,obj_letter,true,false);
    inst_down = collision_line_first(x,y,x,y+32,obj_letter,true,false);

obj_player - step event

Expandif ( keyboard_check_pressed(vk_anykey))
    {
        if keyboard_lastchar = inst_up.object_instance {
            if point_distance(x, y, inst_up.x, inst_up.y) <> 1 {
                image_angle = point_direction (x,y,inst_up.x,inst_up.y);
                move_towards_point(inst_up.x, inst_up.y, 1);
            } 
            else {
                inst_up = "";
                speed = 0;
            }
        }
        
        if keyboard_lastchar = inst_down.object_instance {
            if point_distance(x, y, inst_3.down, inst_down.y) <> 1 {
                image_angle = point_direction (x,y,inst_down.x,inst_down.y);
                move_towards_point(inst_down.x, inst_down.y, 1);
            }
            else {
                inst_down = "";
                speed = 0;
            } 
        }
    
        if keyboard_lastchar = inst_left.object_instance {
            if point_distance(x, y, inst_left.x, inst_left.y) <> 1 {
                image_angle = point_direction (x,y,inst_left.x,inst_left.y);
                move_towards_point(inst_left.x, inst_left.y, 1);
            }
            else {
                inst_left = "";
                speed = 0;
            } 
        }
    
        if keyboard_lastchar = inst_right.object_instance {
            if point_distance(x, y, inst_right.x, inst_right.y) <> 1 {
                image_angle = point_direction (x,y,inst_right.x,inst_right.y);
                move_towards_point(inst_right.x, inst_right.y, 1);
            }
            else {
                inst_right = "";
                speed = 0;
            } 
        }
    }

I have been hitting my head on the table this whole weekend on this. Thanks for the help.

#15 GML Code Help » Help with instance_nth_nearest or something similar » 2014-05-26 18:43:40

koltz
Replies: 27

I am creating a game that needs something like instance_nth_nearest but not quite. What I need is something like instance_nth_nearest, but only looking at 90 degree angles (up,down,left,right) and only within a certain length (32 pixels) of one object type. If that object type isn't in a direction, that direction is ignored. The player then hits a key on the keyboard to move the player in that specific direction with the matching object and it should stop when on top of that object (32 pixels). This continues with the instance updating to the newest nearest objects. When they press a matching key, it repeats itself over and over while ignoring the object the player is directly on. I was looking at range_finder but it doesn't return the instance.

Thanks for any help.

Board footer

Powered by FluxBB