Jump to content

setVehicleComponentPosition is returning unexpected results


tosfera

Recommended Posts

I'm currently working on a tuning store for my latest project, but the setVehicleComponentPosition is behaving... weird in my opinion. This is the case:

I've got a match, a match takes 5 minutes. At the start of a match, every player will be spawned in their vehicle including upgrades and modifications regarding their components. The component data is being saved as vehicle:tune:height. This can be any value between -0.15 and 0.15(not important, it's a numeric value and that's what counts). As soon as the data of the vehicle changes, my onClientElementDataChange is being called. The trigger looks like this;

addEventHandler ( "onClientElementDataChange", getRootElement(), 
    function ( dataName ) 
        if ( getElementType ( source ) == "vehicle" and dataName == "vehicle:tune:height" ) then 
            for k in pairs ( getVehicleComponents ( source ) ) do 
                resetVehicleComponentPosition ( source, k ); 
            end 
             
            if ( getElementData ( source, dataName ) ~= 0 ) then 
                for k in pairs ( getVehicleComponents ( source ) ) do 
                    if ( k ~= "exhaust_ok" ) then 
                        local x, y, z = getVehicleComponentPosition ( source, k ); 
                        setVehicleComponentPosition ( source, k, x, y, z + getElementData ( source, dataName ) ); 
                    end 
                end 
            end 
        end 
    end  
); 

As you can see, the first thing I'm doing is resetting the actual position of every component. Even the exhaust which I didn't change ( not important ). After that, if the new data isn't 0.. I'm increasing the Z-axis of all the components.

The strange thing is, I'm in the server with 1 other guy ( also when I'm alone ) but it doubles my elementData in the client side. Don't get me wrong at this part, whenever I do; getElementData ( source, dataName ), it returns the right value. But the components of my vehicle are affected twice. Whenever I check the position of the components, they moved down twice.

As an example; if I got the value ( on my elementData ) of -0.15, it lowers down all the components by -0.30.

I've been trying to resolve the problem for at least 1 hours now but I can't seem to get it right. Any help will be greatly appreciated!

Link to comment
  • Moderators

1: How many times does this function gets called?

2: What is the position after the reset?

3: And what is the position before you affect any of the components?

4: Does the function getVehicleComponentPosition moves the component down? Or the result is, moved down?

Debug the results.

After what you told me, I would say resetVehicleComponentPosition doesn't reset everything well.

But if that is the case, make sure you backup the original position. (or save it)

local modelComponentPosition = {} 

local model = getElementModel(source) 
local bufferModelComponentData = modelComponentPosition[model] 
if not bufferModelComponentData then 
    modelComponentPosition[model]= {} 
end 
for k in pairs ( getVehicleComponents ( source ) ) do 
    if ( k ~= "exhaust_ok" ) then 
        local x, y, z =  bufferModelComponentData  and table.unpack(bufferModelComponentData[k]) or getVehicleComponentPosition ( source, k ) 
        setVehicleComponentPosition ( source, k, x, y, z) 
        if not bufferModelComponentData then 
            modelComponentPosition[model][k] = {x, y, z} 
        end 
    end 
end 

But maybe you can better generate a xml document with original positions, then you won't have trouble with bugs. :idea:

Link to comment

I've been roaming with the script in the past night, couldn't get sleep anyway. The function is being called once, the position after the reset was twice my actual number, the position before the piece of code was unedited. So the vehicle was normal, not edited. The function is moving them down. ( all the answers regarding my code in the topic )

Currently, I've got what you told me to. Just made it myself a few hours ago. Strange thing now.. it's only moving my components up, even if I have a negative number. Also, some of the components were not saved in my table and didn't move down. But that's okay, just need to figure out why the hell it keeps going up.

Thanks anyway!

Link to comment

I've saved the default positions in a table, including their names to make it easier to get the right positions. Firing the function once, lowers down only the half of the vehicle. Firing it again, will make the vehicle's component reset. Even though the positions should be lowered with the amount depending on the default locations.

This is seriously, one of the functions in MTA that blows my mind. -__-

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...