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.

Run the programming block when a condition is met

Discussion in 'Programming (In-game)' started by Omricon, Mar 10, 2019.

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

    Messages:
    3
    Hello,
    I was wondering if there is a way to initiate the programming block if a rotor was at a specific rotation, or do I simply have to time the it takes for the rotor to be in that position and then verify the angle?

    Thanks for the help!
     
  2. plaYer2k Master Engineer

    Messages:
    3,160
    I have two setups in mind there:

    Physical
    You have a sensor that triggers upon detecting something and then place a block on the other grid of the rotor, opposed to the grid the sensor is on. That way you could find a position so that the sensor triggers the PB when a certain angle is met.

    Programming
    Use polling. Run the PB every game update and check whether the condition is met or not. If you do not allocate for each run but cache, this is very performant.
     
  3. Malware Master Engineer

    Messages:
    9,861
    Try to avoid Update1 if possible. You rarely need that kind of fidelity, and it does iterate through all the blocks on your grid every time it runs - even for an empty program - so it does have an impact. It's also generally a good habit to get into.
     
  4. plaYer2k Master Engineer

    Messages:
    3,160
    Caching the one reference block for the first time and refetching it every 100th run whenever it becomes invalid is quite feasible tho. He doesnt have to iterate through all blocks for every execution.

    Thus it becomes an execution of
    Code:
    if (cachedRotor.CurrentAngle < desiredAngle)
      return; // terminate if not above desired angle
    // continue normal execution
    
     
  5. Malware Master Engineer

    Messages:
    9,861
    @plaYer2k Like I said. This happens even for an empty program. It's a block ownership status update. It's silly, but it's there.
     
  6. Omricon Trainee Engineer

    Messages:
    3
    @Malware So using a timer block to call the programming block and calculating the time it takes the rotor to go from one edge to the other would be the least impact heavy?
     
  7. Malware Master Engineer

    Messages:
    9,861
    All you need to do is not update every tick unless absolutely required and you're fine. The difference isn't that big, unless you have a really big ship.
     
    • Agree Agree x 1
  8. Omricon Trainee Engineer

    Messages:
    3
    @Malware gotcha, I'm just new to C# and Space Engineers in general. I'm having a lot of fun programming different things for me and my friends while we play. I always try to optimise everything as good as possible. The use of this is for a drill that digs for us, we currently have 2 of them, but with the intention of having many, many more. Therefore I want the code to tax as little as possible. The challenge is also welcome!

    Anyways, thank you for your help and swift replies!
     
  9. Ronin1973 Master Engineer

    Messages:
    4,955

    I believe you can set conditions for a programmable block to self-run without a timer. The frequency can be determined and altered in the programmable block itself. So you can poll for changes infrequently. When the conditions are met, you can increase the frequency until the conditions are satisfied then revert to infrequent polling.

    However, you can perform this trick with two timer blocks. Instead of looping the timer blocks themselves, you can create the loop in the code and have the blocks only run once. One block is set to run immediately and the other to "start" and use the internal timer.
     
Thread Status:
This last post in this thread was made more than 31 days old.