# Use of Data Tables

## Recommended Posts

I have been quietly working on my game still. No huge developments but I have been stick on the method in which I change levels. From reading about data tables, I had initially thought that I could make a table of various values and when a value is reached the level would change. I think I have settled on the number of shopping carts collected being used to determine stage, like waves of enemies.

My understanding is that I would use a variable as a pointer and that would look at the data table and choose the value in the table. So for example, if you collect 5 carts, it will point you to a certain value on the data table and then depending on that number it will send you to a different stage or incremental increase in difficulty.

I was wondering if this is an overcomplication of something simple or am I on the right track? Would using a data table help save variables or space as opposed to using other methods?

##### Share on other sites

I guess I am trying to determine the benefits of use or if what my intended use is an effecient and effective method.

##### Share on other sites

I'd probably just use a persistent variable to keep track of the current level / wave, and use the level number for calculations involving number of carts to collect, and things related to difficulty. E.g. number of carts to collect is X + level number, or time in the level is Y - level units of time. Things that would be too complex to calculate on the fly could be in tables indexed by the current level, certainly.

##### Share on other sites

Okay. I think I understand. I have been going back and forth on various ways to accomplish this. I still have some variables. I had thought about using a couple variables.

So maybe X for carts and Y for level. As an example, maybe each wave equal 20 carts so that it may be something like

If x=20 then y=y+1 or something along those lines and then have some things dependent on the level variable.

For some reason I was thinking a table might be easier but I wouldn't really use it for much else. I guess I didn't quite understand their purpose. Since I hadn't had much time to work on the game I've been trying to wrap my head around some of these concepts.

Thanks for the pointers. I hate asking too much without doing my homework but data tables kind of confused me for a bit.

Edited by KevKelley

##### Share on other sites

A variable for the level sounds like the best way to do what you want to do. Using a data table would over complicate things.

Data tables are good for calculations that are too complex or cycle heavy to do in game. Instead of Y = X + (complicated math stuff) you could use Y = answertable [X] where answertable is a data statement containing answers to the complicated math stuff for values of X.

They're also good for situations where there are too many possiblities to use if...then statements. Instead of if X = 0 then COLUP0 = \$66, if X = 1 then COLUP0 = \$68, if X = 2 then etc., etc., you could use COLUP0 = P0color [X] and list all the color choices in data statement P0color.

##### Share on other sites

I usually use a duration variable for each sound effect.  So, to activate a laser blast sound I set its variable to something non-zero like 15.  Each cycle of the main program loop I check for the laser blast sound variable to be more than zero.  If it is I set the sound registers appropriately and then decrement the laser blast duration variable.

The big drawback is, of course, each sound effect takes up an entire variable.

##### Share on other sites
3 hours ago, Gemintronic said:

I usually use a duration variable for each sound effect.  So, to activate a laser blast sound I set its variable to something non-zero like 15.  Each cycle of the main program loop I check for the laser blast sound variable to be more than zero.  If it is I set the sound registers appropriately and then decrement the laser blast duration variable.

The big drawback is, of course, each sound effect takes up an entire variable.

That was one of the big problems I hit when I was first learning. I used up all my variables pretty quickly so now I am trying to look at their use critically and try to determine if I really need to use them.

##### Share on other sites
3 hours ago, Gemintronic said:

I usually use a duration variable for each sound effect.  So, to activate a laser blast sound I set its variable to something non-zero like 15.  Each cycle of the main program loop I check for the laser blast sound variable to be more than zero.  If it is I set the sound registers appropriately and then decrement the laser blast duration variable.

The big drawback is, of course, each sound effect takes up an entire variable.

Not at all.  You can use separate variables for the sound effect type and duration.  That's 256 different sounds for a duration of 255 frames (or bars).  If the game does not need a fraction of that mess, you can split the sound effect type and duration within a single variable...such as 16 sounds each having a duration of 15.  Specific coding for each sound effect can determine when the duration portion of the variable is decremented.

##### Share on other sites

Bitwise operation is still new to me and something I am slowly learning. In regards to variables, I have learned to use them much better or even use the same variable for multiple purposes if it is not to be in use 100% of the time for any particular reason.

I will probably want to learn bitwise operations more once I start to code the sound effects for my game. Currently the only sounds I have are simple beeps or boops or random frequencies for variation purposes.

I had tried playing around with data tables for sound but I didn't fully understand them when I started, although I kind of got the effect I was looking for through trial and error (the lightning sound in Bag Boy).

##### Share on other sites

Then the easiest way is to use 2 variables, one for the sound effect type and the other for duration.

Check duration first.  If it is zero, go to the end (no sound).

Otherwise, subtract 1 and use an on-goto statement with the variable holding the sound effect type to jump to the noise to be played.

##### Share on other sites

I use bitwise operations for levels and conditions ect or variables if i really need to depending on circumstances. Bitwise operations are definitely worth your while learning, as in many cases they can "somewhat" take the place of what 8 variables can achieve (sort of). Or as i like to think of it, a "flexible" variable so to speak. So lets say you use 4bit's for deciding the level, you still have 4bit's remaining for other conditions such as trolley speed functions ect ect.

The bonus is rather then using a variable as a single switch you have the option of 8 switches. Very useful for on/off - true/false situations as you may want to make a change to something like trolley speed yet the stage hasn't completed yet ect. That can all be achieved using an single variable (basically more control of that variable) IMO.

Sorry if it sounds confusing but makes sense in my head.

##### Share on other sites

That is how I first started (using a sample program). But then I used another 2. And another 2. And another 2... Lol

##### Share on other sites
1 minute ago, TwentySixHundred said:

I use bitwise operations for levels and conditions ect or variables if i really need to depending on circumstances. Bitwise operations are definitely worth your while learning, as in many cases they can "somewhat" take the place of what 8 variables can achieve (sort of). Or as i like to think of it, a "flexible" variable so to speak. So lets say you use 4bit's for deciding the level, you still have 4bit's remaining for other conditions such as trolley speed functions ect ect.

The bonus is rather then using a variable as a single switch you have the option of 8 switches. Very useful for on/off - true/false situations as you may want to make a change to something like trolley speed yet the stage hasn't completed yet ect. That can all be achieved using an single variable (basically more control of that variable) IMO.

Sorry if it sounds confusing but makes sense in my head.

Sounds simple. The hard part for me is translating that into code that works for my intended purpose. But I have learned that as I get more comfortable with programming it gets easier seeing the finer details

##### Share on other sites
13 minutes ago, KevKelley said:

Sounds simple. The hard part for me is translating that into code that works for my intended purpose. But I have learned that as I get more comfortable with programming it gets easier seeing the finer details

Exactly! The more you experiment the easier it becomes. Just remember you can still read or write to that variable as a value, which sometimes can be useful for further checks.

So if you have a binary value of 10010111 for that variable being used as bitwise for whatever reasons you dont need to make 8 different checks. Just convert that binary number to hex and you should know what value you're looking for. You can use something as easy as: if x = \$97 then blah blah blah that will check all those conditions as a whole rather then individual checks.

##### Share on other sites

Think of the basic set of game variations in Space Invaders...moving shields, zigzagging bombs, fast bombs, or invisible invaders.  That is 4 variables.  But the game does not store these variables other than the game selection value itself.  This variable begins with a value of zero (Game #1...Single Player, no options).  If game select is pressed, the value is increased to 1 (Game #2...Single Player, Moving Shields).  Press it again and it goes to 2 (Game #3...Single Player, Zigzagging Bombs).  And so on.  But if you look at those values in binary, you can see a pattern emerge:

```Game 1 %00000000 One Player, No Options

Game 2 %00000001 One Player, Moving Sheilds

Game 3 %00000010 One Player, Zigzagging Bombs

Game 4 %00000011 One Player, Moving Shields & Zigzagging Bombs

Game 5 %00000100 One Player, Fast Bombs

...etc

```

So to check if the game features moving shields, the program just needs to see if the rightmost bit of the game variation variable is 1.

For zigzagging bombs, check the next bit higher.  For fast bombs, the 3rd from the right.  And invisible invaders are selected if the 4th from the right is 1.  Games 1 to 16 are all combinations of those 4 variables...and the remainder of the game variations are all 2-player modes, in groups of 16 which contain this set of the game variations found in the first 16.

##### Share on other sites

Now when you use bitwise operations, can you also define a variable? So have x= 4 but then check the various bits for different options?

##### Share on other sites

I may have to redo some of the game logic when I get to that point but luckily what I have isn't too complicated. Reading these comments make much much much more sense than what I was considering, which was having several variables throughout the code so if something was chosen by that particular variable would impact everything for that option

##### Share on other sites
4 minutes ago, KevKelley said:

Now when you use bitwise operations, can you also define a variable? So have x= 4 but then check the various bits for different options?

Sure can you can either use the way you mentioned or create an alias. Personally i usually use this method only because i find it works for my needs. There is a few others though, and this example is straight from RT's site

```   dim _my_variable = a

_my_variable{0} = 0
_my_variable{1} = 0
_my_variable{2} = 1
_my_variable{3} = 0
_my_variable{4} = 0
_my_variable{5} = 0
_my_variable{6} = 0
_my_variable{7} = 0

rem  *  That will set _my_variable to 4, or %00000100.```

##### Share on other sites

^ In practice, you generally would not be setting or clearing the bits that the current routine is not interested in...just change the specific bit(s) you need.

##### Share on other sites

I will note to save any confusion that reading what bit is in an on or off state needs to be read like this

```if _my_variable{2} then blah
if !_my_variable{2} then blah```

so the top line is the syntax to check if bit 2 is on and the other if bit 2 is off.

-----------------------------------------------------------------------------

As for writing/changing the its condition you use this syntax.

```if _my_variable{2} then _my_variable{3} = 1
if _my_variable{3} then _my_variable{2} = 0```

So basically the first line in this example checks if bit 2 is on if so then it will flip bit 3 to an on state.

The second line checks if bit 3 is on and if so it then flips bit 2 to an off state.

You can see the only values used are 1 or 0 for an on or off state. Also if you wanted to check if a bit is off it would be

if !_my_variable{2} then blah blah

-------------------------------------------------------------------------------

There is also the option to check two conditions like

`if !_my_variable{2} && _my_variable{3} then _my_variable{4} = 1 : _my_variable{3} = 0`

So basically this line checks if bit2 is off and if bit3 is on and iff so then flip bit4 on and flip bit3 off

Hope this helps

Edited by TwentySixHundred

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.