Jump to content

insert usedata to table


Recommended Posts

  • Scripting Moderators
1 hour ago, NoviceWithManyProblems said:

spacer.png

You didn't passed table.

I prefer adding elements manually.

local someTable = {}
local somePed = createPed(11, 0, 0, 3)

-- 1st way, ped will be accessible via number, mostly you will need to loop whole table if there's more elements

someTable[#someTable + 1] = somePed
print(tostring(someTable[1])) -- output: ped userdata

-- 2nd way, ped will be accessible via reference

someTable[somePed] = somePed
print(tostring(someTable[somePed])) -- output: ped userdata

 

Edited by majqq
  • Like 1
  • Thanks 1
Link to comment

 

  ok. thx, but look at this:

local peds = {
  {...},
  {...}
}

for k,v in ipairs(peds) do
  createPed(...)
  setElementData(v, "created", true)
  setElementData(v, "name", v[5]) -- eg. "bobby"
end

for k,v in ipairs(getElementsByType("ped") do
    if getElementData(v, "created") then
      pedsToTrigger = {}
      table.insert(pedsToTrigger, v)
      triggerClientEvent(thePlayer, "someEvent", resourceRoot, pedsToTrigger)
    end
end
 
-- client side

  
addEvent("someEvent", true)
addEventHandler("someEvent", resourceRoot, function(pedsToTrigger)
      -- some gui functions
      pedGridList = guiCreateGridList(0, 0, 50, 50, false)
      if pedsToTrigger then
      	for _,v in ipairs(pedsToTrigger) do
            guiGridListAddRow(pedGridList, getElementData(v, "name"))
        end
   	  end
end)

 

why this creates me two separate gridlist instead of introducing them to one?

it looks like this:spacer.png

Link to comment
  • Scripting Moderators
28 minutes ago, NoviceWithManyProblems said:

why this creates me two separate gridlist instead of introducing them to one?

You don't check if this gridlist exists.

About this part of code, you are destroying your server network.

for k,v in ipairs(getElementsByType("ped") do
    if getElementData(v, "created") then
      pedsToTrigger = {}
      table.insert(pedsToTrigger, v)
      triggerClientEvent(thePlayer, "someEvent", resourceRoot, pedsToTrigger)
    end
end

Trigger will be called 1 * count of peds, it's very bad. Because it could be done with just trigger.

-- Client

local pedsGridlist = false

--

function customEvent(serverTable)
	if not pedsGridlist then
		pedsGridlist = guiCreateGridList(200, 100, 50, 50, false)
	end

	-- Finish your code
end
addEvent("customEvent", true)
addEventHandler("customEvent", resourceRoot, customEvent)
local peds = {
	{11, 0, 0, 3, "NPC 1"},
	{11, 0, 0, 4, "NPC 2"},
}

--

function createPeds()
	local ped = false
	local pedData = false
	local dataToSend = {}

	for i = 1, #peds do
		pedData = peds[i]

		ped = createPed(pedData[1], pedData[2], pedData[3], pedData[4])
		dataToSend[#dataToSend + 1] = {ped, pedData[5]}
	end

	setTimer(function()
		triggerClientEvent(root, "customEvent", resourceRoot, dataToSend)
	end, 1000, 1)
end
addEventHandler("onResourceStart", resourceRoot, createPeds)

 

Edited by majqq
Fix
  • Thanks 1
Link to comment
  • Moderators
48 minutes ago, NoviceWithManyProblems said:

why this creates me two separate gridlist instead of introducing them to one?

 

Because you are creating for each event a new gridlist.

 

The following code will create a gridlist if it does not exist

if not pedGridList then
	pedGridList = guiCreateGridList(0, 0, 50, 50, false)
end

 

 

  • Thanks 1
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...