Dev Trick #004 - Dynamization (Patreon)
Content
Hello there!
Today, in this new Dev Tricks, we're going to talk about something a bit more technical, but also something that you'll all need more or less if you decide to write a computer application, whether it's an AVN, a classic program or anything else.
We are going to talk about dynamization!
What the hell is that, you will say. (Some of you at least :) )
It is the fact of making a computer program execute an instruction several times, with identical or different values, in order to avoid having to type these instructions several times. It also allows to manipulate an uncertain number of data.
For example, instead of telling the program "display 1, then display 2, then display 3, then display 4", you can tell it "for each number from 1 to 4, display this number".
Obviously, said like that, it's not magical, but in fact...
Let's take a concrete example!
You want to code a gallery of images in your game (for example :D ).
You can code it "statically" : "show me image 1, then image 2, then image 3 etc...".
You can also code this dynamically: "Here is the list of all the images. For each, you display."
If you have two images, this is not very interesting, if you have 100 on the other hand...
In ren'py/python for example, how does this translate?
Simply, by using :
- a data structure called an array, which will contain all the images
chap0 = ["ch0_prf_0005", "ch0_prf_0012", "ch0_coul_0007", "ch0_cafaur_0012", "ch0_annonce_0005"]
- a piece of code that will browse this array and display the image for each entry in the array
for img in chap0:
imagebutton idle img
And that's all for this, simple, isn't it?!
Of course, your array can contain something else than just strings, like dictionaries or objects, or other arrays.
So you can store your images by chapter in an array, and put each chapter's array in an array, for example.
And the more you get used to using this dynamization, the more your code will become simple, maintainable, scalable (well, if you need to change something 200 times because you wrote the display text of an image 200 times, you are not going to change your code often) and short.
However, I invite you to use as much as possible the comments to explain what each step of your dynamization does. Indeed, it can become more complex to read than just repetitive code.
That's it for today, but if you have any ideas, or comments, go ahead!
See you another time!