Game Development Community

Issue with using onCollision Event and reading the %srcObj.name

by Colin Richardson · in Torque Game Builder · 11/13/2006 (4:53 am) · 9 replies

Hello,

I'm still playing around with the Platform Tutorial and i go 3 steps ahead and 1 step behind (which i don't mind as that's called learning) so here i am with my latest step back.

This is my basic code to check to see if our player has picked up one of the items on his travels.

function items::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
   if(%dstObj.class $= "playerClass")
   {
     echo("Let's despawn this class: " @ %srcObj.class);
     echo("Let's despawn this name: " @ %srcObj.name);
     %srcObj.safeDelete();
   }
}

I've placed directly onto the level a couple of items and want when he travels over them for them to remove themselves (which works a treat).

However i wish to read the name of the object as that tells me which exact item he's picked up and thus respond to that (ie more speed etc).

But the

echo("Let's despawn this name: " @ %srcObj.name);

Produces just "Let's despawn this name" in the Console whereas the "Let's despawn this class" works fine.

What am i doing wrong? I have made sure on the Level Designer that this object does have a name 'MuppetTest' but it's not sending that.

Thanks in advance.
Colin

#1
11/13/2006 (8:32 am)
Try using the getName() method instead

echo("Let's despawn this name: " @ %srcObj.getName() );
#2
11/13/2006 (8:43 am)
If you're sure your object has the name MuppetTest then that is not the object this collision was called on. Try instead printing %srcObj.getId() and then take that id and run a dump() on it. See which object it is.
#3
11/13/2006 (8:54 am)
@Gary

Cheers again, that works a treat!

@Ben

Thanks also for your reply. The %srcObj.getID returns a number (such as 2813 last time i tried) and it is that item that the collision was called on as it's the only one that i've tested with. But looking at Garys response i can see the correct command to use.

However now that i understand how the dump() command works i'll try using that rather than ask these questions again.

Thanks again to you both :)
#4
11/13/2006 (4:36 pm)
Don't be afraid to ask questions if you get stuck! Glad you got it working. Gary posted before I could sit at my computer long enough to type a response, had I seen his I wouldn't have even posted. Can't believe I missed the .name vs. .getName(). Gary's is definitely the "right" solution.
#5
11/13/2006 (4:46 pm)
Hi Ben,

Yes Gary's solution did work, but your answer also helped me understand the dump() command finally.

I do ask questions when i stuck but there's nothing better than thinking 'Right i'll give this one more go before i give up and ask'. I've been in that position a further 2 times today but on both i've got it working on my own which even though i'm a developer with 17 years commercial experience, i get a great buzz from this :)

I doubt i've written the best code but i'm not bothered at the moment, it's just amazing how much you can do in TGB so easily.

But here is one question then for you.

What's the best way to call an update on (say) a Timer Bar.

I have a bar that i show on the bottom of the screen to show how much air the player has left. It slowly decreases at the moment and once it's gone, oh dear so's the player.

But i'm using this code:

///TimerBar is the name of the Timer Bar on the GUI

function decreaseTimer(%this)
{
   ///Ok let's reduce this by 0.1 of a second...
   TimerBar.value = TimerBar.value -0.1;
   if (TimerBar.value > 0)
   {
       ///And now redraw the Bar nice and smoothly
       TimerBar.extent = ((TimerBar.widthPerSecond * TimerBar.value)) SPC getWord(TimerBar.extent,1);
       schedule(100,0,"decreaseTimer");
   } else
   {
      ///Oh dear...
      TimerBar.visible = false;
      TimerBar.timerStarted = false;
      $pGuy.Dead();
   }   
}

Is there a better way as i just get the idea that Schedule isn't the best way to do this, is there a timer object/control? Or just a tidier way of doing this as i need to call it every 1/10th of a second to get a nice scrolling bar (i could do with calling it even quicker)

Cheers, off to bed i go, gotta be at my desk in 8 hours!
#6
11/13/2006 (6:08 pm)
I don't remember the timer syntax but yes, you can register an object to receive ::onTimerUpdate callbacks. Do some searches at tdn and forums for onTimerUpdate and you should find some good stuff.
#7
11/13/2006 (11:00 pm)
Ah, just remembered where I learned about using timers. Check out the simple car physics mini-tutorial on TDN. The car physics run with a timer callback every 25 ms. You'll have to open up the script files to find it.
#8
11/18/2006 (6:12 pm)
You can call %this.setTimerOn(%time) in the timer GUI's onLevelLoaded callback and then you'll get an onTimer callback. You should still manually decrement the time based on elapsed scene time since last timer update. There are a couple ways to get the current time, one is %this.scenegraph.getSceneTime().

Edit: Spelling.
#9
11/21/2006 (11:38 am)
@ Ben & Thomas

Thanks Chaps.

I've not had time (been away working) to check this all out but now that i'm back at my PC i'll do that tomorrow.

Cheers
Colin