Dzsozi (h03) Posted November 15, 2016 Share Posted November 15, 2016 Hello! I am trying to do custom district names for a custom map, but I can't figure it out why is it not working and how to solve the problem. I would like it to work like the default zones, I would like to make a function for that to get the zone name of an element, so maybe a function like getDistrictName(element), like the default getZoneName() function. But when I am trying to check if it is working when I hit a col shape then the chatbox doesn't output anything. I am also having an error with the table.insert function, debugscript says bad argument #2 to 'insert' (number expected, got table), but when I add the k (index value) letter it is not working either. How can I do a function like the default getZoneName of an element, but with custom zones and zone names? I'm not so familiar with tables, I always fail when I try to do something with tables. Here's my script: local colShape local districtName = "" local district = {} local districts = { -- x, y, width, height, name { 164.4404296875, -341.79296875, 1500, 2750, "Vice Point", 255, 225, 150 }, { -87.556640625, -941.6962890625, 1875, 900, "Washington Beach", 0, 180, 0 }, { -287.2763671875, -1803.3896484375, 2175, 1300, "Ocean Beach", 255, 0, 0 }, { -1200.1083984375, -1803.3896484375, 1375, 1355, "Vice Port", 255, 255, 0 }, { -2082.09765625, -1803.3896484375, 1325, 2675, "Escobar International Airport", 0, 255, 255 }, { -1199, -900.8515625, 665, 1000, "Little Havana", 100, 255, 100 }, { -1199, -235, 900, 975, "Little Haiti", 255, 255, 255 }, { -1337.470703125, -20, 207, 400, "City Scrap", 200, 0, 100 }, { -1575, 415, 2075, 2000, "Downtown", 255, 180, 0 }, { -755, -725.953125, 1000, 735, "Starfish Island", 200, 0, 100 }, { -190, -240, 530, 1560, "Leaf Links", 0, 0, 255 }, { -190, 798.7958984375, 530, 700, "Prawn Island", 0, 0, 0 }, } function createDistricts() for k, v in ipairs(districts) do createRadarArea(v[1], v[2], v[3], v[4], v[6], v[7], v[8], 75) table.insert(district, {createColCuboid(v[1], v[2], -50, v[3], v[4], 1000)}, {v[5]}) end end createDistricts() function checkDistrict() for k, v in ipairs(district) do colShape = v[1] districtName = v[2] end end addEventHandler("onClientRender", root, checkDistrict) function outputDistrictName() outputChatBox("" .. districtName .. ".") end addEventHandler("onClientColShapeHit", colShape, outputDistrictName) Link to comment
pa3ck Posted November 15, 2016 Share Posted November 15, 2016 (edited) How would the code know the districtName? Where do you define and change the current districtName? EDIT: Oh yea, I see what you are trying to do. The thing is, the onClientRender function would never know which district the player is in. Try this: http://pastebin.com/k4VkAja0 (Can't paste LUA code for some reason, it just keeps loading) Also, change the table.insert to this: table.insert(district, {createColCuboid(v[1], v[2], -50, v[3], v[4], 1000)}) Edited November 15, 2016 by pa3ck Link to comment
Dzsozi (h03) Posted November 15, 2016 Author Share Posted November 15, 2016 (edited) I changed addEventHandler("onClientColShapeHit", colShape, outputDistrictName) to addEventHandler("onClientColShapeHit", root, outputDistrictName) as well, since colShape variable got removed and I got errors about it being nil, but I also get errors with root. 'attempt to index field '?' (a nil value) What should I change it to? EDIT: 'attempt to index field '?' (a nil value) refers to --> outputChatBox("" .. districts[lastDistrictKey][5] .. ".") Edited November 15, 2016 by Dzsozi Link to comment
pa3ck Posted November 15, 2016 Share Posted November 15, 2016 Add something like this so it will not try to index the filed unless it finds something: function outputDistrictName() lastDistrictKey = getCurrentDistrictKey(source) if lastDistrictKey then outputChatBox("" .. districts[lastDistrictKey][5] .. ".") end end Then enter a colshape. Link to comment
Dzsozi (h03) Posted November 15, 2016 Author Share Posted November 15, 2016 Nothing happens, I don't get any output. Link to comment
pa3ck Posted November 16, 2016 Share Posted November 16, 2016 Post the whole code again, with the changes you made. Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 local districtName = "" local district = {} local districts = { -- x, y, width, height, name { 164.4404296875, -341.79296875, 1500, 2750, "Vice Point", 255, 225, 150 }, { -87.556640625, -941.6962890625, 1875, 900, "Washington Beach", 0, 180, 0 }, { -287.2763671875, -1803.3896484375, 2175, 1300, "Ocean Beach", 255, 0, 0 }, { -1200.1083984375, -1803.3896484375, 1375, 1355, "Vice Port", 255, 255, 0 }, { -2082.09765625, -1803.3896484375, 1325, 2675, "Escobar International Airport", 0, 255, 255 }, { -1199, -900.8515625, 665, 1000, "Little Havana", 100, 255, 100 }, { -1199, -235, 900, 975, "Little Haiti", 255, 255, 255 }, { -1337.470703125, -20, 207, 400, "City Scrap", 200, 0, 100 }, { -1575, 415, 2075, 2000, "Downtown", 255, 180, 0 }, { -755, -725.953125, 1000, 735, "Starfish Island", 200, 0, 100 }, { -190, -240, 530, 1560, "Leaf Links", 0, 0, 255 }, { -190, 798.7958984375, 530, 700, "Prawn Island", 0, 0, 0 }, } function createDistricts() for k, v in ipairs(districts) do createRadarArea(v[1], v[2], v[3], v[4], v[6], v[7], v[8], 75) table.insert(district, {createColCuboid(v[1], v[2], -50, v[3], v[4], 1000)}) end end createDistricts() local lastDistrictKey function getCurrentDistrictKey(element) for k, v in ipairs(district) do if v == element then return k end end return false end function outputDistrictName() lastDistrictKey = getCurrentDistrictKey(source) if lastDistrictKey then outputChatBox("" .. districts[lastDistrictKey][5] .. ".") end end addEventHandler("onClientColShapeHit", root, outputDistrictName) Link to comment
Tails Posted November 16, 2016 Share Posted November 16, 2016 What about this function outputDistrictName() for i=1,#district do if district[i] == source then outputChatBox("Welcome to "..districts[i][2]) end end end addEventHandler("onClientColShapeHit", root, outputDistrictName) Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 Still nothing happens, and I don't get any errors in the debugscript. Link to comment
Tails Posted November 16, 2016 Share Posted November 16, 2016 (edited) function outputDistrictName(hitElem) if hitElem == localPlayer then for i=1,#district do if district[i][1] == source then outputChatBox("Welcome to "..districts[i][5]) end end end end addEventHandler("onClientColShapeHit", resourceRoot, outputDistrictName) Tested and works now. Edited November 16, 2016 by Tails added player check (was triggering twice) Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 It works now, I just have to fix some of the colshapes' sizes now, thank you so much for your help guys! 1 Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 (edited) I would like to ask just one more thing. How can I get the district name of a specified element? So I can get that from other resources with an export function, for example display it on the radar or get the district name of a vehicle for a mission. I guess somehow with isElementWithinColShape, but I am not sure how exactly. Edited November 16, 2016 by Dzsozi Link to comment
LoPollo Posted November 16, 2016 Share Posted November 16, 2016 (edited) 2 hours ago, Dzsozi said: I guess somehow with isElementWithinColShape get the element coords, iterate all the colpshapes and check if it's inside Edited November 16, 2016 by LoPollo Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 Why do I have to do that manually and also, why do I have to get the element coords? I can use isElementWithinColShape then. Link to comment
LoPollo Posted November 16, 2016 Share Posted November 16, 2016 (edited) You are right... it acceps elements and not coord... i was thinking a way without the function and used the "logic" with the isElWithinCol that already do a part of the job Edited November 16, 2016 by LoPollo Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 So how I could make an export function for getting the current district/zone name of an element I would like to get the zone name of? Link to comment
Tails Posted November 16, 2016 Share Posted November 16, 2016 (edited) 12 minutes ago, Dzsozi said: So how I could make an export function for getting the current district/zone name of an element I would like to get the zone name of? function getPlayerDistrict(player) for i=1,#district do if isElementWithinColshape(player,district[i][1]) then return districts[i][5] end end end then <export function="getPlayerDistrict" type="client"/> use it in a different script: local district = exports.resourceName:getPlayerDistrict(localPlayer) Edited November 16, 2016 by Tails Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 23 minutes ago, Tails said: function getPlayerDistrict(player) for i=1,#district do if isElementWithinColshape(player,district[i][1]) then return districts[i][5] end end end then <export function="getPlayerDistrict" type="client"/> use it in a different script: local district = exports.resourceName:getPlayerDistrict(localPlayer) Thank you very much for your help! Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 (edited) I would like to ask for help with one more thing... I was trying to do an event which detects if the district of the element was changed, so I could output the change in the chatbox from other resources or make a dxDrawText with fading when a player goes from Vice Point to Downtown for example, like the default zone name hud does, but I can't manage to get it to work, and I don't get any errors in the debugscript. Is this even possible? I was trying with something like this function getDistrictName(element) if isElement(element) then for i=1,#district do if isElementWithinColShape(element,district[i][1]) then return districts[i][5] end end return "" end end addEvent("onDistrictChange", true) function districtChangeEvent() for i=1,#district do if district[i] == source then triggerEvent("onDistrictChange", root) end end end addEventHandler("onClientColShapeHit", root, districtChangeEvent) ---------------------------- -- and in another resource ---------------------------- function test() outputChatBox(tostring(exports.vice_core:getDistrictName(getLocalPlayer()))) end addEventHandler("onDistrictChange", root, test) I don't know if I done it right or not, probably not if it is not working. Edited November 16, 2016 by Dzsozi Link to comment
Tails Posted November 16, 2016 Share Posted November 16, 2016 (edited) Yes, you got the right idea. You just need to pass the district name to the event and preferably the player, either as root or as an argument. triggerEvent("onDistrictChange", source, districts[i][5]) function test(districtName) outputChatBox(getPlayerName(source).." entered "..districtName) end addEventHandler("onDistrictChange", root, test) Also make sure to export it as well <export function="onDistrictChange" type="client"/> Note that this is all client-side, the event is only triggered by the localPlayer and the test func is only being outputted to the localPlayer. You can make it trigger a server event in the test func though. Or you could create an event on the server. Edited November 16, 2016 by Tails Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 I'm not sure what am I missing or what I did wrong, but I don't get any output. Here are the export functions: addEvent("onDistrictChange", true) function districtChangeEvent(hitElement) for i=1,#district do if district[i] == source then triggerEvent("onDistrictChange", hitElement, source) end end end addEventHandler("onClientColShapeHit", root, districtChangeEvent) --addEventHandler("onDistrictChange", root, districtChangeEvent) The meta file: <script src="zoneC.lua" type="client" /> <export function="getDistrictName" type="client"/> <export function="onDistrictChange" type="client"/> and the other resource with the test function: function test(districtName) outputChatBox(getPlayerName(source).." entered "..districtName) end addEventHandler("onDistrictChange", root, test) I changed the source to hitElement and districts[5] to source, since the source of the onClientColShapeHit event is the colshape and the element is the first argument. I don't know if it is right, I even tried with your version but it was not working either. What could be the problem? Link to comment
pa3ck Posted November 16, 2016 Share Posted November 16, 2016 You are passing source in the triggerEvent, you should pass the district name. Change source to this: districts[i][5] Link to comment
Dzsozi (h03) Posted November 16, 2016 Author Share Posted November 16, 2016 It's still not working. Link to comment
Tails Posted November 17, 2016 Share Posted November 17, 2016 (edited) Change to resourceRoot, player as argument, and renamed test to test2. Might be that test is reserved as a function name. I know you can't use "test" in a commandHandler. triggerEvent("onDistrictChange", resourceRoot, hitElement, districts[i][5]) function test2(player, districtName) outputChatBox(getPlayerName(player).." entered "..districtName) end addEventHandler("onDistrictChange", resourceRoot, test2) Edited November 17, 2016 by Tails Link to comment
Dzsozi (h03) Posted November 17, 2016 Author Share Posted November 17, 2016 It is still not working for some reason. 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