Extension How To Manage Controllers Without Using The Room Manager

Discussion in 'Tutorials' started by devonhg, Mar 10, 2019.

  1. devonhg

    devonhg Member

    Joined:
    Jun 21, 2016
    Posts:
    25
    GM Version: GMS 2 ( Can be easily modified for older versions, just replace the instance create calls )
    Target Platform: ALL
    Download: Pastebin | Github | GMC Marketplace
    Links: More Detailed Article

    Summary:
    In short, this is how you can manage all of your controllers in one code window, instead of jumping around and trying to find invisible objects in rooms.

    Typically if you're putting a controller into your game you open up a room you want the controller to be in and drag it in. This is fine.

    But what if you have many rooms, and you need specific code to be ran in specific rooms? The existing options are to either open up each and every room and manually drop your control object into them, or create a persistent object with a switch statement.

    Using this simple extension, you have a third option, one where you can manage all of your games controllers in one simple code window.

    Tutorial:
    Video Tutorial


    Text Tutorial ( copied from the more detailed article on my website )

    This extension is comprised of 3 scripts.

    Quick note, this system does not require any special instructions for initiation. When any of the scripts are called the system is checked and initiated automatically.
    mcs_add_controllers(rooms,controllers);

    This script is the meat and potatoes of the whole system. Using it you define a room, or a list of rooms, and a controller object, or a list of controller objects, and that's it! When the room is launched those controllers get created automatically.

    As an example:
    mcs_add_controllers(rm_jungle,con_rain);

    The above code would add the "con_rain" object to the room "rm_jungle". It doesn't end there though. You can specify multiple rooms and/or objects and take care of a lot of your controller distribution in one call.
    mcs_add_controllers([rm_jungle, rm_beach], [con_rain, con_enemy_manager]);

    The above code will add the controllers "con_rain" and "con_enemy_manager" to the rooms "rm_jungle" and "rm_beach". Basically what's going on is your passing arrays in as the arguments.

    mcs_auto_add( "Slug" );

    This is a beautiful script that does all the heavy lifting for you. Let's say you have a fantastic naming convention in your game, for instance every controller that is associated with the jungle levels has "jungle" in the name.

    You can add this code at some point in your game:
    mcs_auto_add("jungle");

    Then, all objects that have "jungle" in their name ( such as con_jungle_vines or con_jungle_weather ) will automatically be added to any rooms that have "jungle" in their name ( for instance "rm_jungle_start" or "rm_jungle_main" ).

    This has the potential to eliminate the need of writing a lot of unnecessary create controller calls.

    mcs_remove_controller( room, controller );

    This script is far less exciting, it simply removes any single controller from a given room, and even destroys it if it already exists.

    That about does it, let me know what you think!
     
    NeZvers likes this.

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