Dark Basic/Animations

Handling multiple animations in Dark Basic can be a very daunting task. I would know, i've created dark basic games with up to 100 animations per character. You can however make the task a lot easier depending on the way you structure your animation code. This tutorial simply seeks to provide you with a suitable way of structuring your code as to make using your animations and appending new ones as easy as possible.

THE TEMPLATE



Say you have loaded your 3D model, in the context of this example we will use a human being. Now to this human you have appended all your animations, like idle, walk, and attack. Each animation has a frame length of 10, so if appended sequentially the idle animation would take up frames 1 to 10 of the total human model. The walk animation would take up frames 11 to 20 while the attack animation frames 21 to 30. Now each unique animation must be given a uniqe identification number. So we can give the idle animation the ID number 0, walk ID number 1 and attack ID number 2. A good template for handling this sort of animation is given below, don't worry it will all be explained in the paragraphs following it.

rem animation ID variables
stage = 0
oldStage = 0

do

   stage = 0 `idle when pressing nothing

   if keystate(17) = 1 then stage = 1 `walk when pressing W (forward)
   if keystate(31) = 1 then stage = 1 `walk when pressing S (backward)
   if mouseclick() = 1 then stage = 2 `attack when clicking mouse

   if stage <> oldStage `ensure executed only once

       rem setup idle animation
      if stage = 0
         set object speed 1,10
         set object frame 1,1
         loop object 1,1,10
      endif

       rem setup walking animation
      if stage = 1
         set object speed 1,10
         set object frame 1,11
         loop object 1,11,20
      endif

       rem setup attack animation
      if stage = 2
         set object speed 1,10
         set object frame 1,21
         loop object 1,21,30
      endif

      oldStage = stage
   endif

   sync

loop
HOW IT WORKS


You need the stage variable to determine what animation is needed at anytime within the code. The oldStage variable basically stores what is in the stage variable at a specific point in the program. Now placing the 'stage = 0' line at the top is very important because if the player presses nothing we want the human character to do nothing and if you recall, 0 is the ID number for the idle animation. Now if the player moves forward or backwards by pressing the 'W' or 'S' key respectively then we want the human character to walk, this is handled by the following 'stage = 1' lines. Now however, if the player clicks the mouse button we want the character to attack and vice versa.

The next part of the code is the interesting part. Because of the 'if stage <> oldStage' and 'oldStage = stage' statements that entire block of code is executed only once when the stage variable changes. If it remains the same, that block of code is never executed again. This is exactly what we want because executing play and loop commands every causes a kind of unwanted jerking motion. So within this code block, and for every animation stage, you would setup your objects looping/playing speed, set the start frame and the frames it should loop betwee. Remember that the idle frame was between 1 and 10, well that is why you loop between 1 and 10 under stage 0 in the code.



updated 13.10.06