[Bug] CreaturePackRenderer - multiple initialization calls

Discuss issues pertaining to the various game/web runtimes of Creature here.
Post Reply
pg_interactive
Posts: 25
Joined: Thu Feb 22, 2018 2:42 pm

[Bug] CreaturePackRenderer - multiple initialization calls

Post by pg_interactive » Wed Jan 29, 2020 5:46 pm

Hi

By coincidence, I've stumbled into your code and specifically in:
CreaturePackRenderer
You call your
InitRenderer()
3 times, from:
Awake()
Start() // calls Awake
OnEnable()

Id suggest you only initialize once (if that's what you are doing), cache all your things - in Awake.

NOTE: check MonoBehaviour reference, to understand what each function does, but in short:
Awake runs before start in Script Execution order. Do your caching/init here.
Start runs after Awake, and before any frame methods like Update. Further process your cached stuff here.
OnEnable runs whenever object becomes enabled and active, meaning if you toggle enable->disable->enable, your code will run twice, and thus initialization code is not recommended to be placed here.

PS: Id advise you to use 'protected virtual' for all methods that users might need to override, without need to rewrite your plugin code. For example, i could 'temporarily fix' the above issue on my side by creating
CustomCreaturePackRenderer : CreaturePackRenderer
and overriding
Awake
Start
OnEnable

If only those were marked
protected virtual
without touching your code. This practice I'd suggest you to use everywhere you can, so that users can 'extend' your classes by inheriting and overriding, rather than rewriting them, and then having to go through headaches during next plugin update (having to rewrite again, and again..).
Btw this also includes your InitRenderer - its better to mark it protected virtual (as well as other functions you'd usually mark private). As it stands currently, having InitRenderer marked as public has only disadvantages, since you're not calling it from outside, and you're introducing some security risks.

That's all for now
Cheers!

chong
Posts: 1178
Joined: Thu Feb 19, 2015 2:21 am

Re: [Bug] CreaturePackRenderer - multiple initialization calls

Post by chong » Wed Jan 29, 2020 10:44 pm

Thanks for the very insightful tips!
Will start by looking at marking functions as protected virtual.

Cheers

Post Reply