Step Injection

Discussion in 'Tutorials' started by Battlenus, Dec 3, 2018.

  1. Battlenus

    Battlenus Member

    Joined:
    Dec 3, 2018
    Posts:
    2
    GM Version: 2.2.0
    Target Platform: All
    Download: N/A
    Links: N/A

    Summary:
    This is my step injection code allows instances to add code in the step event of other instances.

    Tutorial:
    The purpose of this code will allow sharing step code and have instances to have multiple states at once.

    First add this code to the create, step, and destroy to a parent object that will have the step code change over time.

    Code:
    /// scStepInjCreate()
    
    // Stores argument while the script string is the key
    stepMap = ds_map_create();
    
    // Stores scripts
    stepKeys = ds_list_create();
    
    Code:
    /// scStepInjRun(id)
    ///@param id
    
    var host = argument0;
    var stepMap = host.stepMap;
    var stepKeys = host.stepKeys;
    
    // Run every script in the list
    var i = 0;
    repeat ds_list_size(stepKeys) {
        var script = ds_list_find_value(stepKeys, i);
        var scriptArray = stepMap[? script_get_name(script)];
    
        // Trigger the script within the host
        with host script_execute(script, scriptArray);
        i += 1;
    }
    
    Code:
    /// scStepInjDestroy()
    
    ds_map_destroy(stepMap);
    ds_list_destroy(stepKeys);
    

    This will store a list of keys and a map of scripts that will run every step.
    To add actions, use this script.

    Code:
    /// scStepInjAdd(id, script, args)
    ///@param id
    ///@param script
    ///@param args
    
    var host = argument[0];
    var script = argument[1];
    var scriptName = script_get_name(script);
    
    // Store all arguments into an array
    var args = [];
    if argument_count > 2 {
        var num_args = argument_count - 2;
        var i = 0;
        repeat num_args {      
            args[i] = argument[i + 2];
            i++;
        }
    }
    
    host.stepMap[? scriptName] = args;
    ds_list_add(host.stepKeys, script);
    

    To remove a script:

    Code:
    /// scStepInjRemove(id, script)
    ///@param id
    ///@param script
    
    var host = argument0;
    var script = argument1;
    var scriptName = script_get_name(script);
    var stepKeys = host.stepKeys;
    ds_list_delete(stepKeys, ds_list_find_index(stepKeys, script));
    ds_map_delete(host.stepMap, scriptName);
    

    To remove all scripts:

    Code:
    /// scStepInjClear(id)
    ///@param id
    
    var host = argument0;
    ds_list_clear(host.stepKeys);
    ds_map_clear(host.stepMap);
    
    Now you can have an object do multiple actions like jump and shoot like:

    Code:
    if gamepad_button_check_pressed(0, gp_face1) && ds_list_find_index(oPlayer.stepKeys, scrJump) != -1 {
        scStepInjAdd(oPlayer, scrJump);
    }
    if gamepad_button_check_pressed(0, gp_face2) && ds_list_find_index(oPlayer.stepKeys, scrShoot) != -1 {
        scStepInjAdd(oPlayer, scrShoot, direction);
    }
    

    Then at the end of the script action, it can remove itself:

    Code:
    /// scrJump()
    
    Jump code...
    
    if place_meeting(x, y + fallSpeed, oSolid) {
        scStepInjRemove(id, scrJump);
    }
    
     
    Last edited: Dec 4, 2018
  2. chance

    chance predictably random Forum Staff Moderator

    Joined:
    Apr 22, 2016
    Posts:
    774
    GM users who are familiar with parent-child code relationship should be able to understand your approach, and your top-level description. But regardless of experience level, I think most members would appreciate some descriptive comments in the code itself, just to make things clear.

    Also, a simple example showing how this is used would also be helpful.
     
  3. Battlenus

    Battlenus Member

    Joined:
    Dec 3, 2018
    Posts:
    2
    Thank you for the quick reply. I have made the changes.

    Maybe the scStepInjAdd should do nothing if the object already has the script, but that would prevent objects to have duplicate scripts.
     

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