Any way to delete GOBs?

10 views
Skip to first unread message

Phenom

unread,
Jul 27, 2009, 6:16:14 PM7/27/09
to gmp-engine
Hey there,

Currently coding a game for the Palm's webOS platform.
I have enemies popping up and I'd like them to reload for each level.
But, of course, GOBs must each have a unique identifier
so I can't reuse the same variables in my code to populate the world.

Anyone know of a way to delete GOBs once they're no longer needed?
Instead of just turning them off?

Trevor Cowley

unread,
Jul 27, 2009, 11:49:23 PM7/27/09
to gmp-engine
Good question!

It's my oversight that there isn't a delete method for Gobs. I'll make
sure to add the method to the engine code.

In the interim, you can delete Gobs using these three steps:

1) First, if you explicitly added events to your Gob's tag, then
remove them. (You probably didn't, but just in case, here's the
instructions:)

// this example uses 'onclick'
// skip this step if you didn't add an 'onclick','onmousemove',
etc.
G.O['gobID'].tag.onclick = null;

1) Second, delete the Gob's HTML tag from the DOM.

// 'parentNode' is a built-in DOM attribute of every HTML tag
// and 'removeChild(node)' is a built-in DOM method
// lets put them together to remove the tag from its parent:
var gob = G.O['gobID'];
gob.tag.parentNode.removeChild(gob.tag);

2) Third, delete the Gob's javascript object from any sets of Gobs
that reference it. Once all references are deleted, the browser's
javascript engine will do garbage collection and recover the memory
used by the Gob.

// G.O is the global Gob set, and this reference
// definitely needs to be deleted. To delete the reference,
// we use the delete keyword.
delete G.O['gobID'];

// did you manually make a reference in a Block's Gob set?
// If so, then delete that/those references too.
delete G.B['someBlock'].O['gobID'];

That's it! You probably only need to do the second step in most cases,
but the other steps may crop up.

In the next few days, I'll be adding code to the engine that does this
same work, and then testing it and releasing it. I'll post something
here as well!

As an aside, I think overlooked the delete method because of the way I
code my games. I tend to create one big master set of Gobs, and just
turn them off and on as I need them. But it's definitely an oversight
not to have a good delete method, so I'll be adding it asap!

Cheers,

Trev

Trevor Cowley

unread,
Jul 28, 2009, 12:02:34 AM7/28/09
to gmp-engine
Hi Ray,

I forgot to include one small, but significant detail.

The above recipe applies only to Gobs without children Gobs inside
them.

If you do have nested Gobs that you are deleting, then you need to
apply the above steps their children first.

Here's a few guidelines for setting up easily deletable Gobs:

1) Don't apply events to them directly. (no onclick, onmousemove, etc
attached to their tags)

2) Don't nest your 'sprite' Gobs (if you are deleting them). Use nice
innerHTML or an IMG sprite to get the complexity you want.

Hope this helps!

Trev

On Jul 27, 3:16 pm, Phenom <phenom0...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages