mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-08-26 22:50:26 +01:00
Adds the use of delta-time to the contribution guidelines (#54911)
Delta-time is a concept we added but isn't used a lot yet, I think adding it to the contribution guidelines with an explanation of how to actually use it (As its a concept that only really applies to game dev afaik, and isn't something everyone is familiar with) will increase the amount of people using it.
This commit is contained in:
@@ -99,6 +99,39 @@ The previous code made compliant:
|
||||
code
|
||||
```
|
||||
|
||||
### All process procs need to make use of delta-time and be frame independent
|
||||
|
||||
In a lot of our older code, process() is frame dependent. As an example, here's some example mob code.
|
||||
|
||||
```DM
|
||||
/mob/testmob
|
||||
var/health = 100
|
||||
var/health_loss = 4 //We want to lose 2 health per second, so 4 per SSmobs process
|
||||
|
||||
/mob/testmob/process(delta_time) //SSmobs runs once every 2 seconds
|
||||
health -= health_loss
|
||||
|
||||
```
|
||||
|
||||
As the mobs subsystem runs once every 2 seconds, the mob now loses 4 health every process, or 2 health per second. This is called frame dependent programming.
|
||||
|
||||
Why is this an issue? If someone decides to make it so the mobs subsystem processes once every second (2 times as fast), your effects in process() will also be two times as fast. Resulting in 4 health loss per second rather than 2.
|
||||
|
||||
How do we solve this? By using delta-time. Delta-time is the amount of seconds you would theoretically have between 2 process() calls. In the case of the mobs subsystem, this would be 2 (As there is 2 seconds between every call in process()). Here is a new example using delta-time
|
||||
|
||||
```DM
|
||||
/mob/testmob
|
||||
var/health = 100
|
||||
var/health_loss = 2 //Health loss every second
|
||||
|
||||
/mob/testmob/process(delta_time) //SSmobs runs once every 2 seconds
|
||||
health -= health_loss * delta_time
|
||||
|
||||
```
|
||||
In the above example, we made our health_loss variable a per second value rather than per process. In the actual process() proc we then make use of deltatime. Because SSmobs runs once every 2 seconds. Delta_time would have a value of 2. This means that by doing health_loss * delta_time, you end up with the correct amount of health_loss per process, but if for some reason the SSmobs subsystem gets changed to be faster or slower in a PR, your health_loss variable will work the same.
|
||||
|
||||
For example, if SSmobs is set to run once every 4 seconds, it would call process once every 4 seconds and multiply your health_loss var by 4 before subtracting it. Ensuring that your code is frame independent.
|
||||
|
||||
### No overriding type safety checks
|
||||
The use of the : operator to override type safety checks is not allowed. You must cast the variable to the proper type.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user