As you may know, Skyrim Platform gives us the chance to tap into two events that get executed every single frame: 'update'
and 'tick'
, and the prospect of the kind of fuck ups modders can do with both of these really gives me shudders.
I'm not someone who likes to do nothing when things are obviously wrong, but sadly the best thing I can do to prevent these kind of situations is to write about good programming practices, since kicking someone in the head is considered wrong in uncivilized cultures like ours.
So here there are some good programming practices that will prevent you from doing shitty scripts.
'update'
and 'tick'
as much as possibleIf there's an event that can do whatever you want, you should use it.
OnEquip.
When checking conditions, try to check first for the cheapest and most likely ones to solve your condition.
This is something you should put special attention into when using or
(||
) or and
(&&
) boolean operators.
If you see the truth table for or
:
OR | true | false |
---|---|---|
true | true | true |
false | true | false |
You will see the only way to get a false
is only when both sides of the operation are false
.
So, to shortcircuit as soon as possible you must put first the condition that is more likely to be true
.
if(IsNaked(player) || player.getName() === 'Vegan4Life') return "Hey! Look at me! LOOK AT ME!!!"
Similar shit happens with and
:
AND | true | false |
---|---|---|
true | true | false |
false | false | false |
In this case you only get true
if everything is true
, so you must put first the most unlikely possibility.
if(player.getName() === 'Arnold' && year === 1984 && IsNaked(player)) return "I'll be back"
if
s as wellIt's quite obvios you can shortcircuit inside an if
, but you should always remember to solve easy things before expensive ones if possible.
I don't want to point fingers, but I saw once an algorithm that managed to break all good practices you'll see in this article[1].
It did something like this:
on update get ammo using expensive operation involving nested loops check if player has a (cross)bow equipped unequip ammo if no (crossbow was equipped)
Can you see all problems here?
First, it is permamently doing a nested loop all the time (more on that soon).
Only after doing that it checks if the player has a (cross)bow equipped!
It's completely preposterous!
Even if that author didn't know both OnEquip and OnUnequip events existed, even if he couldn't find a way to get the ammo not using nested loops, he had no excuse for doing first a quite expensive operation instead of... I don't know... asking first if the player has a fucking (cross)bow equipped to begin with?
on equip check if player has a (cross)bow equipped get ammo using expensive operation involving nested loops unequip ammo if no (crossbow was equipped)
Even something so rudimentary like that would have been a huge performance improvement.
And talking about nested loops
I'm using it as a guide when writing this, as a matter of fact. ↩︎