Jump to content

Make it simplyer?


Protagonist95

Recommended Posts

Posted

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)

 

Posted

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.

Posted
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

Posted
3 minutes ago, Protagonist95 said:

if ( theElement == getlocalPlayer() ) then

 

I used a small L there, just change it to getLocalPlayer() with a capital L and it should work.

Posted

Only way that it can be nil is when the colshape gets deleted before the onClientKey (which should be onClientClick, my bad) gets called. We're assigning the value to tmpColshape on line 5 already so it shouldn't be nil. What other code can be found in your file?

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