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.

A few general knowledge questions.

Discussion in 'Programming Questions and Suggestions' started by Swanicus, Apr 3, 2015.

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

    ok, so I'm fairly new to scripting in general (I know next to nothing in all honesty). And I've never even tried scripting in SE.
    Figured I'd ask so questions to understand how the PB works before trying anything. So here goes.

    1) How many scripts can be run on a single PB? Or, what is the string limit/line limit of each PB?
    2) Is it better to run multiple scripts on one PB? Or does it make more sense to have a PB for each script I intend to run?

    I know their both pretty similar questions. But I feel like I need to know this stuff before I attempt to use the PB.

    Thanks in advance guys.
  2. plaYer2k Master Engineer

    There are mainly two important limits for your code in PBs.

    The first is the amount of characters you can use max. It is iirc 100k. You can not exceed that so dont write too big things in one or you need to split them.
    This has i think two reasons. The first is to set a low limit to your code complexity and intensity (see point two) but also to reduce the dll size and thus not bloat it artificially as it might be an issue for the server that has to handle multiple PBs aswell as a single PBs editor which is not optimized yet.

    The second important and less easy to determine limit is the instruction counter.
    Each instruction got a certain value assigned. So the more you use them, the more that counter increases.
    Have for example a simple function with a loop in it. If the loop goes from 1 to 10, the instructions used for it will be roughly a+10c, if the loop however goes from 1 to 100 it will be a+100c with a,b=0,1,2,..
    That is to ensure that you dont consume too many instructions for your code and thus slow the whole server/pc down as you could have a loop going from 1 to infinite.

    So for your questions.

    You can run only one script per PB. Though a script can be extended to have different functonalities if you write them "well enough".

    I guess that answer is mostly a matter of coding standards aswell as how much room you got to place PBs in your ship.
    Having multiple PBs has the advantage that their functions are nicely separated. If one of them throws an exception, the others can continue. If you would run everything on one then nothing would run anymore. So separating them has stability advantages.
    On the other hand there might be several informations aswell as functions multiple scripts would need. When you separate them into different PBs, you have to allocate the memory for them multiple times aswell as consume processor cycles multipl times for the same task, so having good working code in just one PB is more efficient unless you exceed the limits or throw exceptions.
  3. indigodarkwolf Apprentice Engineer

    From my experimentation, the key to understanding KeenInstruction limits is to have a primitive understanding of how code translates into assembly (or, in .NET's case, the Common Intermediate Language).

    The first rule of KeenInstructions is that entering any function within your PB counts as 1 KeenInstruction. This includes void Main().

    The second rule of KeenInstructions seems to be that any branch-compare instruction counts as 1 KeenInstruction. This is the one that's harder to predict, because branch-compares sometimes show up in places you might not immediately expect. I intend to do a somewhat big post using a bunch of test cases I was toying around with, but I haven't been able to find the time just yet. Maybe this weekend. An incomplete summary might be, "N+1 for every loop of N cycles", "1-3 for every if/else", and "1 for every extra logical comparison in any line of code".

    The third rule of KeenInstructions is that everything else is free. Declaring a System.Generics.List and adding 100 items to it? Completely free. Calling the Find() function on it is also free. This appears to be true for all of the Sandbox.ModAPI.Ingame functions that I've tried, as well.

    Of course, "free" is a relative term, and just because the operation costs no KeenInstructions doesn't mean it's instantaneous. Various operations still take appreciable time to run, especially if you're doing a lot of them, so you still have to be aware of what kinds of things can be real-world expensive, or you can cause hitching in your game when your scripts run. PBs don't care about real-world time. They only care about KeenInstructions.

    Edit: Oh, right, almost forgot. There seems to be some kind of 4th rule involving whether or not unconditional jumps count. But if you're using "goto", you're creating abominations fit only to be burned at the stake, anyways. :p
    Last edited: Apr 3, 2015
Thread Status:
This last post in this thread was made more than 31 days old.