ViRuZGamiing Posted January 25, 2016 Share Posted January 25, 2016 Hi I'm trying to create a colshape to all existing vehicles but it seems to not be there. no errors, showcol and development mode are active. first time using attachElements. vehicles = getElementsByType ("vehicle") for key,vehicle in ipairs(vehicles) do local x, y, z = getElementPosition(vehicle) vehCol = createColRectangle(x, y, 5, 5) attachElements(vehCol, vehicle) end regards billy Link to comment
Army@1 Posted January 25, 2016 Share Posted January 25, 2016 Note from createColRectangle: Attaching a rectangle colshape to another element may give unexpected results as the origin is not at the rectangle centre. Try using a collision circle for attaching instead. Anyway, also try the following code: vehicles = getElementsByType ("vehicle") for key,vehicle in ipairs(vehicles) do local x, y, z = getElementPosition(vehicle) w,h = 5,5 x,y = x+w/2,y+h/2 vehCol = createColRectangle(x, y, w, h) attachElements(vehCol, vehicle) end Link to comment
ViRuZGamiing Posted January 25, 2016 Author Share Posted January 25, 2016 I think I'll go with a marker then instead. Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) attachElements(vehMarker, vehicle, 0, -3, -1) I use this to attach a marker to every vehicle (vehicle is the result from looping through all vehicle elements) I want to call the vehicle my marker is attached to so I tried using getElementAttachedTo but It didn't work. Is there any other way of calling the vehicle. Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 setTimer(function () vehicles = getElementsByType ("vehicle") for key,vehicle in ipairs(vehicles) do if not (getElementData(vehicle, "marker")) then local x, y, z = getElementPosition(vehicle) vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) attachElements(vehMarker, vehicle, 0, -3, -1) setElementData(vehicle, "marker", vehMarker) addEventHandler("onMarkerHit", vehMarker, function(hitElement) if (getElementType(hitElement) == "player") then toggleControl(hitElement, "enter_exit", false) bindKey(hitElement, "f", "down", funcName) end end) addEventHandler("onMarkerLeave", vehMarker, function(hitElement) if (getElementType(hitElement) == "player") then toggleControl(hitElement, "enter_exit", true) unbindKey(hitElement, "f", "down", funcName) end end) end end end, 6000, 0) Here's the code, Vehicle which the marker is attached to should be accessible from function 'funcName' Link to comment
KariiiM Posted January 26, 2016 Share Posted January 26, 2016 Working I tested it by myself, local vehMarker = {} setTimer(function () for index, vehicle in pairs (getElementsByType("vehicle")) do if not (getElementData(vehicle, "marker")) then local x, y, z = getElementPosition(vehicle) vehMarker[vehicle] = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) attachElements(vehMarker[vehicle], vehicle, 0, -3, -1) setElementData(vehicle, "marker", vehMarker[vehicle]) addEventHandler("onMarkerHit", vehMarker[vehicle], function(hitElement) if (getElementType(hitElement) == "player") then toggleControl(hitElement, "enter_exit", false) bindKey(hitElement, "f", "down", funcName) end end) addEventHandler("onMarkerLeave", vehMarker[vehicle], function(hitElement) if (getElementType(hitElement) == "player") then toggleControl(hitElement, "enter_exit", true) unbindKey(hitElement, "f", "down", funcName) end end) end end end, 6000, 0) Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 How would I then call it from funcName? EDIT: Because all I see is the same code as I had but then with a table being used. Link to comment
KariiiM Posted January 26, 2016 Share Posted January 26, 2016 How would I then call it from funcName? Ah Do you want to get the funcName of all this code? to put it in somewhere? Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 when I enter a marker I need to know the vehicle attached to it and use it inside funcName as argument passed through the bindKey. Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 What I eventually try to do is passing the 'vehicle' onto the 'bindFunc' function. setTimer(function () for index, vehicle in pairs (getElementsByType("vehicle")) do if not (getElementData(vehicle, "marker")) then local x, y, z = getElementPosition(vehicle) vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) attachElements(vehMarker, vehicle, 0, -3, -1) setElementData(vehicle, "marker", vehMarker) addEventHandler("onMarkerHit", vehMarker, bindFunc) addEventHandler("onMarkerLeave", vehMarker, unbindFunc) end end end, 6000, 0) Link to comment
KariiiM Posted January 26, 2016 Share Posted January 26, 2016 Use this function to check if the element "marker" is attached or else if (isElementAttached(vehMarker)) then toggleControl(hitElement, "enter_exit", false) bindKey(hitElement, "f", "down", funcName) end and get the funcName from bind like that, if that what you means function funcName() --[[your code.]] end full code setTimer(function () vehicles = getElementsByType ("vehicle") for key,vehicle in ipairs(vehicles) do if not (getElementData(vehicle, "marker")) then local x, y, z = getElementPosition(vehicle) vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) attachElements(vehMarker, vehicle, 0, -3, -1) setElementData(vehicle, "marker", vehMarker) addEventHandler("onMarkerHit", vehMarker, function(hitElement) if (getElementType(hitElement) == "player") then if (isElementAttached(vehMarker)) then toggleControl(hitElement, "enter_exit", false) bindKey(hitElement, "f", "down", funcName) end end end) addEventHandler("onMarkerLeave", vehMarker, function(hitElement) if (getElementType(hitElement) == "player") then if (isElementAttached(vehMarker)) then toggleControl(hitElement, "enter_exit", true) unbindKey(hitElement, "f", "down", funcName) end end end) end end end, 6000, 0) function funcName() --[[your code.]] end Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 You don't seem to get it, I am sorry to be rude but it seems you don't understand what I try to do. I want use (look at the bold text); for key,vehicle in ipairs(vehicles) do to be used inside 'bindFunc' from my last example. addEventHandler("onMarkerHit", vehMarker, bindFunc) So I can do this function bindFunc () use vehicle here end Link to comment
Army@1 Posted January 26, 2016 Share Posted January 26, 2016 You can simply do bindKey(hitElement, "f", "down", funcName, vehicle) by adding an argument, for vehicle, in funcName. if I understand you correctly. Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 Yeah but the bindkey is not in the scope of the vehicle Link to comment
ViRuZGamiing Posted January 26, 2016 Author Share Posted January 26, 2016 function bindFunc(hitElement) if (getElementType(hitElement) == "player") then toggleControl(hitElement, "enter_exit", false) bindKey(hitElement, "f", "down", funcName) end end setTimer(function () for index, vehicle in pairs (getElementsByType("vehicle")) do if not (getElementData(vehicle, "marker")) then local x, y, z = getElementPosition(vehicle) vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) attachElements(vehMarker, vehicle, 0, -3, -1) setElementData(vehicle, "marker", vehMarker) addEventHandler("onMarkerHit", vehMarker, bindFunc) -- I should need a way to pass the argument with this handler end end end, 6000, 0) Can I do this? addEventHandler("onMarkerHit", vehMarker, bindFunc(vehicle)) Link to comment
tosfera Posted January 27, 2016 Share Posted January 27, 2016 function bindFunc(hitElement) if (getElementType(hitElement) == "player") then toggleControl(hitElement, "enter_exit", false) bindKey(hitElement, "f", "down", funcName) end end setTimer(function () for index, vehicle in pairs (getElementsByType("vehicle")) do if not (getElementData(vehicle, "marker")) then local x, y, z = getElementPosition(vehicle) vehMarker = createMarker(x, y, z, "cylinder", 2.0, 255, 0, 0, 255) attachElements(vehMarker, vehicle, 0, -3, -1) setElementData(vehicle, "marker", vehMarker) addEventHandler("onMarkerHit", vehMarker, bindFunc) -- I should need a way to pass the argument with this handler end end end, 6000, 0) Can I do this? addEventHandler("onMarkerHit", vehMarker, bindFunc(vehicle)) Sadly enough, you can't just randomly parse your parameters in your eventHandler, this has to be done with the function call itself. However, since you do want that vehicle to be in that function.. you can try to actually gather the data which you've already set in the marker's hit and see if there is an actual vehicle linked to that. function bindFunc ( hitElement ) if ( getElementType ( hitElement ) == "player" ) then local linkedVehicle = getElementData ( source, "marker" ); if ( isElement ( linkedVehicle ) ) then toggleControl ( hitElement, "enter_exit", false ); bindKey ( hitElement, "f", "down", funcName ); end end end setTimer ( function () for index, vehicle in pairs ( getElementsByType ( "vehicle" ) ) do if not ( getElementData ( vehicle, "marker" ) ) then local x, y, z = getElementPosition ( vehicle ); local vehMarker = createMarker ( x, y, z, "cylinder", 2.0, 255, 0, 0, 255 ); attachElements ( vehMarker, vehicle, 0, -3, -1 ); setElementData ( vehicle, "marker", vehMarker ); addEventHandler ( "onMarkerHit", vehMarker, bindFunc ); end end end, 6000, 0 ); Another thing that I would strongly suggest is keeping some kind of ID running on your vehicles so it's easier to obtain a vehicle as an element. By doing that you only have to save the vehicleId on the marker his butt and obtain the linked vehicle to it. If a vehicle currently explodes, the marker is still there. I do wonder, what are you trying to do? Maybe there is another way that could solve your problem. Link to comment
ViRuZGamiing Posted January 27, 2016 Author Share Posted January 27, 2016 We'll I'm try to open the trunk of a vehicle by pressing F so when a player is standing behind the vehicle the enter gets toggled and a new function get's binded. I am planning on making so that a vehicle has a owner and an ID so I could use those but I was trying to make this work as a standalone first. Link to comment
tosfera Posted January 27, 2016 Share Posted January 27, 2016 First assign an ID to them, after that you can try to use bindKey to bind your key, createColSphere to create a small sphere once a player hits the button. After that use getElementsWithinColShape to get the vehicles around him/her. If there are vehicles in his area, check the elementData to see if the vehicle is owned by him or locked ( not sure how you want to protect it ) and get the closest vehicle too. Once you've done that, get the elementBoundingBox and see if the user is at the end of the vehicle ( get the distance from the biggest value from the vehicle and the position from the player ). Then open it. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now