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.

Question about continuous looping in PB

Discussion in 'Programming (In-game)' started by Matthew Capps, Jun 12, 2020.

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

    Messages:
    2
    so I've just gotten into playing with the PB in game and was wondering how to make a script continuously loop
    basically I'm trying to make it so refineries cant be turned off and if they are, to then instantly turn the back on.
    I'm using a timer block as a check to link multiple refineries. any help would be appreciated

    Code:
    public Program()
    		{
    			Runtime.UpdateFrequency = UpdateFrequency.Update100;
    		}
    public void Main(string argument, UpdateType updateType)
    		{
    			var Checkblock = GridTerminalSystem.GetBlockWithName("Defib") as IMyTimerBlock;
    			var refinery1 = GridTerminalSystem.GetBlockWithName("REF1") as IMyRefinery;
    
     		if ((updateSource & UpdateType.Update100) != 0)
    		   	 {
    
    					if (Checkblock.Enabled == false)
    					{
    						Checkblock.GetActionWithName("OnOff_On").Apply(Checkblock);
    						refinery1.GetActionWithName("OnOff_On").Apply(refinery1);
    			}
    		}
    	}
    
    
     
  2. Malware Master Engineer

    Messages:
    9,861
    <https://github.com/malware-dev/MDK-SE/wiki/Quick-Introduction-to-Space-Engineers-Ingame-Scripts>
    <https://github.com/malware-dev/MDK-SE/wiki/Continuous-Running-No-Timers-Needed>
    <https://github.com/malware-dev/MDK-SE/wiki/Do's-and-Don'ts>

    What is your issue? Your code looks pretty much OK to me.

    1. Please don't continuously fetch your blocks. Do it once in the constructor.
    2. Please don't use the terminal actions where it's not necessary. In this case, simply set the Enabled property.

    Fyi you don't really need a script for this. Your timer block can disable the refineries on its own. Timer blocks are completely superfluous for scripts.

    Code:
    IMyTimerBlock _checkBlock;
    IMyRefinery _refinery1;
    
    public Program()
    {
    	_checkBlock = GridTerminalSystem.GetBlockWithName("Defib") as IMyTimerBlock;
    	_refinery1 = GridTerminalSystem.GetBlockWithName("REF1") as IMyRefinery;
    
    	Runtime.UpdateFrequency = UpdateFrequency.Update100;
    }
    
    public void Main(string argument, UpdateType updateType)
    {
    	if ((updateSource & UpdateType.Update100) != 0)
    	{
    		if (!_checkBlock.Enabled)
    		{
    			_checkBlock.Enabled = true;
    			_refinery1.Enabled = true;
    		}
    	}
    }
    
    Note: The underscore (_) before the class field names are simply a part of a C# naming convention. An alternative is to ditch the underscore and refer to them directly with this. instead: this.checkBlock.Enabled = true

    I prefer the underscore in the PB because it takes the least amount of characters.
     
    Last edited: Jun 12, 2020
  3. Matthew Capps Trainee Engineer

    Messages:
    2
    The real problem i was having is a server I'm on; the production blocks, timers included, are shut off automatically every 30 minutes or so. so this was supposed to be a work around since PBs don't get turned off.
    and it seems like the underscore names dont want to compile ingame as well as some other parts, but this seems to work.
    thank you so much for your help, though!

    Code:
    public Program()
    {
    	Runtime.UpdateFrequency = UpdateFrequency.Update100;
    }
    
    public void Main(string argument, UpdateType updateType)
    {
    var Checkblock = GridTerminalSystem.GetBlockWithName("Defib") as IMyTimerBlock;
    	var refinery1 = GridTerminalSystem.GetBlockWithName("REF1") as IMyRefinery;
    	if ((UpdateType.Update100) != 0)
    	{
    		if (!Checkblock.Enabled)
    		{
    			Checkblock.Enabled = true;
    			refinery1.Enabled = true;
    		}
    	}
    }
     
  4. Malware Master Engineer

    Messages:
    9,861
    Oh trust me, the underscore names will compile. It's a C# standard and I use them throughout all my scripts.

    I might have made a mistake or something, I wrote that right here in the forum as I'm at work.

    Please listen to my advice though. Don't fetch blocks every time.


    Yep, seems like I did make a mistake. I missed a case change ( _checkblock vs _checkBlock)
     
  5. Roxette Senior Engineer

    Messages:
    1,546
    Sometimes I miss the days when most common programming languages were completely insensitive to character case.
     
  6. Ronin1973 Master Engineer

    Messages:
    4,955
    If that's being done, it's a mod setting by an admin. When they find that you're using programmable blocks to circumvent their server rules, your stuff will probably get deleted and you might get banned.
     
    • Agree Agree x 1
Thread Status:
This last post in this thread was made more than 31 days old.