Sign in to follow this  
Protagonist95

Make it simplyer?

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)

 

Share this post


Link to post

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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
18 minutes ago, tosfera said:

 

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

At 22 line:tmpColshape is nil value. 

Share this post


Link to post

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?

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.