Jump to content

Make it simplyer?


Protagonist95

Recommended Posts

Is it possible to make this code simplyer?

function onClientColHit(theElement, matchingDimension)
	local col = source
	if ( theElement == getLocalPlayer()) then -- if player hit colshape
		addEventHandler( "onClientKey", getRootElement(), function(button,press)
			if (button == "mouse3") then
				setElementData(getLocalPlayer(),"item",getElementData(getElementAttachedTo(col),"item")) -- set elementData of and object in colshape
			end
			end)
	end
end
addEventHandler("onClientColShapeHit",getRootElement(),onClientColHit)

 

Link to comment

There is a way to make it cleaner, that is for sure. Try something like this;

local tmpColshape = nil;
addEventHandler ( "onClientColShapeHit", getRootElement(),
    function ( theElement )
        if ( theElement == getlocalPlayer() ) then
            tmpColshape = source;
            addEventHandler ( "onClientKey", getRootElement(), setItemData );
        end
    end
);

addEventHandler ( "onClientColShapeLeave", getRootElement(),
    function ( theElement )
        if ( theElement == getlocalPlayer() ) then
            tmpColshape = nil;
            removeEventHandler ( "onClientKey", getRootElement(), setItemData );
        end
    end
);

function setItemData ( button, pressed )
    if ( button == "mouse3" and pressed ) then
        local tmpObject = getElementAttachtedTo ( tmpColshape );
        setElementData ( getLocalPlayer(), "item", getElementData ( tmpObject, "item" ) );
        removeEventHandler ( "onClientKey", getRootElement(), setItemData );
    end
end

Maybe it's also smart to see if the colshape has an object attached to it in the onClientColShapeHit already. To avoid warnings and such.

Link to comment
39 minutes ago, tosfera said:

There is a way to make it cleaner, that is for sure. Try something like this;


local tmpColshape = nil;
addEventHandler ( "onClientColShapeHit", getRootElement(),
    function ( theElement )
        if ( theElement == getlocalPlayer() ) then
            tmpColshape = source;
            addEventHandler ( "onClientKey", getRootElement(), setItemData );
        end
    end
);

addEventHandler ( "onClientColShapeLeave", getRootElement(),
    function ( theElement )
        if ( theElement == getlocalPlayer() ) then
            tmpColshape = nil;
            removeEventHandler ( "onClientKey", getRootElement(), setItemData );
        end
    end
);

function setItemData ( button, pressed )
    if ( button == "mouse3" and pressed ) then
        local tmpObject = getElementAttachtedTo ( tmpColshape );
        setElementData ( getLocalPlayer(), "item", getElementData ( tmpObject, "item" ) );
        removeEventHandler ( "onClientKey", getRootElement(), setItemData );
    end
end

Maybe it's also smart to see if the colshape has an object attached to it in the onClientColShapeHit already. To avoid warnings and such.

Not working.Error in getLocalPlayer lines -- attempt to call global

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