GML Need help with Coding interactable objects

K

Krano Network Squirrell

Guest
So I am kinda new to GML and the Logic of how the Code Works. So after I got movement down, I feel the need to try and get this to work.

So what I am trying to get it where when you press a button, then it make the area of 1 (when facing this direction) to set a number from 0 to 1, and that if there is an object in which you can use or pick up in that area, then it would be used or picked up. and that the green area would also be set from 0 to 1, then any special object in there is activated. Here is the code I have for it so far:
Code:
//animation

if (moveH > 0)
    {
        sprite_index = spr_neko_walkF
    }
    
if (moveH < 0)
    {
        sprite_index = spr_neko_walkB
    }

if (moveV > 0)
    {
        sprite_index = spr_neko_walkR
    }
    
if (moveV < 0)
    {
        sprite_index = spr_neko_walkL
    }
    
if( hsp == 0 && vsp == 0 )
{
    image_index = 0
    image_speed = 0
}
else
{
    image_speed = 1
}

///Interaction
//get inputs
key_use = keyboard_check(ord("E")) || (gamepad_button_check(0, gp_face3)) || keyboard_check(vk_space)
key_pause = keyboard_check(vk_escape) || gamepad_button_check(0, gp_start) || keyboard_check(vk_backspace)

//react to imputs
if
 

Attachments

Simon Gust

Member
you can use a loop to automatically scan all 4 directions by using 90° angle intervals.
Code:
if (key_use)
{
    for (var i = 0; i < 360; i += 90)
    {
        var inst = instance_place(x + lengthdir_x(16, i), y + lengthdir_y(16, i), obj_interactable);
        if (inst != noone)
        {
            // do something with interactable
            inst.used = true;
        }
    }
}
in the loop, i is 0, 90, 180 and 270.
Using these angles you can use the function lengthdir_x() and lengthdir_y()
to get the x and y position of the place you want to check (at a distance of 16 from your player x and y)
if i is 0, lengthdir_x will return 16, lengthdir_y will return 0
if i is 90, lengthdir_x will return 0, lengthdir_y will return -16
and so on until all four places have been compromised.

Then, instance_place() will seek out all instances of obj_interactable
and if it finds one at the given coordinates, it will put the instance id into the variable "inst".
You can do whatever you want with inst like setting a variable or changing a variable.
if instance_place doesn't find an instance, inst will be noone (i.e. no instance found)
and the code will not continue.
 
K

Krano Network Squirrell

Guest
you can use a loop to automatically scan all 4 directions by using 90° angle intervals.
Code:
if (key_use)
{
    for (var i = 0; i < 360; i += 90)
    {
        var inst = instance_place(x + lengthdir_x(16, i), y + lengthdir_y(16, i), obj_interactable);
        if (inst != noone)
        {
            // do something with interactable
            inst.used = true;
        }
    }
}
in the loop, i is 0, 90, 180 and 270.
Using these angles you can use the function lengthdir_x() and lengthdir_y()
to get the x and y position of the place you want to check (at a distance of 16 from your player x and y)
if i is 0, lengthdir_x will return 16, lengthdir_y will return 0
if i is 90, lengthdir_x will return 0, lengthdir_y will return -16
and so on until all four places have been compromised.

Then, instance_place() will seek out all instances of obj_interactable
and if it finds one at the given coordinates, it will put the instance id into the variable "inst".
You can do whatever you want with inst like setting a variable or changing a variable.
if instance_place doesn't find an instance, inst will be noone (i.e. no instance found)
and the code will not continue.
Thanks I will try this out!
 
Top