Kydoimos 916 Posted February 16, 2014 Hi all - bit of an open question this, but: any pointers on writing professional looking scripts? Also, does anybody have any tips to run scripts more efficiently, and is there a way to preprocess scripts so that when something like an .sqf runs, our computer doesn't have to think too hard about it? Cheers for any thoughts or help on this! Share this post Link to post Share on other sites
bangabob 45 Posted February 17, 2014 For preprocessing scripts watch this video. As for scripting efficiency check out the following link https://community.bistudio.com/wiki/Code_Optimisation#10.2C000_Iterations_Limit_in_Loo ps Professional looking scripts IMO its all about writing code that makes sense to YOU. You can replace variable names that look 'pro' at the cost of understanding the code at first glance. For instance while {_a == _g} do {}; is worse than while {_unitsKilled == _unitsSpawned} do {}; Share this post Link to post Share on other sites
Zenophon 110 Posted February 17, 2014 Optimization is entirely different from programming style. You can write fast code that is impossible for anyone else (or even yourself later) to read. Ideally you would write something that can be maintained and debugged easily before worrying about how many milliseconds it takes to run. If you are just writing scripts or programs (this applies to any programming language or project) for yourself, then have a systematic style that you can understand later. The goal to is waste as little time as possible figuring out what you meant to do and more time improving and implementing new features. Encapsulate useful code into functions that can be used later. It is extremely beneficial to develop your own personal library of functions that can be applied very quickly to solve a problem. In general, there are a few things you can do to make your code more readable. Use blank lines to separate related blocks of code. Use informative variable names to indicate what they are meant to store. Only use comments for things that are not obvious. Regarding optimization, you might be interested in this: https://community.bistudio.com/wiki/Code_Optimisation Regarding the preprocessor, all sqf scripts must be preproccessed and compiled by the engine before they are executed. They are compiled into a special data type called 'code'. There are three ways to compile scripts to code: SomeFunction = { // code }; Placing this in a file will preprocess and compile the code, storing in the variable, when the engine executes the statement (statement meaning the = operator). You can now use this variable to refer to that code later. For example: 0 = [] call SomeFunction; has the same effect as: 0 = [] call <copy and paste code here>; However, if you place this call statement in a file that is preprocessed multiple times, all the copied and pasted code will be preprocessed and compiled again. Although defining the variable only preproccesses and compiles the code once, the variable can still be overwritten later. This makes the variable constant: SomeFunction = compileFinal preproccessFileLineNumber <path to file>; However, it requires that you place the code in a separate file rather than inside another file, which could get messy if you have many small functions. Thus, this compile method is only preferable for larger scripts. All of this is in contrast to the execVM command. This command preproccesses and compiles the code in the given file every time it is called. It would be inefficient to put this statement in a loop: 0 = [] execVM "function.sqf"; In summary, I recommend that you do not use execVM, but one of the other methods. Finally, all the optimization in the world doesn't help you if you are running 100 threads. Every use of 'spawn' or 'execVM' open a new thread for the game engine. Unfortunately, the game engine executes sqf code using a scheduler that only runs on one thread. Thus, true mutlithreading in sqf is not possible. The scheduler assigns CPU time to each sqf thread, cycling between them so that they appear to run in parallel. Any function that loops or waits should do so every few seconds (include a 'sleep 3;' or similar), so as not to burden the scheduler. Also, one thread that loops over many objects is preferable to many threads that act on one object. You should also use 'call' as much as possible, as it does not start a new thread. Instead, it pauses executing the caller thread and starts executing in a lower scope. Share this post Link to post Share on other sites
Kydoimos 916 Posted February 17, 2014 Wow, thanks guys! Very informative! And lot's for me to learn! Share this post Link to post Share on other sites
IndeedPete 1038 Posted February 18, 2014 Once you start developing your own funtions library, consider the use of CfgFunctions. It's quite easy to set up and you won't spam your init.sqf with lots of "compile preProsessFileLineNumbers...". My own lib (still WIP) hosts around 30 different functions (plus sub-functions / helpers) now and all I need to do when I make up a new project is copying my fnc-folder and place an #include into my description.ext. Share this post Link to post Share on other sites
Kydoimos 916 Posted February 18, 2014 That's a great idea! Yeah, I need to start thinking about something like that! Share this post Link to post Share on other sites