Jump to content

IIYAMA

Moderators
  • Posts

    5,976
  • Joined

  • Last visited

  • Days Won

    192

IIYAMA last won the day on May 18

IIYAMA had the most liked content!

About IIYAMA

  • Birthday 01/01/2016

Member Title

  • Global Moderator

Details

  • Gang
    [HB]
  • Location
    Netherlands
  • Occupation
    I have never been to the streets of SA... so who knows?
  • Interests
    Design, scripting, UX/UI

Recent Profile Visitors

32,821 profile views

IIYAMA's Achievements

Gangsta

Gangsta (45/54)

1.5k

Reputation

Single Status Update

See all updates by IIYAMA

  1. Hi there, interested in scripting and debugging?

    Read the story below! ?

     


     

    Today I was testing a resource that I have been working on for the last 2 days. It is a resource for creating 'fires' as element. Used for fixing a lot of issues with 'fire' in general, good for zombie/coop, pvp and roleplay gamemodes. (will be public...)

    Anyway, this post is not about the resource. It is about a way you could check your resource for unwanted function calls.

     

    Back to the story.

    So I opened the network stats window. (/shownetstat)

    I noticed that the data rate going out was incredible high. (This is how much data my MTA client is sending to the server) The zombie resource was also using a lot of the network, but I could notice the difference.

    Unfortunately I wasn't sure which part was causing it. ?

     

    So I started writing a function wrapper. There is also the https://wiki.multitheftauto.com/wiki/AddDebugHook but I was a bit too lazy to test it out. So I went for the simple wrapper. The function that I had to check was 'triggerServerEvent'. Because this one is most likely the one responsible for the high out going network usage.

    do
    	-- Save the original triggerServerEvent reference
    	local triggerServerEvent_ = triggerServerEvent
    	
    	function triggerServerEvent (...)
    		return triggerServerEvent_(...)
    	end
    end

     

    Now I had to collect some of the results. First step was to define some of the variables that are used to store the results.

    Spoiler
    -- Define all required variables
    local triggerServerEventCount = 0 -- How many times the function is called
    local triggerServerEventByEvent = {} -- Which event and from where + how many times
    local debugInfo = ""

     

     

    Grouping and counting the results is also very important:

    Spoiler
    local arg = {...}
    
    --[[
    
    Group by the function calls by 'event' and call location
    
    ]]
    local groupByKey = arg[1] .. " > Line: " ..  debug.getinfo(2).currentline  .. " "
    triggerServerEventByEvent[groupByKey] = (triggerServerEventByEvent[groupByKey]  or 0) + 1
    
    --[[
    
    Increase the function execution counter
    
    ]]
    triggerServerEventCount = triggerServerEventCount + 1

     

     

    And the last step was to display, update and reset the collected results every 1 second.

    Spoiler
    addEventHandler("onClientRender", root, 
      function () 
        --[[
    
        Display information
    
        ]]
        dxDrawText(debugInfo, 30, 300)
      end)
    
    setTimer(function () 
        --[[
    
        Update the display information
    
        ]]
        debugInfo = "triggerServerEventCount: " .. triggerServerEventCount .. " > " .. inspect(triggerServerEventByEvent) .. "\n\n"
        triggerServerEventCount = 0
        triggerServerEventByEvent = {}
      end, 1000, 0)

     

     

    And here is more or less the result (screenshot & code). Could be improved by also adding the filename, but this is was good enough for this resource.

    (In the screenshot the issue is already fixed, just displaying debug information ingame)mta-screen_2021-10-16_21-01-20.thumb.png.8e7b9a484631211c4b02fbc8e4adf557.png

    Spoiler
    
    do
    	-- Save the original triggerServerEvent reference
    	local triggerServerEvent_ = triggerServerEvent
    
    
    	-- Define all required variables
    	local triggerServerEventCount = 0 -- How many times the function is called
    	local triggerServerEventByEvent = {} -- Which event and from where
    	local debugInfo = ""
    
    	addEventHandler("onClientRender", root, 
    	function () 
    		--[[
    
    			Display information
    
    		]]
    		dxDrawText(debugInfo, 30, 300)
    	end)
    
    	setTimer(function () 
    		--[[
    
    			Update the display information
    
    		]]
    		debugInfo = "triggerServerEventCount: " .. triggerServerEventCount .. " > " .. inspect(triggerServerEventByEvent) .. "\n\n"
    		triggerServerEventCount = 0
    		triggerServerEventByEvent = {}
    	end, 1000, 0)
    
    	function triggerServerEvent (...)
    		local arg = {...}
    
    		--[[
    
    			Group by the function calls by 'event' and call location
    
    		]]
    		local groupByKey = arg[1] .. " > Line: " ..  debug.getinfo(2).currentline  .. " "
    		triggerServerEventByEvent[groupByKey] = (triggerServerEventByEvent[groupByKey]  or 0) + 1
    
    		--[[
    
    			Increase the function execution counter
    
    		]]
    		triggerServerEventCount = triggerServerEventCount + 1
    
    		--[[
    
    			Resume the normal behaviour of the function:
    
    		]]
    		return triggerServerEvent_(...)
    	end
    end

     

     

×
×
  • Create New...