Jump to content

How to check whether vehicles are underwater? (then destroy)


chiefwhosm

Recommended Posts

Hi,

I've been converting my TankJack sa-mp gamemode over to MTA. For the most part it's gone fairly smoothly, however there's one bug that I can't seem to get round. If the tank goes into the sea, it doesn't auto-destroy as it would in SA. So what I need is a constant check to see if the vehicle is underwater or not, and if it is to blow up the vehicle (or just end the round, which will in turn destroy the vehicle anyway).

All the functions I've tried have pretty much worked like this (ive deleted parts of my code so often this is the best example of what I'd been doing that I can give):

function destroyTank (vehicle)

if then

blowVehicle (van)

else

end

end

The commands i've tried are:

isVehicleOnGround, I thought if false, then it would count sea as not being ground. However now on reflection, I wonder if, had it worked, whether anyone driving off a small cliff to the ground below wou;d have instantly exploded :lol:

isPlayerUnderWater, I thought that if the vanPlayer were checked, and found to be underwater then naturally if it then says to blow up the tank then it would destroy the tank. This never happened. One thing that did work was if I set it up as a command, and ran the command, then it would explode, making me wonder if my eventHandlers arent being called. Anyway, one thing I did learn was that though I was drowning in my vehicle underwater, unless I exited the vehicle I wasn't considered as being underwater.

isPlayerDead, well I thought if vanPlayer is dead then obviously he's been damaged in someway while inside the vehicle (such as drowning), but this once again didn't do a thing.

I've been at it for hours on this one, so maybe I'm missing something really obvious. In regards to addEventHandlers, the way I read it from the MTA wiki, as long as it has 'root' in the handler, then it could be continually processed throughout the course of the game per tick, is that correct? Or do I need a call somewhere else?

Thanks :)

Link to comment

Okay, as to event handlers.

On the wiki it says that if you have an event handler (eg):

addEventHandler( "onSpawnpointUse", rootElement, onSpawnpointUseHandler )

Often, this can be the root element (meaning the handler will be called when the event is triggered for any element).

Now do I read that correctly that any event handler with 'root' in it, is always called every tick, that the code is always checking the functions with root in the handler to see if they need anything done with them.

Link to comment

No, that is incorrect, assuming I understand you.

First you should understand how events work:

Each event is triggered with a 'source' element. This is the element that caused the event to happen. For example, onPlayerEnterVehicle has a source of the player that triggered it.

Now, the first thing it does is any event handlers for 'onPlayerEnterVehicle' attached to the source element (the player in this case) get triggered.

Next, we go to the player's child elements, their children and their children (etc...) triggering the same event - the 'source' variable remains the same (the player) throughout this. The 'this' variable changes though.

Next we do the same thing with the player's parent element, it's parents and their parents (etc...), right up to the root element.

As you should be able to see, every event will eventually get back to the root element. So, attaching an onPlayerEnterVehicle handler to the root element will have it triggered for every player entering any vehicle.

You can see a visual view of your server's elements by starting the 'elementbrowser' and viewing your server's web interface.

For example, your element tree might look like:

  
root 
    myresource 
        map 
            vehicle1 
            vehicle2 
            container 
                vehicle3 
                vehicle4 
  

So, you can attach your event handler to any element in the tree. If you were using a vehicle based event, say onVehicleExplode, you can get 6 different effects:

- Attach it to the 'map' element (or myresource or root, same effect): Get every vehicle's explode event.

- Attach it to the 'vehicle1', 'vehicle2', 'vehicle3' or 'vehicle4' element: Just see when that one specific vehicle explodes

- Attach it to the 'container' element (you can create this using createElement): Just see when vehicle3 or vehicle4 explode.

You should also be able to see that triggering an event on the root element can be a Bad Thing. It will trickle down to every element on the server, including those in other resources. You may not want this, and it will cause more effort for the server with large maps/many resources.

You can also see that you can attach event handlers to your resource's root element to only receive notifications for events that occur inside your resources.

If you want to do something every 'frame'/tick, you need to use onClientRender - a client side event. Server side has no equivalent and you really shouldn't require it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...