Thursday, September 16, 2010

Beginner Tutorial Part 3

Welcome to the final part of the beginner tutorial.  Our chocolate vendor is almost ready.  All we have left is to

  1. Have it vend chocolate
  2. Make the chocolate edible

Which might not seem like much, but there’s a LOT of new concepts here.

Your script so far should look something like this:

new: simp 2 23 9000 "moe_tutorial_chcd" 7 0 300
bhvr 1

attr 198
elas 10
aero 5
accg 7
fric
50
mvto 6100 9110

scrp 2 23 9000 1
lock
    frat 2
    anim [0 1 2 3 4 5 6]
    over

 

rscr
enum 2 23 9000
    kill targ
next

Now, it’s time to make chocolate come out of our vendor. But before we actually put in the script for making a chocolate bar, we already know that we’re going to need to move the chocolate bar to the vendor’s current location.  So how do we find that?

Well I need to introduce you to the concept of the TARG.

Let us imagine for a moment that we are the engine running this script.  For everything, every code, and ever alteration that occurs, we are working with the “owner” of the script, that is in this case, the chocolate vendor.  As soon as we add the code for the new piece of chocolate, our “targ” is going to change to the chocolate.

I have highlighted this because it’s an important concept.  There are codes to get the position of an object which we will be using, but in order for us to get the position of the vendor, and move the chocolate to it, we need to set a couple variables to store the vendor’s position before we create the chocolate bar, because the codes are only going to work on the currently targeted object.  You’ll see what I mean below.

Our next lines of CAOS are:

setv va00 posl
setv va01 posy

But what does that actually mean? 

The primary way of making objects change and interact is to set variables, and operate (change, divide, add, subtract, etc) on them.  There are five types of variables we can define:  Object Variables, Game Variables, Engine Variables, Named Variables, and Script Variables.

Each type of variable has a different “name”, and is used for different purposes, but each one can hold anything from a number, to a sentence, to an agent in the world.  Variables are VERY powerful.

In this case, we’re using a Script Variable.  You can define in any Script Variable from va00 to va99.  These variables are useful because they will IGNORE the current target and stay in the script until it ends, at which point, they’ll be discarded and lose their values.

So for instance, we are telling the engine that we want to setv (set variable) va00 to the position left of the current target (which at the moment is our Chocolate Vendor).  What this command will do is examine our vendor, and it will look for the left-hand side of the vendor, and return whatever X-Coordinate on the world that happens to be.  Just like using the coordinate tool we used to place our vendor in the beginning, but automatically.

The second command did the same as the first, except it returned the Y-Coordinate of the world at the center of our agent.

-----

Below is a diagram of all the position commands and what value they’ll return to help you understand what each one does.

posr” will return the X coordinate of the right of the agent.

posl” will return the X coordinate of the left of the agent.”

post” will return the Y coordinate of the top of the agent.

posb” will return the Y coordinate of the bottom of the agent.

posx” and “posy” will return the X and Y coordinates, respectively, of the center of the agent’s bounding box.

If this confuses you at first, don’t worry. Just keep it in mind, and you’ll understand what happens when we make the chocolate bar. :)

-----

Let’s make the chocolate now.  Add this after the last command we typed.

new: simp 2 11 9000 "moe_tutorial_chcd" 2 7 301
bhvr 48

attr 195
elas 10
aero 5
accg 7
fric
50
mvto
va00 va01

Much of this is review, so I won’t go through it all, but basically, we’re making a new: simple object, which is a generic (2), food (11), in the 9000 range. It uses the same sprite file as our chocolate vendor, but only uses 2 frames, and starts at frame 7.

Our chocolate is edible and and carryable by creatures, so it’s bhvr is 48 (16 + 32), and it has all the same attributes as our vendor, except it cannot be activated.  The rest is the same, except for the mvto command.

This mvto command is moving the piece of chocolate, not to a predefined coordinate, but to the variables we saved earlier.  Remember, va00 is the left hand side of our vendor, while va01 is the Y coordinate for the center of our vendor.  We defined those earlier.  Now you may be beginning to understand why we set those variables before making the chocolate.

As soon as we used the new: simp command, our target changed.  If we were to “setv va00 posl” right now, va00 would be set to the left-hand X-Coordinate of the chocolate, not the vendor, since the chocolate became our target when it was made, and since technically the chocolate hasn’t been placed anywhere, it would return a value of 0, and would never be moved! D:  That’s why it was important to set our variables for the position of the vendor before creating the chocolate.

-----

Now, we just made some chocolate come out of our vendor. Let’s give it a kick.  It looks like the chocolate would come out of the vendor and go flying left a bit, so let’s give it some left velocity.

Add a “setv velx –10” to give it some “umph” coming out. This will tell the engine to make this object move, that is, set it’s velocity to, –10 pixels a second along the X axis.

-----

Ok, so we have our vendor, and when it’s pushed it animates, vends some chocolate, and then what?  Well it needs to go back to it’s original pose now that the chocolate has popped out.

To do this, we’ll add “targ ownr” which tells the engine to target the owner of the script (our chocolate vendor) because right now we’re still targeting the chocolate, and then we’ll add “pose 0” on the next line, to tell it to set it’s pose back to 0.

