Welcome to the Creatures Wiki! Log in and join the community.

Cob Tutorial 10 - Critter 2

From Creatures Wiki
Jump to navigation Jump to search

Introduction

Enchanted Butterfly

Using the enchanted butterfly cob from my site, this time we’ll be a bit more creative, and make a critter that doesn’t move in a strictly uniform fashion. Using random variables to control the speed, up/down motion, left/right motion and length of movement we can create a critter that moves much more naturally, with a whole lot less hardcoding.

Installation script

inst
new: simp hsbf 13 0 3500 0
setv cls2 2 18 31502
setv attr 71
bhvr 1 1
tick 1
slim
edit
endm
inst
new: simp hsbf 13 0 3500 0
setv cls2 2 18 31502
setv attr 71

The attr number which is set at 71 rather than 193, which is what we used for our last critter cob - the snake. The attributes of this butterfly are carryable (1) + mouseable (2) + activatable (4) + can collide (64) which equals an attribute number of 71. The reason why this is different from the snakey cob is we did not turn the gravity on for this butterfly, so it is not effected by the gravity, so it will fly and not fall to the ground.

bhvr 1 1

We want both hand and norn to be able to push this object.

tick 1

As soon as the object is injected in the world we want the timer script called (that is the butterfly to start flapping its wings and trying to move.

slim
edit
endm

Timer script (9)

With this script we are aiming to make the butterfly fly around Albia on an endless loop. We don’t want to make it fly at the one speed or direction so we’ve used random variables (remember from the car cob?) to have a varying value for the x velocity and y velocity of the object.

scrp 2 18 31502 9
rndv va00 1 2
rndv va04 2 7
reps va04
rndv va03 10 30
doif va00 eq 1
rndv va01 -5 0
setv velx va01
rndv va02 -5 5
setv vely va02
anim[0123456]
over
base 0
over
else
doif va00 eq 2
rndv va01 0 5
setv velx va01
rndv va02 -5 5
setv vely va02
base 7
anim[0123456]
over
base 0
over
endi
endi
repe
tick 1
endm
scrp 2 18 31502 9

It’s a timer script, so we use number 9. It’s called almost as soon as the object is injected (one tick later in fact, which is almost nothing as a tick is between 1/10 of a second and 1 second depending how fast your system is).

rndv va00 1 2


rndv va04 2 7

We want the script to make the butterfly flap it’s wings and move forward a bit to repeat a few times. So the butterfly will look more natural, we’ll vary it by setting it as a random variable with a value between 2 and 7. This means the animation of the butterfly flying will loop (or will be played) 2, 3, 4, 5, 6 or 7 times.

reps va04

Here’s where we use va04. Reps (as we’ve seen before) stands for repeat script. va04 is the number of times to repeat. If va04 is set to 5 the animation will repeat 5 times.

rndv va03 10 30
doif va00 eq 1

We set va00 as a random value between 1 and 2 above. Now we’re using it (just like in the car cob) to decide whether the butterfly will go left or right. It’s important that we know which way it is flying so we can coordinate the animation sequence.

rndv va01 -5 0

va01 will be the butterfly’s left right movement. Because va00 was 1 the butterfly will be moving left. We don’t want the butterfly to be flying left, but moving right so we set va00 as a negative number (negative velx values move left and positive move right remember?). We want the butterfly to move at various angles (not just 90 degree) so we set the left movement as a random number between -5 and 0.

setv velx va01

Here’s where we actually set the velx value using the variable we just filled with a random number.

rndv va02 -5 5

This will be the up down movement. Because the up down movement does not depend on the the butterfly’s animation sequence, we can set it as going up or down (negative for up, positive for down) in the one go, unlike the velx which depends which way the butterfly is facing (a butterfly looks stupid going backwards, believe me). If the number is between -5 and -1 the butterfly will go up, if it’s 0 the butterfly will just move left or right and won’t move up or down, and if it’s between 1 and 5 the butterfly will move down. We could have set these to any values, but a butterfly goes faster than five tends to look a bit unrealistic.

setv vely va02

Here’s where we actually set the vely (the velocity on the y-axis of Albia - up and down) to be whatever number va02 ended up being.

anim[0123456]

The actual animation (showing sprites 0, 1, 2, 3, 4, 5 and 6) of a butterfly flying left.

over

Creatures waits til the animation is over before it goes any further so the butterfly moves left a bit and up or down a bit and flaps it wings before the script goes any further and before the reps command comes into play and repeats that bit of script again.

base 0
over
else
doif va00 eq 2

Now this is the other option and will be pretty much the same, except the butterfly is moving right.

rndv va01 0 5

Same as before, we’re getting a random number and putting it into the va01 variable except this time the number is between 0 and 5 so it is a positive number so the x-velocity of the butterfly (velx - how quickly it’s moving left or right) will be positive and the butterfly will move right.

setv velx va01

setting velx to whatever number is in va01

rndv va02 -5 5

Doing the whole vely thing again.

setv vely va02
base 7

The base is 7, just to refresh that means the animation will start at the 7th spot in the sprite file and that 0 in the animation will actually be the 7th sprite and not the first sprite in the file.

anim[0123456]

Butterfly flies right animation is played once.

over
base 0
over
endi
endi
repe

end the repeat loop (the script will only get this far once the loop (all the stuff between reps and repe) has been executed how every many times va04 is.

tick 1

In one tick (between 1/10th of a second and 1 second depending on your system) run the whole timer script again. This is the command that keeps the butterfly constantly flying.

endm

That lovely command which signals the end of the script and time for bed for Helen. :)

Push Script (1)

To make this critter particularly spiffy, I decided to make the critter change direction when pushed by a norn or the hand. :) You could definitely argue that this serves no purpose, but I like to show off what I can do and, well the whole concept of computer games has no purpose either, but we still all play. :) This is a pretty short script (well compared to that last one anyway). What we’ll do is check which way the butterfly’s going, change the direction it’s going in and change the animation so it doesn’t inadvertently fly backwards.

