kevin433 Posted February 25, 2010 Share Posted February 25, 2010 Hi, I wanted to know if there is a way to find out if the Player is facing a vehicle or object or something like that. BTW: I wanted also to ask if there are any limits in MTA. In SA:MP there are Vehicle, object and pickup limits, but are there also limits in MTA? Link to comment
lil Toady Posted February 25, 2010 Share Posted February 25, 2010 I guess isElementOnScreen would be useful for you to find out if a player is facing some element. As for the limits, there is an overall limit for the number of elements (vehicles, objects, peds, etc. all together) but i doubt you will ever reach this limit as its over 65000 elements. But there are also some other limits like number of objects in one area (cant remember the numbers) and a number of vehicle models in one area (if i remember right it was about 120 different models, but you are not limited in number of vehicles as long as the number of models does not get over this limit). So practically you are free to have as many elements as you want as long as they are spread over the map well. Link to comment
kevin433 Posted February 25, 2010 Author Share Posted February 25, 2010 (edited) 65000 Elements. I'm never going to reach that I tried "isElementOnScreen", but it gives me an error: attempt to call global 'isElementOnScreen' (a nil value) That's my code: local x,y,z = getElementPosition(player) local colsphere = createColSphere(x,y,z,5) local vehicles = getElementsWithinColShape(colsphere, "vehicle") for i,vehicles2 in ipairs(vehicles) do if(isElementOnScreen(vehicles2)) then outputChatBox(getElementID(vehicles2)) end end There's more above it in my script, but in that part is the error. I'm using a commandhandler to call the function. When I take out "if(isElementOnScreen(vehicles2)) then" everything works fine. EDIT: Found one wrong thing, I called the function "isElementOnScreen" in a Server-side file Edited February 25, 2010 by Guest Link to comment
Aibo Posted February 25, 2010 Share Posted February 25, 2010 i bet that's server side script, while isElementOnScreen is obviously a client-only function. Link to comment
kevin433 Posted February 25, 2010 Author Share Posted February 25, 2010 I made it now Client-side, but it's still not working function OpenCar() local x,y,z = getElementPosition(getLocalPlayer()) local newcolsphere = createColSphere(x,y,z,5) local vehicles = getElementsWithinColShape(newcolsphere, "vehicle") for theKey,vehicles2 in ipairs(vehicles) do if(isElementOnScreen(vehicles2)) then outputChatBox(getElementID(vehicles2)) end end end BTW: I'm still learning the generic for loop. Link to comment
Buffalo Posted February 25, 2010 Share Posted February 25, 2010 Please note that for legacy reasons, a colshape created on the client does not collide with elements already existing at that location until they first move Maybe you should retrieve this vehicles table server side, then triggerClientEvent() and send it to client side, then proceed check with loop and isElementOnScreen() Link to comment
kevin433 Posted February 25, 2010 Author Share Posted February 25, 2010 The only problem is then that I can't get the Player who triggered the event, because I triggered the "OpenCar" function with a GUI button , but a button doesn't "send" the player with it. So I would have to do this: Press Button -> triggerSeverEvent -> vehicle loop -> triggerClientEvent -> isElementOnScreen. But as I said I can't get the player who triggered the event, or can I? Link to comment
Buffalo Posted February 25, 2010 Share Posted February 25, 2010 It's always local player, which you can get with getLocalPlayer(), and you should use triggerServerEvent(string:eventName,getLocalPlayer(),args..), so event source would be local player Link to comment
kevin433 Posted February 25, 2010 Author Share Posted February 25, 2010 That's how far I came: GUI Button -> triggerServerEvent -> get elementID's near the player -> ??? I didn't came further than in the example above. Client side: -- Player click a button, then OpenCar gets triggered function OpenCar() triggerServerEvent("CVehicles", getLocalPlayer()) end Server-side: function CheckVehiclesNear() local x,y,z = getElementPosition(source) local newcolshape = createColSphere(x, y, z,5) local vehicles = getElementsWithinColShape(newcolshape, "vehicle") for theKey,vehicles2 in ipairs(vehicles) do outputChatBox(getElementID(vehicles2)) end destroyElement(newcolshape) triggerClientEvent(source,"COnScreen", getRootElement(), vehicles2) end addEvent("CVehicles", true) addEventHandler("CVehicles", getRootElement(), CheckVehiclesNear) Client-side. tried it, but it didn't work: function CarOnScreen(vehicles) for theKey,vehicles2 in ipairs(vehicles) do if(isElementOnScreen(vehicles2)) then outputChatBox(getElementID(vehicles2)) end end end addEvent("COnScreen", true) addEventHandler("COnScreen", getRootElement(), CarOnScreen) Should I maybe set the "triggerClientEvent in the loop in the Server-side script? Link to comment
lil Toady Posted February 25, 2010 Share Posted February 25, 2010 You dont need all the fancy server-client event calls, just get the closest vehicles looping through all of them (getElementsByType("vehicle")) and comparing distance between the vehicle and local player(getDistanceBetweenPoints3D), if its close, check if its on screen. Link to comment
kevin433 Posted February 25, 2010 Author Share Posted February 25, 2010 Have this now: function OpenCar() local x,y,z = getElementPosition(getLocalPlayer()) for i,vehicle in ipairs(getElementsByType("vehicle")) do local vx,vy,vz = getElementPosition(vehicle) if(getDistanceBetweenPoints3D(x,y,z,vx,vy,vz) <= 4) then if(isElementOnScreen(vehicle)) then outputChatBox(getElementID(vehicle)) end end end end It works! Problem now: I want to get the ID of the Vehicle the player is looking at, because, when I stand between 2 vehicles and both are <=4 then I get both ID's, but I just want to get the one which I'm looking at, that's why I added "isElementOnScreen", but I get both ID's when I stand between them and I just look at one of them while the other car is "behind" my camer. Link to comment
lil Toady Posted February 25, 2010 Share Posted February 25, 2010 Strange it hasnt passed the vehicles you dont see. But anyway. You could drop the loop at all and instead shoot a process line of sight in the direction the player is facing for the distance you want. local x, y, z = getElementPosition ( player ) local rot = getPedRotation ( player ) local radius = 4 local lx = x + math.sin ( math.rad ( -rot ) ) * radius local ly = y + math.cos ( math.rad ( -rot ) ) * radius local hit, hx, hy, hy, element = processLineOfSight ( x, y, z, lx, ly, z ) if ( hit and getElementType ( element ) = "vehicle" ) then -- 'element' is the vehicle you need end Link to comment
kevin433 Posted February 25, 2010 Author Share Posted February 25, 2010 Thank you very much,lil Toady! It finally works! 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