1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Run another PB with argument in script

Discussion in 'Programming (In-game)' started by Hemp Plan[e]t, Dec 1, 2017.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Hemp Plan[e]t

    Hemp Plan[e]t Apprentice Engineer

    Messages:
    157
    Hello. How can i run another PB from my script with a custom argument?
     
  2. Sinbad

    Sinbad Senior Engineer

    Messages:
    2,788
    Code:
    public interface IMyProgrammableBlock : IMyFunctionalBlock, IMyTerminalBlock, IMyCubeBlock, IMyEntity
    	{
    		//
    		// Summary:
    		//	 This programmable block is currently running its program.
    		bool IsRunning { get; }
    		//
    		// Summary:
    		//	 Contains the value of the default terminal argument.
    		string TerminalRunArgument { get; }
    
    		//
    		// Summary:
    		//	 Attempts to run this programmable block using the given argument. An already
    		//	 running programmable block cannot be run again. This is equivalent to running
    		//	 block.ApplyAction("Run", argumentsList); This should be called from an ingame
    		//	 script. Do not use in mods.
    		//
    		// Parameters:
    		//  argument:
    		//
    		// Returns:
    		//	 true if the action was applied, false otherwise
    		bool TryRun(string argument);
    from the looks of this, block.ApplyAction("Run", arguments) is one way to do it, although this may have been depricated (without the defenition summary being updated to reflect this) with the latest api update (which was supposed to remove Actions IIRC).
    the other way i can see is the listed way here:
    block.TryRun(string argument); which will not only run the PB instanced as 'block' but will also evaluate as true if the target block was succesfully run (a pb cant run more than once per tick, this will give you feedback to let you know it if it was run or not).
    please note, ive not tried it, i just peeked the definition of the interface in visual studio and pasted it here.
     
    • Agree Agree x 1
  3. sammyvoncheese

    sammyvoncheese Apprentice Engineer

    Messages:
    151
    here is what I have been using. Pass the list of programmable blocks you want to execute on, an argument to run, and a "Tag" which is something unique in a block name like "Prog Block 1"

    void ProgrammableBlock(List <IMyTerminalBlock> progblock, string argument, String tag) {

    for(int i = 0; i< progblock.Count; i++) {
    if(progblock.CustomName.Contains(tag)) {
    IMyProgrammableBlock myPB = (progblock as IMyProgrammableBlock);
    //Echo("Calling tryrun on " + tag + " with arg "+ argument);
    setRightBottomText("\nCalling ProgrammableBlock tryrun " + tag, true);
    myPB.TryRun(argument);
    }

    }
    }
     
  4. Malware

    Malware Master Engineer

    Messages:
    9,867
    @sammyvoncheese Avoid fetching blocks during normal runtime. It takes time to do so. Prefetch and cache your blocks.
     
    • Informative Informative x 1
  5. sammyvoncheese

    sammyvoncheese Apprentice Engineer

    Messages:
    151
    Yes understand, the block list is fetched prior to this call.
     
    • Like Like x 1
Thread Status:
This last post in this thread was made more than 31 days old.