Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Removing each grid without specific block name (help !)

Discussion in 'Modding' started by Ifnot, Jan 8, 2019.

  1. Ifnot Trainee Engineer

    Messages:
    3
    I am working on a PvP map, and i want to automatically "purge" destroyed ships and grid parts.

    [UPDATED]

    The following script delete each 2 seconds all the grids without a specific block named "[G] Grid Holder" with explosion before grid deletino.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Sandbox.Common;
    using Sandbox.Common.ObjectBuilders;
    using Sandbox.Definitions;
    using Sandbox.Game;
    using Sandbox.Game.Entities;
    using Sandbox.Game.EntityComponents;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.ModAPI.Interfaces;
    using SpaceEngineers.Game.ModAPI;
    using VRage.Game;
    using VRage.Game.Components;
    using VRage.Game.Entity;
    using VRage.Game.ModAPI;
    using VRage.ModAPI;
    using VRage.ObjectBuilders;
    using VRage.Utils;
    using VRageMath;
    
    using IMyTerminalBlock = Sandbox.ModAPI.Ingame.IMyTerminalBlock;
    using IMyGridTerminalSystem = Sandbox.ModAPI.Ingame.IMyGridTerminalSystem;
    
    namespace PurgeGridsWithoutHolder {
    	
    	[MySessionComponentDescriptor(MyUpdateOrder.BeforeSimulation)]
    	
    	public class PurgeGridsWithoutHolder : MySessionComponentBase{
    		HashSet<IMyEntity> entityList = new HashSet<IMyEntity>();
    		
    		int scriptRun = 0;
    		int scriptRunTrigger = 60;
    		List<MyParticleEffect> effects = new List<MyParticleEffect>();
    
    		public override void UpdateBeforeSimulation(){
    			if(MyAPIGateway.Multiplayer.IsServer == false){
    				return;
    			}
    
    			if(scriptRun < scriptRunTrigger * 2){
    				
    				scriptRun++;
    				return;
    				
    			}
    			
    			scriptRun = 0;
    			
    			Main();
    		}
    		
    		void Main(){
    			CleanUpGrids();
    		}
    		
    		void CleanUpGrids() {
    			entityList.Clear();
    			MyAPIGateway.Entities.GetEntities(entityList);
    			
    			foreach(var entity in entityList){
    				var cubeGrid = entity as IMyCubeGrid;
    				var removeGrid = true;
    				
    				if(cubeGrid == null || MyAPIGateway.Entities.Exist(entity) == false){
    					continue;
    				}
    
    				// Open and prepare the grid
    				IMyGridTerminalSystem GridTerminalSystem = MyAPIGateway.TerminalActionsHelper.GetTerminalSystemForGrid(cubeGrid);
    
    				// Search if there is a block named [G] Grid Holder inside it
    				List<IMyTerminalBlock> blockList = new List<IMyTerminalBlock>();
    				GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blockList);
    				for (int i = 0; i < blockList.Count; i++) {
    					if (blockList[i].CustomName == "[G] Grid Holder") {
    						removeGrid = false;
    						break;
    					}
    				}
    				
    				if (removeGrid) {
    					List<IMyEntity> entityList = new List<IMyEntity>();
    					GridTerminalSystem.GetBlocksOfType<IMyEntity>(entityList);
    					for (int i = 0; i < entityList.Count; i++) {
    						MyParticleEffect effect = null;
    						if(MyParticlesManager.TryCreateParticleEffect(22, out effect, false))
    							effect.WorldMatrix = entityList[i].WorldMatrix;
    					}
    
    					cubeGrid.Delete();
    				}
    			}
    		}
    
    		public void Log(string message) {
    			MyLog.Default.WriteLineAndConsole("NPC Performance: " + message);
    			MyVisualScriptLogicProvider.ShowNotificationToAll(message, 5000, "White");
    		}
    	}
    }
    
     
    Last edited: Jan 10, 2019 at 22:32
  2. mojomann71 Junior Engineer

    Messages:
    728
    You can do this with the admin tool if you are the host of the server.
     
  3. Ifnot Trainee Engineer

    Messages:
    3
    Thanks for your answer but this is not what i am searching.

    I already do a manual cleanup on my dedicated servers, but in this case, i need something automatic with a mod.

    I updated my main message for showing my code with some "TODOs", any help ? :3

    I am really doing my best learning c# and digging the API :/

    I may look for the source of : https://steamcommunity.com/sharedfiles/filedetails/?id=1167708101
     
  4. Digi Senior Engineer

    Messages:
    2,337
    grid.Close() to remove it (preferably call it only server side)

    Why do you want to remove the particle manually, why not have it do its thing and finish on its own, a single playback? That would be the simplest way. Also make it a local var to simplify spawning multiple particles for possible multiple grids.
     
    Last edited: Jan 10, 2019 at 21:12
  5. Ifnot Trainee Engineer

    Messages:
    3
    Hey everyone, i managed to do the job !!! :D I shared my complete code on my first post :)

    @Digi thanks for you reply ! So I just did a local variable and use it each time i spawn an effect.
    But, do you think never deleting the effect variable will bring the garbage collector to delete itself the old effect ? (i dont want they stack in the memory each time ...)

    I have just one tweak to do : for the moment i only spawn particle on found IMyEntity on the deleted grid, it looks that simple block like armor are not listed, just functionnal blocks. How can I list also armor blocks ?