• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code What's the best way to do tile_get_ids_at_depth?

Nocturne

Friendly Tyrant
Forum Staff
Admin
There is no real equivalent in GMS2... Tiles are no longer individual entities and instead are an array of data "blobs" associated with a tilemap element, so by getting the element ID you then have access to all the data for the tiles on that element. Maybe tell us what you want to actually DO with the tile data and we can explain how you go about it?
 

spacerobot

Member
I am trying to have sort of a secret area where when the player is within a certain area the tiles in that area will fade the alpha to 0. Then when they leave the tiles fade back. I used to get a list of tiles in a certain x and y area on a certain depth to do that.

I'm thinking now after your response and reading a little that it might be easier to just set up a layer with the tiles I want to fade then fade the layer if that's possible?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Currently, you can't fade the alpha of a group of tiles on a tilemap. This is something that will be coming, but currently tiles are either visible or they're not. Look at maybe using an asset layer with sprites on it?
 

spacerobot

Member
Ahh gotcha.

Yes an asset layer with sprites would work too. Can all the sprites be faded at the same time or is that sort of the same issue as the tile layer fades?
 

mMcFab

Member
Another option is to use a shader and pass the alpha as a uniform using "layer_script_begin" and "layer_shader" - this will fade all drawings on any layer type for the targeted layer (including tile layers and asset layers).
 

mMcFab

Member
Sure! Here's a quick explanation of setting up a transparency manipulating shader for a certain layer (I'm writing it as if I were writing a tutorial, so you may already know some of this stuff, this just helps anyone else who may search the forum):
The first thing you'll need to do is create a shader and give it a sensible name (I'll be referring to it as "sh_alpha_multiply", as it will multiply the alpha value of the drawn elements). Switch to the fragment tab, as we can leave the passthrough vertex shader as-is.
We will simply update the fragment shader code to look like this (Copy and pastable):
Code:
//
// Simple passthrough fragment shader wih alpha multiplication
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform float alpha;

void main()
{
    vec4 col = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
    col.a *= alpha;
    gl_FragColor = col;
}
The differences between the standard shader and this are as follows
  • There is the line "uniform float alpha;" - this is the variable that we pass the multiplying value to from GameMaker
  • I have broken the default gl_FragColor line down, simply moving the code up and assigning it to the vec4 variable "col" - I only do this to make the action of multiplying alpha more human-readable
  • I then multiply the alpha of "col" by the uniform we pass and set gl_FragColor (the output of the shader) to col.
So, that's the shader done! They are actually pretty straightforward when you get to know them, but insanely powerful (especially if you get into vertex shaders)

Now we simply need a script to set the alpha value of the shader (we can set this after shader_set normally, but we need a script if we want it to work nicely with a layer shader)

So, in order to pass the alpha value to the shader, we need to get the uniform ID. This will be done with the "shader_get_uniform(shader, uniformName)" function. We then set it with "shader_set_uniform_f(uniformID, Value)". I'll have all this info dealt with in one script, though sometimes you may find it beneficial to get this ID in create to save processing time later.

Here's a script that will simply make the layer fade in and out with time. Simply adjust the var "alpha" to change behaviour to what you desire:
Code:
var uniformID = shader_get_uniform(sh_alpha_multiply, "alpha")
var alpha = 0.5 + lengthdir_x(.5, current_time/10);
shader_set_uniform_f(uniformID, alpha);
Just to note, the var does not need the same name as the uniform.

I have named the script "scr_layer_alpha" for this guide.

The only thing left to do is bind the script and shader to a layer - after that, it's pretty much done, though you may want to tweak bits of it.
This code can go in pretty much any event, though it's probably best suited for Create, Room Start or even room creation code. Let's assume the name of the layer you want to apply this script to is "Tiles_1" - substitute the name or ID as necessary.
Code:
var layer_id = layer_get_id("Tiles_1");
layer_shader(layer_id, sh_alpha_multiply);
layer_script_begin(layer_id, scr_layer_alpha);
The shader seems to automatically be reset after the layer has drawn, so there is no need to call "shader_reset()" afterwards.
And that's everything you need to do!

I've tested this so it should work - if there are any issues with the code, or you want me to explain anything else, just let me know!

Hope this helps!
 

mMcFab

Member
I'm getting some errors on this in the compile window. I am attaching a screenshot. Any ideas on what this means?
It looks like you might have pasted the shader code into the vertex shader tab (the errors indicate this at least)
Just to be clear, the two shader tabs should look like this:
Vertex (shFadeAlpha.vsh tab)
Code:
//
// Simple passthrough vertex shader
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
  
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
}
Fragment (shFadeAlpha.fsh tab)
Code:
//
// Simple passthrough fragment shader with alpha multiplication
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform float alpha;

void main()
{
    vec4 col = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
    col.a *= alpha;
    gl_FragColor = col;
}
If this isn't the case, post the shader code and I'll have a look at it :)
 
Top