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.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Ifnot Trainee Engineer

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


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

    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 {
    	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){
    			if(scriptRun < scriptRunTrigger * 2){
    			scriptRun = 0;
    		void Main(){
    		void CleanUpGrids() {
    			foreach(var entity in entityList){
    				var cubeGrid = entity as IMyCubeGrid;
    				var removeGrid = true;
    				if(cubeGrid == null || MyAPIGateway.Entities.Exist(entity) == false){
    				// 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>();
    				for (int i = 0; i < blockList.Count; i++) {
    					if (blockList[i].CustomName == "[G] Grid Holder") {
    						removeGrid = false;
    				if (removeGrid) {
    					List<IMyEntity> entityList = new List<IMyEntity>();
    					for (int i = 0; i < entityList.Count; i++) {
    						MyParticleEffect effect = null;
    						if(MyParticlesManager.TryCreateParticleEffect(22, out effect, false))
    							effect.WorldMatrix = entityList[i].WorldMatrix;
    		public void Log(string message) {
    			MyLog.Default.WriteLineAndConsole("NPC Performance: " + message);
    			MyVisualScriptLogicProvider.ShowNotificationToAll(message, 5000, "White");
    Last edited: Jan 10, 2019
  2. mojomann71 Senior Engineer

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

    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

    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
  5. Ifnot Trainee Engineer

    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 ?
Thread Status:
This last post in this thread was made more than 31 days old.