scrp 2 18 31502 1

The ever faithful push script you learnt about way back in the cheeses. :)

setv actv 0

Set the activation status to 0 (not been pushed) so it can be reactivated. When something’s been pushed this is set to 1, so if you want something to be activated more than once you’ll need to include this in your script.

doif velx lt 0

Execute the next bit of script is the x-velocity (how quickly the butterfly is moving in a given direction) is less than 0 (or is negative). If the velx is less than 0 the butterfly is moving right. In that case we want to make it go left.

setv velx 3

We’re changing the velocity to positive 3 (so the butterfly is moving left at a rate of 3).

base 7
anim[0123456]

We’re changing the animation so the butterfly flies in the right direction and doesn’t look dumb flying backwards.

over
base 0
else
doif velx gt 0

If the x-velocity is greater than 0 (ie. positive) and the butterfly is moving right do the next script. If the butterfly is flying right, we want to make him go left.

setv velx -3

Make the butterfly go left by setting it’s x-velocity to a negative number (-3).

base 0
anim[0123456]

Fix the animation so the butterfly isn’t flying backwards. That animation will make sure the butterfly isn’t just moving left, it’s flying left as well.

over
base 0
over
endi
endi
tick 1

We want the timer script to start again as soon as this is over (so the butterfly doesn’t just run out of fuel and stop after being pushed).

endm

Conclusion

You didn’t really learn any new commands in this tutorial, more concepts. I mainly chose to deconstruct the butterfly in particular because it was an excellent example of random variables in action, as well as being a critter and as well as being an object that moved both up and down and left the right.

Extra consolidation

To consolidate your knowledge, here are some challenges for you to check you really understood what was going on in this tutorial

1. Change the animations so the butterfly flies backwards instead of forwards

2. Make the butterfly squish (disappear) when it is pushed instead of changing directions. I’ll put a lovely squish sound file in the zip.

3. Make it so the butterfly can only fly up and down or left and right.

4. Use a random value to make the butterfly’s pose (the sprite that’s currently showing in C2) shift to a random different one when it is clicked and then continue flying

5. Change the butterfly so it flies forwards then backwards on the spot five times when it is clicked.

Credits

The sprite used in this cob was created by Malveka. Thankyou very much for creating such a lovely picture for me to create a cob with.