Decrementing an array of classes
Author |
Message |
TheOneTrueGod

|
Posted: Sat Apr 29, 2006 8:14 am Post subject: Decrementing an array of classes |
|
|
Allright, first some background info:
code: |
type Bullet :
record
x, y : int
end record
var B : array 1 .. 10 of Bullet
var BCount : int := 0
loop
if BCount < 10 then
BCount += 1
B (BCount).x := Rand.Int (0, maxx)
B (BCount).y := 0
end if
for i : 1 .. BCount
B (i).y += 10
if B (i).y > maxy then
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
B (i) := B (BCount) %This is the line I need focused on.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
BCount -= 1
else
drawfilloval (B (i).x, B (i).y, 10, 10, black)
end if
end for
View.Update
Time.DelaySinceLast (30)
cls
end loop
|
Allright, basically, when I want to get rid of one element of type "bullet", all I have to do is set the entire record to the last record, and then remove the last record from existance.
Now, what I want to know is, how can I do this with classes?
code: |
Bullet(B(i)) := Bullet(B(BulletCount))
|
^^^
The above code didn't work for me, and doing something like
code: |
Bullet(B(i)) := nil
|
Won't really help me in this situation, because then i'll have a large nil sitting in the middle of my array, and if at some point in time there were 1000 bullets on screen, and that number was decreased to 1 at some later point in time, then I would still be running through all 1000 bullets, checking to see if it was active, realising it wasn't, then continuing on with my code.
This could cause some major lag problems...
So, anyways, my question is, how can I assign one element of an array of classes to an entire other element of that array?[/code] |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Cervantes

|
Posted: Sat Apr 29, 2006 10:32 am Post subject: (No subject) |
|
|
Your problem might be coming from your terminology:
TheOneTrueGod wrote:
So, anyways, my question is, how can I assign one element of an array of classes to an entire other element of that array?
You don't have an array of classes. You have an array of objects.
Objects are referenced by pointers:
code: | var my_bullet : pointer to Bullet |
So, the first thing we should do when we want to remove a bullet is free that bullet.
code: | free bullet_array (element_to_be_removed) |
In your code, that looks like
(You shouldn't be naming variables with a capital letter. Capitals are for Constants or Classes or Modules. Nor should your type be "Bullet". You don't see "String" or "Int" variable types, you see "string" and "int".)
Now, the second thing we need to do is copy the last bullet object in the array to element i.
code: | bullet_array (i) := bullet_array (upper (bullet_array)) |
(Why aren't you using Flexible Arrays?)
In your code, that would be:
Now we remove the last element of the array.
code: | new bullet_array, upper (bullet_array) - 1 |
In your code, that would be:
As a final thought, you might want to use a Linked List data structure, since removing elements from the middle of them is much easier, and since you probably are never going to only need a certain bullet at a certain index.
Note: The above code is untested. Let me know if it causes problems. |
|
|
|
|
 |
wtd
|
Posted: Sat Apr 29, 2006 12:51 pm Post subject: (No subject) |
|
|
Additionally, here Bullet is not a class. It is a record. |
|
|
|
|
 |
TheOneTrueGod

|
Posted: Sat Apr 29, 2006 5:20 pm Post subject: (No subject) |
|
|
ah, so thats what I was doing wrong. Thanks for the help.
I didn't use linked lists or flexible arrays because I don't know them yet, and i'm just learning OOT (from your awesome walkthrough ).
@wtd, The bottom two sections are exerts (sp?) from a different program.
(Technically, neither of the examples provided are actually from the program I'm working on, they are just representative examples that show what i'm having problems with, so I didn't wind up posting like 300 lines of code ) |
|
|
|
|
 |
|
|