Print Page | Close Window

Dynamic Tile Instead of Static Tile (Fix

Printed From: Mirage Source
Category: Tutorials
Forum Name: Temporary Archive (Read Only)
Forum Discription: Temporary 3.0.3 archive tutorials, will be deleted when converted.
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=238
Printed Date: 20 December 2006 at 5:53pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: Dynamic Tile Instead of Static Tile (Fix
Posted By: Sync
Subject: Dynamic Tile Instead of Static Tile (Fix
Date Posted: 11 February 2006 at 3:44pm
Difficulty: Medium 4/5

First, let me give you an explanation of dynamic and static:

dynamic - This is data that can change on the fly. Every non-constant variable you declare in a VB app is dynamic, because you can change it. When we talk about dynamic arrays, we're talking about changing the dimensions of the array on the fly.

static - This is data that can not be changed. A constant is a good example of static data. If you have a specific IP address assigned to you by your ISP that never changes, that IP address is considered to be static. When we talk about static arrays, we're talking about arrays that have fixed dimensions.

In Vanilla Mirage, the Tile array dimensions are declared to a fixed size. Unfortunately, VB has a maximum size for memory used by User-Defined Types of about 64 kilobytes. When you add extra layers to your maps (which go in the User-Defined Type TileRec) you are adding more memory to the MapRec variables. Eventually, if you add too many layers, or you add too many tiles to your map (bigger MAX_MAPX and MAX_MAPY), you will start getting errors telling you that you have too many non-static variables, or that you are using more than the 64K allocated size for User-Defined types.

When you dynamically declare your Tile array, you bypass this feature, because VB doesn't allocate the 64K of memory to your array.. It allocates what it needs when it needs it. So, you, in effect, bypass the 64K limit set by VB.

This is not simple to do.. You have to know what you're doing to use dynamic arrays. This tutorial was designed to help you along your way. It was built for the Vanilla Mirage, so you will have to consider that if you added any new maprec variables in your code. (Including Seamless Maps) The one thing you have to always remember is that the dynamic array has to be given the proper dimensions before you try to write data to, or read data from, it.

The most common problem with setting up a dynamic Tile array is the Run-Time Error 9. This should blow up a light-bulb in your head. I mean, you should feel the pain of the shards as they hit the inside of your skull. It is meant to wake you up, and let you know that you forgot to set the dimensions of your array. Think about it. When you call an array's data, you have to use the subscripts that are within the dimensions of that array. If you haven't declared any dimensions, then any subscript you send to that array will not be there. The application doesn't know where to get the memory you are trying to use, because you haven't declared it. So it tells you 'Subscript out of range.'

How do we make sure our dynamic arrays are declared before we use them? It's simple. If you have a publicly defined dynamic array, you need to find a function to declare its dimensions that gets called before you try to manipulate the data in that array. I know what you're thinking.. How do I do that? Okay, see that "Project" menu at the top of the Visual Basic window? Yeah.. The one that starts with 'P'. :) Click that, and go down to "<Your Project Name> Properties..." A new window will pop up that has the title "<Your Project Name> - Project Properties". See the label "Startup Object:"? Look under that. It will have either a form from your project chosen, or "Sub Main".

If your project starts with a form, go to that form's module, and redeclare the global arrays in the form's load procedure. If it says "Sub Main" then you need to find the Main procedure and redeclare the arrays inside that procedure.

How do you go about redeclaring the size of your arrays? It will look something like:
ReDim Map.Tile(0 To MAX_MAPX, 0 To MAX_MAPY)

Changing code is a learning experience. This tutorial was designed to help further your learning experience. The only code I am going to provide with this tutorial is listed above. Everyone's source is taylored to what they want. Everyone does different things to their source, and giving code out that does specific things just causes people to ask questions because the code provided doesn't work for their source. This tutorial is designed to explain how to make dynamic arrays, and to help you use your mind to figure out how to make it work for you, not to show you how.

Please keep all questions/comments on this tutorial related to what it is, and what it does.. It is meant to be general description of how to make dynamic arrays.

Don't forget that if you change the MapRec User-Defined Type, you will either need to convert or delete your maps. This tutorial does not cover converting maps. (You should already know how to delete them :))

For those of you who aren't sure how to declare a dynamic array.. You do it by placing nothing in between the parentheses. The following code will give examples of static and dynamic array declarations.

Static:
  Tile(0 To MAX_MAPX, 0 To MAX_MAPY) As TileRec

Dynamic:
  Tile() As TileRec



Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info