Behaviour, Attributes and Events

IMPORTANT: Make sure you have no scripts in the scriptorium for the object 2 1 10000. If you have you should either change all references to 2 1 10000 in the tutorial to some other classifier that is free, or remove the scripts. Viewing and removing scripts can be achieved using the CyberLife CAOS Tool or any other tool that allows DDE communication and script injection. Refer to the author of that tool for instructions.

With the CAOS tool you can check the scriptorium by opening the scriptorium viewer from the 'view' menu. Wait for it to load and then open up the tree structure and go down the path for family 2, genus 1. If you can see an entry for species 10000 then you need to remove the scripts for it - if not you are clear to proceed!

Let's start by creating a simple object, we'll use the ball sprite again, but this time we'll include some event scripts as well as a chunk for creating the object.

inst

new: simp bal2 8 0 9000 0

setv cls2 2 1 10000

setv attr 198

edit

endm

 

scrp 2 1 10000 1

setv vely -70

setv velx 50

endm

 

scrp 2 1 10000 2

setv vely -70

setv velx -50

endm

 

scrp 2 1 10000 0

setv vely -70

endm

If you are unsure of any of these commands then please refer to the CAOS Language Guide.

So, we've created the same ball we used in tutorial 1 but notice that the value for attr has increased by 4 - this allows the object to be activateable (which means responds to mouse clicks). As well as the ball we've also specified some event scripts - namely an activate 1 that will launch the ball to the right, an activate 2 that will launch the ball to the left and a deactivate that will launch the ball straight into the air. So, inject these into Albia and drop the ball onto the floor. If you try and click the ball you'll notice that nothing happens -why? Even though the ball has attr set to allow it to respond to mouse clicks it doesn't know what it's supposed to do when it gets one - this is where bhvr comes in. So lets delete the ball and create a new one, bhvr has two components to it - one for what happens when the hand clicks on it (the first number) and the other for what a creature can do to it. As we are not dealing with creatures here we will only be modifying the first value. Change your ball creation chunk so that it becomes:

inst

new: simp bal2 8 0 9000 0

setv cls2 2 1 10000

setv attr 198

bhvr 1 0

edit

Now inject this into the world and click on it. If you have successfully managed to inject the new object and the scripts we wrote then the ball should fly off to the left. Try clicking again and you'll find that the ball goes nowhere this time - why is this? The reason is that each object has an attribute called it's actv flag that gets changed whenever an event script is executed on it. You can use the chunk below to display the value of actv in your DDE output window (in the CyberLife CAOS Tool this is called the Script Output window):

rtar 2 1 10000

dde: putv actv

and you should find that it is 1 - which is what an activate 1 script will change it to. The value we chose for bhvr - which was 1 - is referred to as monostable. This means that further clicks have no effect until the object has become inactive again (actv 0) - so how does an object become inactive again? In this example the thing to do would be to alter the activate 1 script for the object so that the last line of it reads:

setv actv 0

If you make this change, remove the ball and then re-inject your object and scripts you should find that it now responds every time you click on it.

Ok, lets delete the ball and go back to the original event scripts - without the setv actv 0 in and re-inject them. This time we'll create the ball but give it's bhvr a value of 3. This corresponds to a toggle - and will alternate the event sent on each click from an activate 1 to a deactivate and back again. Inject this ball and click it a few times. The first click should send it to the left, the second should launch it straight up.

Now we come to the last value for bhvr which is 4 - this corresponds to a cycle. The first click will activate 1 the second will activate 2 and the third click will deactivate. Remove the previous ball and create a new one using a value of 4 for bhvr, click it 3 or more times and notice it's response.

If you use the DDE command to show the value of actv between clicks you will see it alter with each click in all of these examples - to a 2 after an activate 2 and to 0 after a deactivate. This is crucial for the type of bhvr you give your object because it is the value of actv combined with the objects bhvr that interprets which event should be run.

Try changing the activate 1 script so that it finishes with a setv actv 0 this time and create a ball that should cycle (bhvr 4 0). Now click on it once and find out the value of actv - no surprise, it should be 0. But now click on it a second time and you'll see that it runs the activate 1 event again instead of the activate 2 that you might have expected. This is because it relies on actv being 1 (i.e. "I have just run an activate 1 event") before it can run an activate 2 event - and similarly it will need an actv value of 2 before it can run a deactivate.

That's it as far as responding to user clicks goes, but the other part of bhvr refers to what actions a creature can perform on an object. There are no settings like 'toggle' or 'cycle' because the creature itself decides what action to take - all you can specify is what actions are possible. One thing to bear in mind is that as far as a creature is concerned all objects are the equivalent of monostable - they can only receive an activate 1 or activate 2 if they are already inactive. The values for actv after a hit or eat event are different though - and change the value of actv to 3 and 4 repectively. These values do not stop an activate 1 or activate 2 event happening - only a repeat of the same event, either eat or hit.

A deactivate event will only be received if the object's actv value is greater than 0.

There is another kind of event that is used quite often in objects - a timer event - which doesn't directly come from an activation, pickup or similar game occurence.

The way a timer operates is by giving an object a value for tick - this then counts down and when it gets to 0 it will run the objects timer script and set tick back to the starting value. So, to start a timer you specify a non-zero value for tick, and to stop the timer you give tick a value of 0.

The example below creates a ball object that receives a timer event every 20 ticks (roughly 2 seconds) - this event gives it velocity.

inst

new: simp bal2 8 0 9000 0

setv cls2 2 1 10000

setv attr 198

bhvr 3 0

mvto 4856 432

setv grav 1

sys: camt

tick 20

 

scrp 2 1 10000 9

setv vely -90

endm

You should now have a ball that is periodically thrust into the air. This is all well and good for things you want to always receive a timer event, but quite often you want a timer to start and stop based on some other criteria. If you add the following events into the world we can change the way the ball works.

scrp 2 1 10000 1

tick 20

endm

 

scrp 2 1 10000 0

tick 0

endm

Now you can click on the ball and with an activate the ball's timer will start, and with a deactivate the timer will stop.

Have a play with the ball to make sure you understand what is happening, notice the delay between an activate click and the timer starting - this is the tick counting down.

Try changing the value of the tick in the activate 1 script above - reduce it to 10 and then click on the ball to activate it. It will rise off the ground with each timer event and this is because the timer is running so fast the object is not settling on the floor before receiving the next burst of velocity.

Lets stop there and get on with the next lesson.