AC
Result# = AC(IncConstant)
 
Parameters:

    IncConstant = The constant value the auto counter will be increased
Returns:

    Result# = Value of Auto Counter before adding the users increment constant
 

     PlayBASIC's Auto Counter commands will help you to create lists of unique numeric values for use as structures or unique states within your games.

      The Auto Counter is an internal value built right into the PlayBASIC compiler. This value is increased each time the AC function is used within your code.

     AC works much like a normal function, that being,it takes a constant input value, performs a calulation upon and returns a constant result.

      This calculation is not done at run time however, but at compile time. What actually occurs is the compiler evaluates the result from the AC() function, then inserts that result back into the compiled code as a constant in it's place.

      Since it's evaluated during compilation, the AC parameter can only be a constant (literal) value.


What's it used for ?



      The main reason we might use the Auto Counter feature in the compiler, is to create a series meaningfully named constants that all have unique values. Which in game programming is often used signify particular state or mode of a game character.

      For example, we'll often use states such as Alive or Dead in our programs, just numerically. So we might use, one to represent when a character is alive and a zero it's dead. This is fine if we only have two states characters can be, although it's not that readable really. By that I mean, if I leave the program for a week or two and come back to it later, would I still be able to easily decipher what it's doing without having to dig through it ? - Maybe, but probably not.

      What we could do, is represent our character states using a series constants. This allows us to the create meaningfully named constants that can be used throughout our program to signify exactly what each state is. It's subtle things like that, that can help the readability of the our programs dramatically. Generally, the more readable a program is (closer to English), the easier it is to understand. So to create a couple of constants to represent our characters state, might look like this in code.

  
  Constant Dead =0
  Constant Alive =1
  


      Now, what if our characters have lots and lots of different states (actions if you live) they might perform within our game ? - Obviously the more states we have, the more important it becomes to use meaningful named constants. Now we could run through and assign each constant a unique numerical value by hand, as seen in the example above.

      However, this can some be somewhat error prone. As it'd be easy to give two states the same value. It's important that our states are unique, so that our program code can clearly identify each state and performed the required action. For example, imagine if we have a Walking and Running state of our character, but mistakenly gave them the same value. How would our program know the difference between them ? - It wouldn't. Which, can be a very difficult bug to pick up in our code, particularly as our programs get longer.

      This is where Auto Counter becomes really useful. It's entire purpose is to aid in building series of constants with unique values. So by using it, we can help make our programs that little bit more bullet proof. For example, lets imagine we have a character that has the following states, Alive , Dead , Walking , Running , Jumping , Climbing , Shooting , Crawling & Resting . We could create our tokens like this.

  
  Constant Alive      = AC(1)
  Constant Dead          = AC(1)
  Constant Walking     = AC(1)
  Constant Running     = AC(1)
  Constant Climbing     = AC(1)
  Constant Jumping     = AC(1)
  Constant Shooting     = AC(1)
  Constant Crawling     = AC(1)
  Constant Resting     = AC(1)
  



      Since each constant is assigned the result of the Auto Counter, it'll be given a unique value. Each time the AC function is called the Auto Counter is bumped internally. So they'll all unique. If I want to add another state, I'd just Cut'n'paste the line and change the name. So PlayBASIC would take care of it's value for me.




FACTS:


      * Even though AC() is a built in function, since PlayBasic V1.64M, it can also be used in assignments. Making the following (Ac= 10) identical to using AcSet =10




Mini Tutorial:


      Showing the use of AC, ACSet and ACReset

  
; Set the Auto Counter to 0
  ACReset
  
; Using AC multiple times, and it increases
; Auto Counter value by 1 each time
  Print AC(1)
  Print AC(1)
  Print AC(1)
  
; Set the Auto Counter to 10
  ACSet = 10
  
; Since the auto counter is evaluated at compile time,
; you can assigned it's value to constants as well
  Constant Play_Mode = AC(2)
  Constant Menu_Mode = AC(2)
  
  Print Play_Mode
  Print Menu_Mode
  
; Display the screen and wait for a key to be pressed
  Sync
  WaitKey
  


This example would output.

  
  0.0
  1.0
  2.0
  10
  12
  




 
Related Info: ACReset | ACSet | Constant | Literals | Select | Types :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com