That’s all this script needs to do, so now we need to end the script. To do this, we add an “endm” line. This tells the engine that’s the end of the script, it can close it up and add it to the Scriptorium and/or stop running it.

Your script should now look like this:

new: simp 2 23 9000 "moe_tutorial_chcd" 7 0 300
bhvr 1

attr 198
elas 10
aero 5
accg 7
fric
50
mvto 6100 9110

scrp 2 23 9000 1
lock
    frat 2
    anim [0 1 2 3 4 5 6]
    over

setv va00 posl
setv va01 posy

new: simp 2 11 9000 "moe_tutorial_chcd" 2 7 301
bhvr 48

attr 195
elas 10
aero 5
accg 7
fric
50
mvto
va00 va01
setv velx –10
targ ownr
pose 0

endm

rscr
enum 2 23 9000
    kill targ
next

 

We’re almost done!  But oh no! We forgot that this vendor has a sound file!  Right before it pops out the chocolate, it needs to play the vending sound.  To do this, simply type:

sndc “vdnr”

This tells the engine to play a sound that is controlled, named “vndr”. 

Put this piece of CAOS right above the “setv va00 posl ” command, that way it makes the sound just as it’s vending, right before chocolate pops out. :)

new: simp 2 23 9000 "moe_tutorial_chcd" 7 0 300
bhvr 1

attr 198
elas 10
aero 5
accg 7
fric
50
mvto 6100 9110

scrp 2 23 9000 1
lock
    frat 2
    anim [0 1 2 3 4 5 6]
    over

sndc “vdnr”
setv
va00 posl
setv va01 posy

new: simp 2 11 9000 "moe_tutorial_chcd" 2 7 301
bhvr 48

attr 195
elas 10
aero 5
accg 7
fric
50
mvto
va00 va01
setv velx –10
targ ownr
pose 0

endm

rscr
enum 2 23 9000
    kill targ
next

There.  Now we just need to make the chocolate edible and we’re done. :)

To make the chocolate edible, we’re going to make a new script.  For future reference I won’t tell you to close the script with “endm” just know that it must be done. I usually just write out the script’s name (scrp X X X) and endm first, then just place my cursor in between and type, that way I never forget an endm.

So!  We need to make an eat script for our food.  If you remember how we made the activate script for our vendor, this should just be review. 

scrp 2 11 9000 12 (remember this is our food, it’s 2 11, not our vendor, which was 2 23)

stim writ from 79 1
sndc "chwp"
doif pose eq 0
    pose 1
elif pose eq 1
    kill ownr
endi

endm

First we declared the new script. It’s an eat script (12) for our generic object (2), which is food (11), with a species of 9000.

Then we told the agent to “stimulate” the from in a certain way.  The from will be whatever agent activated this script. In this case, a creature will most likely activate it, so it will stimulate the creature.  Examine your Beginner’s document for more information about stim writ, or check the list of stimuli in the CAOS Documentation. The value of 1, was the amount we wanted to stimulate the creature.  I went ahead and had it stimulate the creature fully.  When you stim a creature, they learn from the action. The creature that eats this chocolate will learn that it gains nutrients from chocolate. :)

Then we had it produce a sound, in this case, a nice chomping sound called “chwp”.

The next bit is going to take some explanation.  Welcome to the world of the doif.  It’s one of the most important commands in the game, and the most versatile.  Luckily it’s pretty self-explanatory.

[Do] if something equals something,

do something.

[Else] if something eq something other than the first value we tried,

do something else.

end the doif block.

So basically it reads, if the pose eq 0, set the pose to 1, if pose eq 1, kill the owner of the script (the piece of chocolate).  It’s very simple if you just read it. :)

This basically tells our block of chocolate that it can be eaten twice.  On the first try it changes it’s pose to have a bite out of it.  On the second try, it’s destroyed.

-----

Now there’s ONE last thing we need to do.  Add the chocolate pieces to our remover script, and also make our remover script able to remove all the scripts we’ve added to the Scriptorium.

enum 2 11 9000
    kill targ
next

scrx 2 23 9000 1
scrx 2 11 9000 12

----

The scrx command will remove whatever script number you put after it from the scriptorium. We had an eat script for the chocolate, and an activate script for the vendor, so we scrx’ed both of those.

And that’s it!!!! Our chocolate vendor is FINISHED!

Bellow is the full code. :)

 

new: simp 2 23 9000 "moe_tutorial_chcd" 7 0 300
bhvr 1

attr 198
elas 10
aero 5
accg 7
fric
50
mvto 6100 9110

scrp 2 23 9000 1
lock
    frat 2
    anim [0 1 2 3 4 5 6]
    over

sndc “vdnr”
setv
va00 posl
setv va01 posy

new: simp 2 11 9000 "moe_tutorial_chcd" 2 7 301
bhvr 48

attr 195
elas 10
aero 5
accg 7
fric
50
mvto
va00 va01
setv velx –10
targ ownr
pose 0

endm

scrp 2 11 9000 12

stim writ from 79 1
sndc "chwp"
doif pose eq 0
    pose 1
elif pose eq 1
    kill ownr
endi

endm

rscr
enum 2 23 9000
    kill targ
next

enum 2 11 9000
    kill targ
next

scrx 2 23 9000 1
scrx 2 11 9000 12

No comments:

Post a Comment