Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » Visual Basic O.O

This thread is locked; no one can reply to it. rss feed Print
Visual Basic O.O
Archon
Member #4,195
January 2004
avatar

This is probably a weird forum to post in but I thought maybe you guys were friendlier than the ones at xtremevbtalk.com ...
Its to do with game programming so I qualify to stay here :P

Anyway with my game. Im not sure how to do object handling. Someone told me to do 'Collections' but didnt get too 'on the topic'.
Anyone know how I could do object handling and that when an object (class) is created that itd be able to run a particular method (called Step) ?

23yrold3yrold
Member #1,134
March 2001
avatar

Well, I would keep all my objects in an STL container of pointers to objects. Matter of fact, I do. ;)

Is a "Collection" a VB style container? I assume this is a VB question from that url. ;) A virtual function called Step() takes care of your last problem in C++ ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Matthew Leverton
Supreme Loser
January 1999
avatar

Add a class module to your project. With that, you can create objects and methods.

Archon
Member #4,195
January 2004
avatar

I know how to create classes and then objects, but I dont know how to set it to DO STUFF every x milliseconds...

Matthew Leverton
Supreme Loser
January 1999
avatar

You are not being very clear on what you want to do.

You could create a Timer object that belongs to the class. My VB is rusty, but something like this in the class module's global scope:

Private t As Timer With Events

Archon
Member #4,195
January 2004
avatar

What I want to do is:
Im a space ship, the object name is 'Player as new clsPlayer' (I mean just 'Player')

The player moves around and when the player presses CTRL, it creates an object from the 'clsLaser' class module... I want it to be created, every 40th of a second: move a certain amount of 'x' any 'y' and then do some collision detection with all other ship objects and then if it is destroyed: get deleted from memory...

But I dont know how to keep track of all the objects so say with collision detection, for it to check collision with all ships
--------

Collection is a 'list object' pretty much I think

Billybob
Member #3,136
January 2003

Just put all your objects into arrays/an array. Then you can cycle through them and do collision detection, velocity stuff, etc...

As for timing things, a timer is one of the default controls you get. Slap it on the dialog, set the settings, add code to the timer's event, done.

Archon
Member #4,195
January 2004
avatar

If I use algorithms, would you know how to make sure that when an object is 'destroyed' that the array sorts itself so I dont have 'null' arrays?
-----------
Ok here is my code (testing OO)

1Private Objects(1 To 3) As Object
2 
3Private Sub Form_Load()
4Set Objects(1) = New Class1
5Set Objects(2) = New Class2
6 
7Set Objects(3) = New Class2
8Set Objects(3) = Nothing
9Set Objects(3) = New Class1
10 
11For Index = 1 To 3
12 Objects(Index).Step
13Next Index
14 
15End Sub

But I need to check whether an array is pointing to an object or whether it is 'Nothing' but I dont know how to check it... Any ideas?

Billybob
Member #3,136
January 2003

There are two ways I can think of.
1) Require that every array entry be filled with some sort of class. Make sure every class has a "isDead" function. You get the idea.
2) Resizable arrays. You can resize an array with ReDim(google tutorials on that if you don't know how to use it). When an object is deleted you simply take the object from the back, put it where the deleted object is, and resize the array to 1 less size. in For loops you go from 0(depends on how you allocate) to UBound(Objects), IIRC.

Archon
Member #4,195
January 2004
avatar

Interesting... Ill check out both ideas. But when you go 'Set Objects(whatever) = Nothing' does that free up memory?
----------
I think ill chose option 2... Very clever methinks
This is my code updated:

1Private Objects(1 To 4) As Object
2 
3Private Sub Form_Load()
4Set Objects(1) = New Class1
5Set Objects(2) = New Class2
6 
7Set Objects(3) = New Class2
8Set Objects(3) = Nothing
9Set Objects(3) = New Class1
10 
11Set Objects(4) = New Class1
12Set Objects(3) = Nothing
13Set Objects(3) = Objects(4)
14Set Objects(4) = Nothing
15 
16For Index = 1 To 3
17 Objects(Index).Step
18Next Index
19 
20End Sub

Peter Hull
Member #1,136
March 2001

You can use collection like this

Dim Lasers as New Collection
Lasers.Add(New Class1)
' Add as many as you like
...
Dim i
For Each i in Lasers
 i.Step
Next i

If you give your class an Active property then you can get rid of lasers that have gone off screen like this

Dim i as Integer
i=1
Do While i<Lasers.Count
 If Lasers(i).Active Then
  i=i+1
 Else
  Lasers.Remove(i)
 End If
Next i

Once the object has been removed it will be deleted.

HOWEVER, you'll be doing a lot of creating and destroying Objects which might be slow. In that case, preallocate an array of as many lasers as you need, and use the Active property to only update the active ones. When the player presses fire, search the array for an inactive laser and make it active.

Pete

[edit] Formatting :P

Archon
Member #4,195
January 2004
avatar

Well is alot of creating and deleting arrays time consuming for the computer? Or is it mainly Collections?

Rick
Member #3,572
June 2003
avatar

Quote:

But I need to check whether an array is pointing to an object or whether it is 'Nothing' but I dont know how to check it... Any ideas?

use Is Nothing in a If statement.

For Index = 1 To 3
   If Not Objects(index) Is Nothing Then
      Objects(Index).Step
   End If
Next Index

Resizing the array will eat up some time. The active flag is good, but you still leave unused memory out there. Unless you reuse the object sometime.

The best solution is uing For Each Loop instead of the normal For Loop. That way you only loop through the valid objects. I haven't use For Each in a while, but search it on the net and you should find some examples.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Go to: