Legotrash Posted December 7, 2014 Share Posted December 7, 2014 (edited) I'm making a mod and I keep wondering if I should take some functions out of the quest script,make them into functions and call them whenever necessary. I'm wondering because,so far,I don't see how could I gain anything from it. Look at these two examples: No1 If something == 1 do somethingelse do something elseendif No2 If something == 1 call aaMyFunctionendif In the first example,if the condition is not met then the rest will be simply skipped,right? If so,I don't need to turn it into a function (like the second example). The only situation where I would think "I should make a function for that." is when I would like something to happen along with what the quest script is doing. I'm troubled about this because I see good modders using functions a lot. Anyway,feel free to let me know your thoughts about this. EDIT: Err, I typed in the title "question" instead of "discussion". Pretend you didn't notice it. :D Edited December 7, 2014 by Legotrash Link to comment Share on other sites More sharing options...
The_Vyper Posted December 8, 2014 Share Posted December 8, 2014 Example 1 is actually quite useful when you want to have two possible results from a single call. Example: If Infamy < 1 NPCRef.ModDisposition Player 100 Else NPCRef.ModDisposition Player -100 NPCRef.StartCombat Player End That's just one example, but there are plenty of other ways that could come in handy. It could also be set up to provide more than two outcomes. The second example is useful if you want to do one specific thing when the right conditions are met. There are plenty of ways that could come in handy as well. You could use it to call a function that would have the same result as example 1, but it's a lot easier to keep track of (and change as needed) if everything is in one script. Link to comment Share on other sites More sharing options...
Legotrash Posted December 8, 2014 Author Share Posted December 8, 2014 I think I kinda misled you with the way I expressed myself. What I wanted to show with the examples was that, after the condition, the first has everything happening in the very same script while the other example calls a second one (the function). I was wondering if there's any benefit in creating functions for things that can be easily included in the initial script. The condition is there in both examples so it's not that there's anything extra in either of them.Sorry for the confusion.In the end you did tell me what I was looking for though so I don't feel that I wasted your time. :D So,I got one opinion so far (thank you very much!) and I kinda agree with it to be honest. In the other hand...creating functions instead would keep things a bit more tidy I think. Still, so far in my mod's script I've kept everything in the quest script. I just don't feel the need to create functions. Link to comment Share on other sites More sharing options...
forli Posted December 8, 2014 Share Posted December 8, 2014 (edited) Generally speaking, functions are not strictly required in programming, but their are very very very useful for many reason:FIRST OF ALL: Subdivide a problem in many sub-problems. Reuse the code: if you need to do the same exactly steps in many places of the scripts What I call "compress" the code: If you have "IF CONDITION: <200 lines>, ELSE: <150 lines>", creating 2 functions which execute (respectively) those 200 and 150 lines will turn the first script into a shorter and readable "IF CONDITION: functionA, ELSE: functionB. Eve better if functionA and functionB are very similar (look at point 2). Performance: In Oblivion the if conditions are very poorly implemented. "If CONDITION: <200 lines>" is much more expensive than "IF CONDITION: functionX" where functionX contains the 200 lines. It's explained here (look at paragraph "Oblivion evaluates entire If statement").If your IF only contains few lines of code there's no reason to create a separate function. Edited December 8, 2014 by forli Link to comment Share on other sites More sharing options...
Legotrash Posted December 9, 2014 Author Share Posted December 9, 2014 Thank you forli for the comment! I see your point in everything except no.4. If the condition remains the same how's one way better than the other? I read the paragraph you linked (thank you by the way!) but I really don't see its relation to this.Still,you made valid points already so I think I'm gonna turn the big parts of my script into functions. The functions take place while the main script keeps going,right? I just want to make sure because I might have to make the script do some things in the proper order. Having two scripts working at the same time might complicate things. We'll see how things will unfold. :D I'm still interested to opinions so if anyone wants to comment on this feel free to do so.In case anyone's interested to know,I'm working in a weather mod where the player will be affected by it,like Duke Patrick's Hypothermia mod. It was good but it needed polishing and Duke abandoned it so I decided to make my own. Anyway,the concept is the same but I'm doing things my way and I might add a couple of new features (we'll see). Link to comment Share on other sites More sharing options...
forli Posted December 9, 2014 Share Posted December 9, 2014 About 4):When Oblivion reach an IF, it do this:a) Examines (doesn't execute) the whole If block (and ElseIf / Else blocks, if any) to find the EndIf (the bigger the block, the more time and CPU it needs).b) Execute the condition (the whole condition). && and || are not "shortcircuit", so doing "If refVar && refVar.something" will result in CTD if "refVar" is null.c) Execute the first block with a true condition (or the Else block (if any), if all previous If / ElseIf conditions are false) Because of a), if your If / ElseIf / Else blocks contains many lines it will waste your CPU. It would examine all of them asking "Are you EndIf?".Instead, a single function (containing all those lines) is much faster, since it examines the function, realize it's not an "EndIf" and go on (and won't check all lines inside it!). It will quickly reach the "EndIf", then it will proceed with b) and c). For little If blocks you can completely ignore them, but for If with 20+ lines I would use a function for them. Link to comment Share on other sites More sharing options...
Legotrash Posted December 9, 2014 Author Share Posted December 9, 2014 Oh,my bad! Got it,thank you very much! (I did read the explanation in the link a bit hasty :blush: .sorry!)Really helpful forli,I appreciate it! Link to comment Share on other sites More sharing options...
Recommended Posts