Jump to content

Custom location names / districts


Dzsozi (h03)

Recommended Posts

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

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 by pa3ck
Link to comment

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 by Dzsozi
Link to comment

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

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
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 by Tails
added player check (was triggering twice)
Link to comment

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 by Dzsozi
Link to comment
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 by Tails
Link to comment
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

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 by Dzsozi
Link to comment

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 by Tails
Link to comment

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

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