GameMaker Help with tile collision

T

Thatonegamedeveloper

Guest
upload_2019-6-27_2-38-55.png

Hi I keep having a problem with tile collision in game maker studio 2, I tried following this tutorial but it didn't work out. Is there a way to add multiple collision to different tile for example if I add one collision set to a tile and add another collision set to another layer of tiles.

The tutorial:
 

Rob

Member
I'm sorry but you didn't give enough info to go on. How are you doing collisions exactly, what is/isn't working and what are you trying to do?

Show your code using the "insert" tool
 

Slyddar

Member
If you are following that tutorial, one of the arguments of the tile_meeting script he creates, is a tilemap. You can pass any tilemap to the script and find if the coordinates collide with any tile on that tilemap.

You could also modify the script to take a tile number as well, so you can calculate if there is a collision with a specific tile.
 
T

Thatonegamedeveloper

Guest
If you are following that tutorial, one of the arguments of the tile_meeting script he creates, is a tilemap. You can pass any tilemap to the script and find if the coordinates collide with any tile on that tilemap.

You could also modify the script to take a tile number as well, so you can calculate if there is a collision with a specific tile.
Yeah, the whole player goes through the tile I try to add code the end step of obj_player. I didn't use tile_meeting because it doesn't show up as a function.

Code:
/// @description Insert description here
// You can write your code in this editor


if (tilemap_get (x + spdx, y, "Tiles_1"))
{
    while (!tilemap_get(x + sign (spdx) ,y, "Tiles_1"))
    {
        x += sign (spdx);
    }
    spdx = 0;
}

if (tilemap_get(x , y + spdy, "Tiles_1"))
{
    while (!tilemap_get(x ,y + sign (spdy) , "Tiles_1"))
    {
        y += sign(spdy);
    }
    spdy = 0;
}

x += spdx;
y += spdy;
 
T

Thatonegamedeveloper

Guest
It's not a function, but the v
It's not a function, but the video shows you the code to use. You need to create a script and use that code so then you will be able to use it as a function.
ideo shows you the code to use. You need to create a script and use that code so then you will be able to use it as a function.
I edit the code to add tile_meeting turn out I just need to rename the script I keep getting error code
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event2
for object obj_player:

tilemap_get_at_pixel argument 1 incorrect type (string) expecting a Number (YYGI32)
 at gml_Script_tile_meeting (line 21) - meeting =              tilemap_get_at_pixel(tilemap, bbox_right, bbox_top)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_tile_meeting (line 21)
called from - gml_Object_obj_player_Step_2 (line 5) - if (tile_meeting(x + spdx, y,"Tiles_1"))
I look over the script in the tutorial but can't seem to find a solution.
 

TsukaYuriko

☄️
Forum Staff
Moderator
A tutorial shouldn't be your go-to resource when you encounter error messages - it should be the manual.

The message states that something is wrong with argument 1 of a function in a specific line of code. The function seems to be tilemap_get_at_pixel. According to its manual entry, its first argument expects a number. Did you provide it with a number or something else?
 

Slyddar

Member
I edit the code to add tile_meeting turn out I just need to rename the script I keep getting error code
You are getting an error because the tutorial is incorrect. He is passing the name of the layer as a string, but as mentioned by @TsukaYuriko it is expecting a number. It only worked for the guy in the tutorial due to blind luck (his layer ended up being evaulated as a 0).
You need to replace the code in the script with code that converts the string layer name into a layer_id, like this:
Code:
xx = argument0;
yy = argument1;
tilemap = layer_tilemap_get_id(argument2);
 
T

Thatonegamedeveloper

Guest
You are getting an error because the tutorial is incorrect. He is passing the name of the layer as a string, but as mentioned by @TsukaYuriko it is expecting a number. It only worked for the guy in the tutorial due to blind luck (his layer ended up being evaulated as a 0).
You need to replace the code in the script with code that converts the string layer name into a layer_id, like this:
Code:
xx = argument0;
yy = argument1;
tilemap = layer_tilemap_get_id(argument2);
Hi I enter th script in but the player still pass through the tiles.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Whenever you perform changes or follow instructions provided to you via replies, please post an updated version of all relevant code so we can see what you changed where - if we have to puzzle this together ourselves, that's another potential problem source and we can never be sure whether our finished puzzle matches reality, either.
 
T

Thatonegamedeveloper

Guest
Please post your script, and the line of code you have to call the script.
Code:
///@arg x
///@arg y
///@arg tilemap

var xx, yy, tilemap, xp, yp, meeting;

xx = argument0;
yy = argument1;
tilemap = layer_tilemap_get_id(argument2);

//save our current position
xp = x;
yp = y;

//move to the position where we wanna check for a tile collision
x = xx;
y = yy;

//check for collision on all four corners of the collision mask
meeting =        tilemap_get_at_pixel(tilemap, bbox_right, bbox_top)
                ||
                tilemap_get_at_pixel(tilemap,bbox_right,bbox_top +sprite_height/2)
                ||
                tilemap_get_at_pixel(tilemap, bbox_left, bbox_top + sprite_height/2)
                ||
                 tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom)
                ||
                tilemap_get_at_pixel(tilemap, bbox_left, bbox_top)
                ||
                tilemap_get_at_pixel(tilemap, bbox_left + sprite_width/2, bbox_top)
                ||
                tilemap_get_at_pixel(tilemap, bbox_left + sprite_width/2, bbox_bottom)
                ||
                tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom);

//Move back to the original position
x = xp;
y = yp;

//Return wether or not there was a collision
return(meeting);
upload_2019-6-30_5-20-54.png


upload_2019-6-30_5-21-36.png
 

Slyddar

Member
Not sure if it's relevant, but if you are flipping the image_xscale/yscale at any point, you need to abs() the sprite_width and sprite_height variables.

Now what does the step event look like where you are calling the script?
 
Top