Jump to content

Attachment problem


Recommended Posts

function attach1(startedresource)
if startedresource == getThisResource() then
	A1 = createVehicle ( ID, X, Y, Z, rotX, rotY, rotZ )
	B2 = createObject ( ID, X, Y, Z, rotX, rotY, rotZ )
	C3 = attachElements ( B2, A1, 0, 0, 0 )
	D4 = createObject ( ID, X, Y, Z, rotX, rotY, rotZ )
attachElements ( D4, C3, 0, 0, 0 )	
end
end

Link to comment

I would suggest you have a table with objects. Then check if the table size is greater then 2, if it is, then print a message. You have complicated your script way too much.

local objects = { }
local selectedObjIndex = { };
 
if not objects[ playerSource ] then objects[ playerSource ] = { }; end
-- when you create new object, check its size:
if  #objects[ playerSource ] < 2 then -- we can create 1 more object
local obj = createObject( ...... ); -- you don't have to assign it to a variable but it'll make the code cleaner
local objTab = { obj, { 0,0,0, 0,0,0 } }; -- make a table with the object and a table for its offset coords, NOTE that coords table is at index 2! object element at 1!
table.insert( objects[ playerSource ], objTab );
   selectedObjIndex[ playerSource ] = #objects[ playerSource ]; -- make the new object as a selected
else
return outputChatBox( "You can only create 2 objects!!!", playerSource );
end
 
-- in your movement functions use the selectedObjIndex instead of argument passed from pressed key (bindKey)
-- this will shorten the script by all not necessary bindKey with different objects
-- something like this:
 
local selectedIndex = selectedObjIndex[ playerSource ];
if key ..... then
   objects[ playerSource ][ selectedIndex ][ 2 ][ 1 ] = objects[ playerSource ][ selectedIndex ][ 2 ][ 1 ] - precision;
-- [ 2 ] <-- will be the table of the offset coords...  [ 2 ][ 1 ] would be offsetX, [ 2 ][ 2 ] offsetY, [ 2 ][ 3 ] offsetZ, etc.
-- as noted on line 8
   setElementAttachedOffset( objects[ playerSource ][ selectedIndex ][ 1 ], ........ ); -- [ 1 ] will return the object element
end

It may look a bit complicated at first but if you'll read the comments and try to understand the code then you'll be fine.

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