• 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!

[SOLVED] layer_script_begin Error (script with arguments)

S

shnizl

Guest
Heya :)

Is it not possible to use a script with passing arguments in layer_script_begin()?

I am trying to use a shader that constantly increases the alpha value of a tile layer, then decreasing, increasing and so on. In my Objects Create Event i assigned a variable _time that changes in a step event and sets the alpha value with _speed as a uniform variable in the shader. Those are used in the the script scr_shd_alpha_begin() within layer_script_begin. Looks like this:


// Create Event
_time = 0;
_speed = 1.0;

var lay_id = layer_get_id("Wand_Lava");

layer_script_begin(lay_id, scr_shd_alpha_begin(_time, _speed));
layer_script_end(lay_id, scr_shd_alpha_end);


// Script scr_shd_alpha_begin
var _time = argument0;
var _speed = argument1;
if event_type == ev_draw
{
if event_number == 0
{
_uniTime = shader_get_uniform(shd_alpha, "u_time");
_uniSpeed = shader_get_uniform(shd_alpha, "u_speed");
shader_set(shd_alpha);
shader_set_uniform_f(_uniTime, _time);
shader_set_uniform_f(_uniSpeed, _speed);
}
}

When i run it i get this Error:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_camera_matrix:

illegal access of argument, argument is not provided to script
at gml_Script_tile_collide_at_points (line 3) - var tile_map_id = argument[0];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_tile_collide_at_points (line 3)

object obj_camera_matrix is unused and all its code is marked as comments. gml_Script_tile_collide_at_points is from a tutorial, has no correlation to the other script and actually works fine. The shader works on other objects and tiles and the script also works if i dont transmit arguments but define variables in it. But then i don't see how to change them constantly as i do in the step event.

I am really getting desperate here :( If you have any suggestions it'd make my day.
 

NightFrost

Member
Is it not possible to use a script with passing arguments in layer_script_begin()?
It is not, to my knowledge, since the script call is not related to any object and not called in scope of any instance, but instead directly by GM outside any scope. I've use both globals and objectname.variablename notation to pass information to layer scripts.
 
S

shnizl

Guest
Thanks a lot :) I am glad there is such a simple solution to that, I will try it in the evening
 
A

Annoyed Grunt

Guest
You are misunderstanding how functions work. Each script is represented by an integer (the script id). This value is a constant you can access through your script name. Let's pretend your scr_shd_alpha_begin has id equal to 10.

Code:
var a = scr_shd_alpha_begin //a == 10
When you call the script like this

Code:
scr_shd_alpha_begin(_time, _speed)
You are saying, "execute script with id 10 and pass it arguments _time and _speed, and gets its return value". That is, when you call a script, the return value is replaced in the expression. That's why we can do math and other stuff with functions. If I have a script cube with script id 32 that returns the cube of a value, then that means:

Code:
var a = cube; //a == 32
var b = cube(4) //b == 256;
Importantly, if a script does not have a return statement in it, then it returns 0 by default. Which leads us to your problem:

Code:
layer_script_begin(lay_id, scr_shd_alpha_begin); //equivalent to layer_script_begin(lay_id, 10)
layer_script_begin(lay_id, scr_shd_alpha_being(_time, _speed); //equivalent to layer_script_begin(lay_id, 0)
So that means layer_script_being is not executing the script you want (the one with id 10) but rather the one with id 0, which just so happens to be tile_collide_at_points.
 
S

shnizl

Guest
Thanks for your explanation, i knew about returns, the thing with script id was new. But why would the id of a function change when i pass it with arguements? You even said it means "execute with same id and pass arguements". And why is the problem about returning 0? The return value stays the same with and without arguments.

Nevertheless, it finally works now with globals, yay :D
 
A

Annoyed Grunt

Guest
Thanks for your explanation, i knew about returns, the thing with script id was new. But why would the id of a function change when i pass it with arguements? You even said it means "execute with same id and pass arguements". And why is the problem about returning 0? The return value stays the same with and without arguments.

Nevertheless, it finally works now with globals, yay :D
The function layer_script_being requires a value (number). If you pass it the name of the script, which is a constant, you're effectively passing the function the script's id. However, if you call the script, then the value is the returned value by the execution. Since your script does not actively return anything, it defaults to zero.
 
Top