Advertisement
devonhg

Master Control for Game Maker Studio 2

Feb 25th, 2019
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ======================================================================================================
  2.                                           Add Controllers Script
  3. ======================================================================================================
  4.  
  5. ///mcs_add_controllers( Rooms, Controllers );
  6. ///@param Rooms - The room(s), single or array, that are getting controllers added to
  7. ///@param Controllers - The Controllers to add to the room(s), single or array
  8. #region //Details and Use
  9. /*
  10.     Add room controllers to the game. You can specify a room and the controllers
  11.     that go into it.
  12.    
  13.     Room ( or an array of rooms ) : What room(s) to put controller(s) into.
  14.    
  15.     Controller ( or an array of controllers ) : What controller(s) to put into the room(s).
  16.    
  17.     Event : Game Config Script
  18.     Object Type : None  
  19.     Scope : Game
  20. */
  21. #endregion
  22.  
  23. #region //Check if the system has been initiated, if not then initiate it.
  24. if( !instance_exists( mc_master ) ){
  25.     instance_create_depth( 0,0, 0,mc_master );
  26. }
  27. #endregion
  28.  
  29. var rm = argument[0], i, j, k, l, obj = argument[1];
  30.  
  31. //If argument count is less than 1 you've dun fucked up
  32. if argument_count > 1{
  33.    
  34.     #region //Main Processing
  35.         //First, we check the rooms and see if they are indexed
  36.         if( is_array( rm ) ){//We have an array of rooms
  37.            
  38.             //Loop through the entries, find our rooms, add if neccessary
  39.             for( j=0; j < array_length_1d(rm); j++ ){
  40.                 for( i = 0; i <= array_height_2d(global.game_nor_con); i++ ){          
  41.                     if( i == array_height_2d(global.game_nor_con) || global.game_nor_con[i,0] == -1 ){//Add it
  42.                         global.game_nor_con[i,0] = rm[j];
  43.                         break;
  44.                     }
  45.                     if( global.game_nor_con[i,0] == rm[j] ){
  46.                         break;//It's already listed, so stop
  47.                     }
  48.                 }
  49.             }
  50.            
  51.             #region //Object processing
  52.                 //Now that we've handled our rooms, let's insert objects
  53.                 if( is_array(obj) ){//Array of rooms, Array of objects
  54.                     for( l=0; l < array_length_1d(obj); l++ ){
  55.                         for(k=0; k < array_length_1d(rm); k++ ){//We are going to loop through and perform this on all of htese
  56.                             for( i = 0; i < array_height_2d(global.game_nor_con); i++ ){           
  57.                                 if( global.game_nor_con[i,0] == rm[k] ){//We've found our room
  58.                                     for(j=1; j <= array_length_2d(global.game_nor_con, i); j++){//Loop through our controllers, add if not there
  59.                                         if( j == array_length_2d(global.game_nor_con, i) || global.game_nor_con[i,j] == -1 ){
  60.                                             global.game_nor_con[i,j] = obj[l]; // Put it on the end of our list
  61.                                             break;
  62.                                         }
  63.                                         if( global.game_nor_con[i,j] == obj[l] ){
  64.                                             break;//We're already listed, stop 
  65.                                         }  
  66.                                     }
  67.                                 }
  68.                             }
  69.                         }
  70.                     }
  71.                 }else{//Array of rooms, only one object
  72.                     for(k=0; k < array_length_1d(rm); k++ ){//We are going to loop through and perform this on all of htese
  73.                         for( i = 0; i < array_height_2d(global.game_nor_con); i++ ){           
  74.                             if( global.game_nor_con[i,0] == rm[k] ){//We've found our room
  75.                                 for(j=1; j <= array_length_2d(global.game_nor_con, i); j++){//Loop through our controllers, add if not there
  76.                                     if( j == array_length_2d(global.game_nor_con, i) || global.game_nor_con[i,j] == -1 ){
  77.                                         global.game_nor_con[i,j] = obj; // Put it on the end of our list
  78.                                         break;
  79.                                     }
  80.                                     if( global.game_nor_con[i,j] == obj ){
  81.                                         break;//We're already listed, stop 
  82.                                     }  
  83.                                 }
  84.                             }
  85.                         }
  86.                     }
  87.                 }
  88.             #endregion
  89.        
  90.         }else{//We have just one room
  91.            
  92.             //First, we loop through and check each of the rooms to make sure it isn't already listed
  93.             for( i = 0; i <= array_height_2d(global.game_nor_con); i++ ){          
  94.                 if( i == array_height_2d(global.game_nor_con) || global.game_nor_con[i,0] == -1 ){//Add it
  95.                     global.game_nor_con[i,0] = rm;
  96.                     break;
  97.                 }
  98.                 if( global.game_nor_con[i,0] == rm ){
  99.                     break;//It's already listed, so stop
  100.                 }
  101.             }
  102.            
  103.             #region //Object Processing
  104.                 //Now that we've handled our rooms, let's insert objects
  105.                 if( is_array(obj) ){//One room, Array of objects
  106.                     for( k = 0; k < array_length_1d(obj); k++ ){
  107.                         for( i = 0; i <= array_height_2d(global.game_nor_con); i++ ){          
  108.                             if( global.game_nor_con[i,0] == rm ){//We've found our room
  109.                                 for(j=1; j <= array_length_2d(global.game_nor_con, i); j++){//Loop through our controllers, add if not there
  110.                                     if( j == array_length_2d(global.game_nor_con, i) || global.game_nor_con[i,j] == -1 ){
  111.                                         global.game_nor_con[i,j] = obj[k]; // Put it on the end of our list
  112.                                         break;
  113.                                     }
  114.                                     if( global.game_nor_con[i,j] == obj[k] ){
  115.                                         break;//We'r already listed, stop  
  116.                                     }  
  117.                                 }
  118.                                 break;
  119.                             }
  120.                         }
  121.                     }
  122.                 }else{//One room, only one object
  123.                     for( i = 0; i <= array_height_2d(global.game_nor_con); i++ ){          
  124.                         if( global.game_nor_con[i,0] == rm ){//We've found our room
  125.                             for(j=1; j <= array_length_2d(global.game_nor_con, i); j++){//Loop through our controllers, add if not there
  126.                                 if( j == array_length_2d(global.game_nor_con, i) || global.game_nor_con[i,j] = -1 ){
  127.                                     global.game_nor_con[i,j] = obj; // Put it on the end of our list
  128.                                     break;
  129.                                 }
  130.                                 if( global.game_nor_con[i,j] == obj ){
  131.                                     break;//We'r already listed, stop  
  132.                                 }  
  133.                             }
  134.                             break;
  135.                         }
  136.                     }          
  137.                 }
  138.             #endregion
  139.         }
  140.     #endregion
  141.    
  142.     event_perform(mc_master, ev_room_start);//Initialize objects
  143.    
  144. }else{
  145.     show_error( "msc_add_controllers: Error, this script requires additional arguments", true );
  146. }
  147.  
  148.  
  149. ======================================================================================================
  150.                                             Auto Add Script
  151. ======================================================================================================
  152.  
  153.  
  154. ///mcs_auto_add( "Slug" );
  155. ///@param Slug - A string containing the "word" to link everything together with
  156. /*
  157.     This script quite simply links all controllers and all rooms
  158.     that have the unified "slug" in their names. For instance "Jungle" will
  159.     add all objects that contain the word "Jungle" to all rooms that have
  160.     the word "Jungle" in the name.
  161.    
  162.     Higly recomend using some sort of convention that indicates you wish to
  163.     include a controller.
  164.    
  165.     For instance, instead of just "jungle" do "a_jungle", then name your objects
  166.     con_a_jungle_* and rm_a_jungle_* so you don't accidently include anything you
  167.     don't wish to include.
  168. */
  169. #region//Check if the system has been initiated, if not then initiate it.
  170. if( !instance_exists( mc_master ) ){
  171.     instance_create_depth( 0,0,0,mc_master );
  172. }
  173. #endregion
  174.  
  175. var _s = argument[0], i, _n, _arr_r, _arr_o;
  176.  
  177. _arr_r[0] = -1;
  178. _arr_o[0] = -1;
  179.  
  180. #region//Search for rooms
  181.  
  182. //Check for up to 1000 rooms.
  183. for( i=0; i<1000; i++ ){
  184.     if( room_exists(i) ){//Check this room
  185.         _n = room_get_name(i);
  186.         if( string_pos( _s, _n ) != 0 ){//Check to see if our room has our slug
  187.             if( _arr_r[0] == -1 ) _arr_r[0] = i;
  188.             else _arr_r[array_length_1d( _arr_r )] = i;
  189.         }
  190.     }else{//We ran out of rooms
  191.         break; 
  192.     }
  193. }
  194.  
  195. #endregion
  196.  
  197. #region//Search for controllers
  198.  
  199. //Check for up to 1000 controllers.
  200. for( i=0; i<1000; i++ ){
  201.     if( object_exists(i) ){//Check this controller
  202.         _n = object_get_name(i);
  203.         if( string_pos( _s, _n ) != 0 ){//Check to see if our controller has our slug
  204.             if( _arr_o[0] == -1 ) _arr_o[0] = i;
  205.             else _arr_o[array_length_1d( _arr_o )] = i;
  206.         }
  207.     }else{//We ran out of controllers
  208.         break; 
  209.     }
  210. }
  211.  
  212. #endregion
  213.  
  214. //Add our controllers to our rooms
  215. if( _arr_r[0] != -1 && _arr_o[0] != -1 ){//Make sure we found stuff first
  216.     mcs_add_controllers(_arr_r,_arr_o);
  217. }else{
  218.     show_debug_message("Error MCS: No controllers or rooms found for slug '" + _s + "'"  );
  219. }
  220.  
  221.  
  222. ======================================================================================================
  223.                                     Remove Controllers Script
  224. ======================================================================================================
  225.  
  226.  
  227. ///mcs_remove_controller( room, controller );
  228. ///@param Room - The room we are removing a controller from
  229. ///@param Controller - The controller we are removing
  230. /*
  231.     Using this script you are able to remove a room controller from being
  232.     generated, as well as delete its instance.
  233. */
  234.  
  235. var con = argument[1], rm = argument[0], i, j;
  236.  
  237. for( j=0; j<array_height_2d( global.game_nor_con ); j++ ){
  238.     if( global.game_nor_con[j, 0] == rm ){
  239.         for( i=1; i<array_length_2d( global.game_nor_con, j ); i++ ){
  240.             if( global.game_nor_con[j, i] == con ){
  241.                 if( instance_exists( global.game_nor_con[j, i] ) ){
  242.                     with( global.game_nor_con[j, i] ) instance_destroy();
  243.                 }
  244.                 global.game_nor_con[j, i] = -1;
  245.                 break;
  246.             }
  247.         }
  248.     }
  249. }
  250.  
  251.  
  252. ======================================================================================================
  253.                                     Master Control Object
  254. ======================================================================================================
  255. //MUST BE NAMED "mc_master"!!
  256. //MUST BE PERSISTENT!!
  257. =========================
  258. /////Create Event
  259. =========================
  260.  
  261. /// @description Initiation and Description
  262. #region // Details
  263. /*
  264.     This object is created when the controller engine is initiated. This handles all the
  265.     controller inclusions based on what is in global values.
  266.    
  267.     Upon creation, it initializes the necessary global variables and launches the controller
  268.     creation code.
  269.    
  270.     The structure of the global 2d array is as follows (OLD)
  271.         game_nor_con[#,0] = Room ID
  272.         game_nor_con[#,1..2..3..] = Controller Instance IDs
  273.    
  274. */
  275. #endregion
  276.  
  277. global.game_nor_con[0,0] = -1;
  278. event_perform(mc_master, ev_room_start);//Use this code to manually initiate controllers
  279.  
  280. show_debug_message("MCS|||Master Control System initiated.");
  281.  
  282. =========================
  283. /////Room Start Event
  284. =========================
  285.  
  286. /// @description Initiate Controllers
  287.  
  288. var i,j, ind = array_height_2d(global.game_nor_con);
  289.  
  290. for(i=0; i < ind; i++){
  291.     //Check if our item here is for our room
  292.     if(room == global.game_nor_con[i,0]){
  293.         //If so, if it's a valid object go ahead and create it
  294.         for(j=1; j < array_length_2d(global.game_nor_con, i); j++){
  295.             if( global.game_nor_con[i, j] != -1 and global.game_nor_con[i, j] != -4 ){
  296.                 if( !instance_exists( global.game_nor_con[i, j] ) ){
  297.                     instance_create_depth(0,0, 0, global.game_nor_con[i, j]);
  298.                 }
  299.             }
  300.         }
  301.     }
  302. }
  303.  
  304. show_debug_message("MCS|||Control initiations ran.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement