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.

Real slowdown when running the script

Discussion in 'Programming (In-game)' started by legaCypowers, Mar 2, 2019.

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

    Messages:
    10
    Hello awesome people, i'm currently trying to create a RGB light animation(by assigning random values to Color), but i'm experiencing a TERRIBLE performance issue, that when i enter the scene with the changing color light, my script is pretty simple, can anyone more experienced help me identify what i'm doing wrong that cause this severe performance degradation?
    Code:
    public Program()
    
    {
    
    	// The constructor, called only once every session and
    	// always before any other method is called. Use it to
    	// initialize your script.
    	//	
    	// The constructor is optional and can be removed if not
    	// needed.
    	//
    	// It's recommended to set RuntimeInfo.UpdateFrequency
    	// here, which will allow your script to run itself without a
    	// timer block.
    
    	Runtime.UpdateFrequency = UpdateFrequency.Update100;
    }
    
    
    
    public void Save()
    
    {
    
    	// Called when the program needs to save its state. Use
    	// this method to save your state to the Storage field
    	// or some other means.
    	//
    	// This method is optional and can be removed if not
    	// needed.
    
    }
    
    
    List<IMyTerminalBlock> lights = new List<IMyTerminalBlock>();
    
    string LightName = "Light - Living Room Mood";
    public void Main(string argument, UpdateType updateSource)
    
    {
    
    	Random rnd = new Random();
    	GridTerminalSystem.SearchBlocksOfName(LightName, lights, light => light is IMyLightingBlock); //Gets lights containing the name
    	foreach(IMyLightingBlock light in lights){
    		if(light.IsWorking){ //Check if the light is turned on
    			Color newColor = new Color(rnd.Next(0,255),rnd.Next(0,255),rnd.Next(0,255)); //Creating new color accordingly to the set RGB components
    			light.Color = newColor; //Apply new color
    		} // end of if code
    	}//end of foreach code
    }
    //end of main code
    Thank you in advance.
     
  2. Sero93 Trainee Engineer

    Messages:
    47
    „GridTerminalSystem.SearchBlocksOfName“ is inside your main Programm loop. This means it will run each time your program runs. Now, searchblocksof name goes through ALL your blocks on that grid and compares strings.
    String operations, like comparing are costly especially if you do it on huge ships each script loop. Cache your lights ONCE and reuse the reference.
    Like, search your lights outside the main Programm loop in the „program()“ function and save them to your light list and in game loop only step through that list.

    If you want to find all newly placed lights also, without recompiling script, you have to make the searching only every x seconds to Minima performance impact.

    => gridterminalsystem.search... run every script loop == bad performance
     
    • Like Like x 1
  3. legaCypowers Trainee Engineer

    Messages:
    10
    Ohh i see, yeah i didn't think about that, sometimes those tiny logical mistakes are hard to spot.
    Thank you so much!
     
  4. Sero93 Trainee Engineer

    Messages:
    47
    Well I wouldn‘t say it’s a “tiny“ mistake. If you plan on using your script in any multiplayer world you should have performance as top priority. There are many things to consider and much to read up what actually performs better for which reasons, but caching is the easiest, most fundamental one. If you expand your script keep in mind what actually has to be done every tick and try to keep it at a minimum. I advice using the keen discord, those guys helped me from no idea to at least some basic nowledge about performance.
    They are very nice and you can discuss questions/concepts there quite extensive, if you ask nicely.
     
  5. legaCypowers Trainee Engineer

    Messages:
    10
    It is tiny, i got the the wrong line of thought when i wrote the script, i expanded, tested on multiplayer(well with me + 2 friends), i posted the script on the workshop, seems to work nicely, even with other light "animations", but again thank you for the hint about keen discord servers :)
     
Thread Status:
This last post in this thread was made more than 31 days old.