
-----------------------------------
TheOneTrueGod
Sat Apr 29, 2006 8:14 am

Decrementing an array of classes
-----------------------------------
Allright, first some background info:

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?

     Bullet(B(i)) := Bullet(B(BulletCount))

^^^
The above code didn't work for me, and doing something like

     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]

-----------------------------------
Cervantes
Sat Apr 29, 2006 10:32 am


-----------------------------------
Your problem might be coming from your terminology:

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:
var my_bullet : pointer to Bullet

So, the first thing we should do when we want to remove a bullet is free that bullet.
free bullet_array (element_to_be_removed)
In your code, that looks like
free B(i)
(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.
bullet_array (i) := bullet_array (upper (bullet_array))
(Why aren't you using B (i) := B(BCount)

Now we remove the last element of the array.
new bullet_array, upper (bullet_array) - 1
In your code, that would be:
BCount -= 1

As a final thought, you might want to use a Note: The above code is untested. Let me know if it causes problems.

-----------------------------------
wtd
Sat Apr 29, 2006 12:51 pm


-----------------------------------
Additionally, here Bullet is not a class.  It is a record.

-----------------------------------
TheOneTrueGod
Sat Apr 29, 2006 5:20 pm


-----------------------------------
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  :D )
