This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.
Every once in a while people ask, how they can randomly select an animation.
This very short tutorial shows a very simple way to do this.
There are a few basic techniques required. You should understand how the random instruction works, as well as the tokenat.
Here a very short summary:
random(x) generates a random selected number between zero and lesser than x. So if you use random(3) you will get a number between greater or equal to zero and less three (0=< X < 3) ie. three is never reached!!!
A valid result could be 0.3142 - if you hand that to an instruction, that requires an integer you will get an error. So what you should do is embed the random within the instruction int()
So int(random(3)) will give either 0, 1 or 2 as a result.
tokenat(s,i,d) parses a string. A string is basicly just a variable holding text. For tokenat to work, you have to use a delimiter, that tokenat can hunt for.
tokenat expects as first parameter the string, that should be parsed. The second parameter tells tokenat which sub-string (ie token) we would like to receive. And the third is the delimiter we use in the string.
For our example this could look like
tokenat("one:two:three", 2, ":")
which would return three!
tokencount(s,d) will help us, to make the command more generic. tokencount returns the number of tokens found in the string.
will return 3.
First we generate a global variable holding the names of our animations to select from in form of a "tokenized" list like this:
Global Text anims = "one:two:three"
Now we only have to give our animations the appropriate names and then we can use
set Animation (tokenat(anims, int( random( tokencount(anims, ":"))), ":"))
to play a "random" animation.
That's basicly it. Let me know, if you need any more information.
Hello, what should I do if I want animations to come in order, not randomly?
try this: Sprite on animation 1 finished, Sprite play animation 2