RealBASIC question

FieldDoc

Registered
I am trying to write a simple war game - tile based. Each unit has several characteristics that I would like to store in some sort of custom array but I have not been able to work out how.

Fow instance, each unit has the following characteristics:
- Name (string)
- Cost (integer)
- Movement (integer)
- Attack (integer)
- Defense (integer)
- Wounded (boolean)

I was hoping to make some sort of custom array so I could do the following:

unit(5).attack = 4
unit(6).name = "dave"

Unfortunately, I haven't the faintest idea how to do this. I tried messing around with creating a custom class (based on the collection superclass) but I couldn't get it to work.

Could anyone offer any suggestions please?
 
An own class for this should work fine. You just have to create the properties you need there and I think you don't need a superclass for this because you just collect own data and maybe some methodes.

Then you have to define an array in your app for this new class like MyArray(100) As MyNewClass
Then create your elements in this array for the parts needed like MyArray(4) = New MyNewClass
After this you could access the properties as MyArray(4).Propertyname=5

It's just an example so play a bit around. Better would be to create methodes that access the data in the class like GetMyProperty and SetMyProperty so you don't need to access them directly.
 
You could make this very detailed or very simple.

I've never worked with RealBasic so I don't know how the syntax differs from Visual Basic. The safest way to go about doing this would be to create a class for your units, something like this:


CLASS Unit

'Public variables that can be accessed directly from outside the class

Public Name AS String
Public Cost AS Integer
Public Movement AS Integer
Public Attack AS Integer
Public Defense AS Integer
Public Wounded AS BOOL

END CLASS


Then in your main code, you can declare an array of Units, like:

DIM Units(NumberOfUnits) AS Unit

Then you can access them as was described above: Units(UnitNumber).Attack, etc.


You might have to go through each one and initialize it with a New operator, depending on how RB works with objects. This isn't a very dynamic way to do things though. You may want to look into if RealBasic supports dynamic arrays or if you can change the size of the array at runtime and add to it when you need to. Also, when you start to learn more about classes (depending again on RB's support of classes) you'll learn all kinds of better ways to do things, like having each type of unit be it's own class, such as Tanks, Planes, Soldiers, etc. This is more complicated at first if you're new to programming, but it makes easier to change and add units down the road.

Start small and read the docs and look for examples on object-oriented programming.
 
Back
Top