[SOLVED] layer_script_begin Error (script with arguments)

Discussion in 'Programming' started by shnizl, Jul 19, 2019.

  1. shnizl

    shnizl Member

    Joined:
    Jul 19, 2019
    Posts:
    3
    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.
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,959
    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.
     
    shnizl likes this.
  3. shnizl

    shnizl Member

    Joined:
    Jul 19, 2019
    Posts:
    3
    Thanks a lot :) I am glad there is such a simple solution to that, I will try it in the evening
     
  4. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    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.
     
    shnizl likes this.
  5. shnizl

    shnizl Member

    Joined:
    Jul 19, 2019
    Posts:
    3
    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
     
  6. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice