Jump to content

gridlist load data only if I restart the resource (thisdp's GUI)


Recommended Posts

Gridlist only load data if I restart the resource... I'm using thisdp's GUI ... I tried to load the weapons from server side when player joins but still the same.

function loadweapons()
	triggerServerEvent("loadOwnWeapons", localPlayer)
end
addEventHandler("onClientPlayerJoin", getRootElement(), loadweapons)

I set the window visible to false

function test()
        weapwindow = DGS:dgsCreateWindow(screenW - 278 - 10, (screenH - 237) / 2, 278, 237,"Your Weapons",false)
		DGS:dgsSetVisible(weapwindow, false)
		DGS:dgsWindowSetCloseButtonEnabled(weapwindow, false)
		DGS:dgsWindowSetMovable ( weapwindow, false )
        gridlist = DGS:dgsCreateGridList (0.04, 0.01, 0.92, 0.58, true, weapwindow )
		local items = getElementData(localPlayer, "weaponmodel")
        column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5)
        for k, v in ipairs(items) do
        row = DGS:dgsGridListAddRow(gridlist, v[1]) 
		DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
        end
		DGS:dgsSetVisible(weapwindow,false)
end
addEventHandler ( "onClientResourceStart", resourceRoot, test )

If I debug the items with iprint(items) I get only string instead of table... but if I restart the resource I get the table and everything is working.

And here I set the window visible to true if I click on other button from another window

addEventHandler ( "onClientClick", root,
    function ( button, state )
        if ( button == "left" and state == "down" ) then
            if drawInfo == true and ( isMouseInPosition ( screenW * 0.4195, screenH * 0.6680, screenW * 0.0523, screenH * 0.0166 ) ) then
			drawInfo = false
			DGS:dgsSetVisible(weapwindow,true)
            end
			if drawInfo == true and ( isMouseInPosition ( screenW * 0.4836, screenH * 0.6670, screenW * 0.0547, screenH * 0.0176 ) ) then
		    drawInfo = false
            end
        end
    end
)

the loadOwnWeapons server-side event is:

addEvent("loadOwnWeapons",true)
addEventHandler("loadOwnWeapons",getRootElement(),function(player)
	 local owner = getElementData(source, "acc:id")
	 local query = dbQuery(con,"SELECT * FROM weapons WHERE owner = ?", owner)
	local weapons = dbPoll(query,-1)
	if weapons then
	local newTable = {}
for k,v in ipairs(weapons) do
			newTable[#newTable + 1] = {v["model"]}
end
    setElementData(source, "weaponmodel", newTable)
end
end)

My question is why I only get the table from the loadOwnWeapons server-side event if I restart the resource?

Maybe because I only get the weaponmmodel elementdata in the onClientResourceStart event handler... but what should I replace it with?

Edited by thund3rbird23
Link to comment
  • Moderators
1 hour ago, thund3rbird23 said:

 

onClientPlayerJoin doesn't trigger for the localPlayer, only for the remote players (players other than yourself).

Use onClientResourceStart instead.

 

Or use onPlayerJoin serverside, if you are not using triggerClientEvent event directly after that event (, then it doesn't matter if the client has loaded his resource). But if you are planning that in the future, then keep using onClientResourceStart.

https://wiki.multitheftauto.com/wiki/OnPlayerJoin

 

Link to comment

  

3 hours ago, IIYAMA said:

Or use onPlayerJoin serverside, if you are not using triggerClientEvent event directly after that event (, then it doesn't matter if the client has loaded his resource). But if you are planning that in the future, then keep using onClientResourceStart.

 

I still don't understand what is the problem... look I added a button which is "refresh" the gridlist's items, when I click on that button and debug the items with iprint then I get only one string... for ex.: xyz123 and after I restart the resource and then click on the refresh button then I get the table which I need... But why?

function refreshBttnClick()
triggerServerEvent("loadOwnWeapons", localPlayer)
DGS:dgsSetVisible(weaponwindow,false)
       weaponwindow = DGS:dgsCreateWindow(screenW - 278 - 10, (screenH - 237) / 2, 278, 237,"Your Weapons",false)
	   DGS:dgsWindowSetCloseButtonEnabled(weaponwindow, false)
	   DGS:dgsWindowSetMovable ( weaponwindow, false )
        gridlist = DGS:dgsCreateGridList (0.04, 0.01, 0.92, 0.58, true, weaponwindow )
        column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5)
        local items = getElementData(localPlayer, "weaponmodel")
		iprint(items)
        for k, v in ipairs(items) do
        row = DGS:dgsGridListAddRow(gridlist, v[1]) 
		DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
		
        end
		refreshbttn = DGS:dgsCreateButton(0.05, 0.62, 0.34, 0.08, "Refresh", true, weaponwindow)
		addEventHandler ( "onDgsMouseClick", refreshbttn, refreshBttnClick, false )
end

 

Edited by thund3rbird23
Link to comment
6 hours ago, IIYAMA said:

It does matter which incorrect value you see, since it can point to the direction of the problem.

I'll try to describe it clearly ?

I query the weaponmodels here:

--- server.lua ---

addEvent("loadOwnWeapons",true)
addEventHandler("loadOwnWeapons",getRootElement(),function(player)
	 local owner = getElementData(source, "acc:id")
	 local query = dbQuery(con,"SELECT * FROM weapons WHERE owner = ?", owner)
	local weapons = dbPoll(query,-1)
	if weapons then
	local newTable = {}
for k,v in ipairs(weapons) do
			newTable[#newTable + 1] = {v["weaponmodel"]}
			
end
    setElementData(source, "weaponmodel", newTable)
end
end)

Then I calling this server event in client-side:

--- client.lua ---

function test()
triggerServerEvent("loadOwnWeapons", localPlayer)
local items = getElementData(localPlayer, "weaponmodel")
iprint(items)
end
addEventHandler ( "onClientResourceStart", resourceRoot, test )

The situation is: When I connect to the server then the items debug is (only one string):

XZG-324

After I restart the resource the items debug is an empty table:

{}

After I restarting the resource twice (a table with the player's weaponmodels which I need without restarting the resource):

{{"XZG-324"},{"YRT-598"},{"OEV-153"},{"EFC-543"},{"LEZ-054"}}

 

Link to comment
  • Moderators

The second situation is obvious, because the data isn't available yet.

 

The first and third are a little bit strange. Maybe it is for some unknown reason also related to the timing.

 

Debug code:

-- serverside

-- BEFORE line 12
newTable.tickCount = getTickCount() -- verification

-- line 12: setElementData(source, "weaponmodel", newTable)

-- AFTER line 12
triggerClientEvent(client, "onLoadOwnWeapons", client, newTable)

 

-- client

addEvent("onLoadOwnWeapons", true)
addEventHandler("onLoadOwnWeapons", localPlayer, 
function (items2) 
	local items = getElementData(localPlayer, "weaponmodel")
	iprint("\n\nitems:", items, "\n items2:", items2, "\n\n")
end, false)

 

And compare those values. (+ screenshot)

 

 

Link to comment
25 minutes ago, IIYAMA said:

The second situation is obvious, because the data isn't available yet.

 

The first and third are a little bit strange. Maybe it is for some unknown reason also related to the timing.

 

Debug code:


-- serverside

-- BEFORE line 12
newTable.tickCount = getTickCount() -- verification

-- line 12: setElementData(source, "weaponmodel", newTable)

-- AFTER line 12
triggerClientEvent(client, "onLoadOwnWeapons", client, newTable)

 


-- client

addEvent("onLoadOwnWeapons", true)
addEventHandler("onLoadOwnWeapons", localPlayer, 
function (items2) 
	local items = getElementData(localPlayer, "weaponmodel")
	iprint("items:", items, " items2:", items2, "\n\n")
end, false)

 

And compare those values. (+ screenshot)

 

 

items debug is false
items2 debug is the table what I need.

Screenshot: https://i.imgur.com/33JanAb.png

EDIT: my bad... I misspelled a word in items variable. The items & items2 debug is the table what I want.

Edited by thund3rbird23
Link to comment
7 minutes ago, IIYAMA said:

When the event onLoadOwnWeapons is fired, your data is ready to be used.

 

Uh okay I'm totally lost... I need those table when I open the gui window. I set the guivisible true when I click on a button from another window.

This is the main window but I need to set the guivisible false so we can't see the gui always.

The items variable doesn't work here I just left it there to better illustrate what I want to do. (I need to put the table in the gridlist but only after I click on a button)

function test()
        weapwindow = DGS:dgsCreateWindow(screenW - 278 - 10, (screenH - 237) / 2, 278, 237,"Your Weapons",false)
		DGS:dgsSetVisible(weapwindow, false)
		DGS:dgsWindowSetCloseButtonEnabled(weapwindow, false)
		DGS:dgsWindowSetMovable ( weapwindow, false )
        gridlist = DGS:dgsCreateGridList (0.04, 0.01, 0.92, 0.58, true, weapwindow )
		local items = getElementData(localPlayer, "weaponmodel")
        column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5)
        for k, v in ipairs(items) do
        row = DGS:dgsGridListAddRow(gridlist, v[1]) 
		DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
        end
		DGS:dgsSetVisible(weapwindow,false)
end
addEventHandler ( "onClientResourceStart", resourceRoot, test )

This is the another window where I have the button which "opens" the gui window (I set the gui visible to true):

addEventHandler ( "onClientClick", root,
    function ( button, state )
        if ( button == "left" and state == "down" ) then
            if drawInfo == true and ( isMouseInPosition ( screenW * 0.4195, screenH * 0.6680, screenW * 0.0523, screenH * 0.0166 ) ) then
			drawInfo = false
			DGS:dgsSetVisible(weapwindow,true)
            end
			if drawInfo == true and ( isMouseInPosition ( screenW * 0.4836, screenH * 0.6670, screenW * 0.0547, screenH * 0.0176 ) ) then
		    drawInfo = false
            end
        end
    end
)

How can I put the table in to the gridlist if I click on that button with your solution? Because currently the gridlist is empty and the debug just return an empty table "{}"

Link to comment
  • Moderators
17 minutes ago, thund3rbird23 said:

How can I put the table in to the gridlist if I click on that button with your solution?

You will have to run this part, when the data is available: (line 7 until 11)

local items = getElementData(localPlayer, "weaponmodel")
column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5)
for k, v in ipairs(items) do
  row = DGS:dgsGridListAddRow(gridlist, v[1]) 
  DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
end

 

local items = getElementData(localPlayer, "weaponmodel")

And if you only do this once, you probably do not need elementdata.

 

addEvent("onLoadOwnWeapons", true)
addEventHandler("onLoadOwnWeapons", localPlayer, 
function (items) 
  column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5) -- < this line can be moved back to it's original spot.
  for k, v in ipairs(items) do
    row = DGS:dgsGridListAddRow(gridlist, v[1]) 
    DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
  end
end, false)

 

Edited by IIYAMA
  • Thanks 1
Link to comment
58 minutes ago, IIYAMA said:

You will have to run this part, when the data is available: (line 7 until 11)


local items = getElementData(localPlayer, "weaponmodel")
column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5)
for k, v in ipairs(items) do
  row = DGS:dgsGridListAddRow(gridlist, v[1]) 
  DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
end

 

local items = getElementData(localPlayer, "weaponmodel")

And if you only do this once, you probably do not need elementdata.

 


addEvent("onLoadOwnWeapons", true)
addEventHandler("onLoadOwnWeapons", localPlayer, 
function (items) 
  column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5) -- < this line can be moved back to it's original spot.
  for k, v in ipairs(items) do
    row = DGS:dgsGridListAddRow(gridlist, v[1]) 
    DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
  end
end, false)

 

Thank you. Now it seems to work... but I can't get the dgsGridListGetSelectedItem before it's worked when I used onResourceStart event handler.

Do you have any idea?

addEvent("onLoadOwnWeapons", true)
addEventHandler("onLoadOwnWeapons", localPlayer, 
function (items) 
        weapwindow = DGS:dgsCreateWindow(screenW - 278 - 10, (screenH - 237) / 2, 278, 237,"Your Weapons",false)
		DGS:dgsWindowSetCloseButtonEnabled(weapwindow, false)
		DGS:dgsWindowSetMovable ( weapwindow, false )
		gridlist = DGS:dgsCreateGridList (0.04, 0.01, 0.92, 0.58, true, weapwindow )
		local items = getElementData(localPlayer, "weaponmodel")
        column = DGS:dgsGridListAddColumn(gridlist, "Weapons", 0.5)
        for k, v in ipairs(items) do
        row = DGS:dgsGridListAddRow(gridlist, v[1]) 
		DGS:dgsGridListSetItemText ( gridlist, row, column, v[1] )
        end
		sellbttn = DGS:dgsCreateButton(0.62, 0.62, 0.34, 0.08, "Sell", true, weapwindow)
		addEventHandler ( "onDgsMouseClick", sellbttn, SellBttnClick, false )
end, false)

--- SellBttnClick function (from Dgs Wiki page)---

function SellBttnClick(button, state)
	if button == "left" and state == "down" then
			if source == gridlist then 
		local Selected = DGS:dgsGridListGetSelectedItem(gridlist)
		if Selected ~= -1 then 
			local output= DGS:dgsGridListGetItemText(gridlist,Selected,column)
			outputChatBox(""..output.."",255,0,0)
		end
	end
	end
end

 

Link to comment
4 minutes ago, IIYAMA said:

Any errors?

Does the function SellBttn... gets called after clicking? (Just the function, not all the condition within)

 

No errors in debug and if I put only outputChatBox("clicked") inside the SellBttnClick function then displays that... so yes the function gets called after clicking.

Link to comment
  • Moderators
52 minutes ago, thund3rbird23 said:

No errors in debug and if I put only outputChatBox("clicked") inside the SellBttnClick function then displays that... so yes the function gets called after clicking.

<A> Now debug each condition until you find the one that isn't met:

iprint("function call")
if button == "left" and state == "down" then
	iprint("correct mouse button")
	if source == gridlist then 
		iprint("source is correct")
		local Selected = DGS:dgsGridListGetSelectedItem(gridlist) -- keep in mind that this variable starts with a capital S
		if Selected ~= -1 then 
			iprint("did select something Selected")

<B> You can also do it like this, debugging literally the condition, before doing the condition itself: (which takes more time to set-up, but gives also a lot more information)

iprint("correct mouse button:", button == "left" and state == "down", ">input>", button, state)
if button == "left" and state == "down" then
	iprint("source is correct:", source == gridlist, ">input>", source, gridlist)
	if source == gridlist then 
		local Selected = DGS:dgsGridListGetSelectedItem(gridlist) -- keep in mind that this variable starts with a capital S
		iprint("Selected is correct:", Selected ~= -1, ">input>", Selected )
		if Selected ~= -1 then 
			

 

Normally I start with <A>, finding out the location of the problem and then do <B> to figure out what is actually going on.

(Edit, it seems like \[ B \] makes my text bold...)

 

Edited by IIYAMA
Link to comment
7 minutes ago, IIYAMA said:

[A] Now debug each condition until you find the one that isn't met:


iprint("function call")
if button == "left" and state == "down" then
	iprint("correct mouse button")
	if source == gridlist then 
		iprint("source is correct")
		local Selected = DGS:dgsGridListGetSelectedItem(gridlist) -- keep in mind that this variable starts with a capital S
		if Selected ~= -1 then 
			iprint("did select something Selected")

You can also do it like this, debugging literally the condition, before doing the condition itself: (which takes more time to set-up, but gives also a lot more information)


iprint("correct mouse button:", button == "left" and state == "down", ">input>", button, state)
if button == "left" and state == "down" then
	iprint("source is correct:", source == gridlist, ">input>", source, gridlist)
	if source == gridlist then 
		local Selected = DGS:dgsGridListGetSelectedItem(gridlist) -- keep in mind that this variable starts with a capital S
		iprint("Selected is correct:", Selected ~= -1, ">input>", Selected )
		if Selected ~= -1 then 
			

 

Normally I start with [A], finding out the location of the problem and then do to figure out what is actually going on.

 

[A], spacer.png

spacer.png

So the problem is the source isn't correct? I'm calling the source (gridlist) from the "onLoadOwnWeaponsevent

Link to comment
  • Moderators
5 minutes ago, thund3rbird23 said:

So the problem is the source isn't correct? I'm calling the source (gridlist) from the "onLoadOwnWeaponsevent

So yes.

Since you clicked on the button, which makes the button the source and not the gridlist. So that condition will never be true.

 

Just change it to:

if gridlist then 
  
-- or: 
  
if isElement(gridlist) then -- < if you do not trust anything

 

Now you check if the gridlist is actually created.

 


 

How does it feel to debug like this?

Line for line until you know where and what the problem is.

  • Like 1
Link to comment
  • Moderators
1 minute ago, thund3rbird23 said:

Now the 


local Selected = DGS:dgsGridListGetSelectedItem(gridlist)
		iprint("Selected is correct:", Selected ~= -1, ">input>", Selected )

Selected returns false. But before that it worked without any problem

Did you actually select something from the list? (and is it still selected when you clicked the button?)

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