Scripting Moderators ds1-e Posted January 24, 2019 Scripting Moderators Share Posted January 24, 2019 Hi. I have a question about efficient. I have a function which creates, and update a image every frame (onClientRender), and i need to change color of those images. So i have two ways: 1. Using a lot of if's, and by that change a image color. 2. Or use a function which is using data from table, loop "for" which returns color value. local partsColor = { {maxv = 1000, minv = 1001, r = 92, g = 141, b = 71}, {maxv = 910, minv = 999, r = 92, g = 141, b = 71}, {maxv = 910, minv = 909, r = 90, g = 141, b = 71}, } function getColor(value) for i, color in ipairs(partsColor) do if value >= color.maxv and value <= color.minv then return color.r, color.g, color.b end end end I'm asking because i don't know if i should loop within onClientRender. Link to comment
Moderators IIYAMA Posted January 24, 2019 Moderators Share Posted January 24, 2019 1 hour ago, majqq said: Hi. I have a question about efficient. I have a function which creates, and update a image every frame (onClientRender), and i need to change color of those images. So i have two ways: 1. Using a lot of if's, and by that change a image color. 2. Or use a function which is using data from table, loop "for" which returns color value. local partsColor = { {maxv = 1000, minv = 1001, r = 92, g = 141, b = 71}, {maxv = 910, minv = 999, r = 92, g = 141, b = 71}, {maxv = 910, minv = 909, r = 90, g = 141, b = 71}, } function getColor(value) for i, color in ipairs(partsColor) do if value >= color.maxv and value <= color.minv then return color.r, color.g, color.b end end end I'm asking because i don't know if i should loop within onClientRender. Functions are slower. But you are at a point where you shouldn't worrying about optimizing to the bone. But if you really want to know about speed: Functions are one of the slowest type of operations in Lua. A loop can be faster than an if statement. But not with the ipairs loop.ipairs() = function Why use these `slow` functions? Your brain is also a kind of computer and it's performance can also be decreased. This happens very quickly when you do not use enough functions. Functions are used to name/identify the operations in your code. 1 Link to comment
Scripting Moderators ds1-e Posted January 25, 2019 Author Scripting Moderators Share Posted January 25, 2019 15 hours ago, IIYAMA said: Functions are slower. But you are at a point where you shouldn't worrying about optimizing to the bone. But if you really want to know about speed: Functions are one of the slowest type of operations in Lua. A loop can be faster than an if statement. But not with the ipairs loop.ipairs() = function Why use these `slow` functions? Your brain is also a kind of computer and it's performance can also be decreased. This happens very quickly when you do not use enough functions. Functions are used to name/identify the operations in your code. Well i should care about it, because onClientRender is the one and only event which use a lot of CPU. It's used to draw icons, vehicle menu, speedometer and other things. And i want that my code looks clearly, and works efficiently, as far it's possible. ipairs it's for iterating through numbers, and pairs iterates through all keys? Link to comment
Scripting Moderators thisdp Posted January 25, 2019 Scripting Moderators Share Posted January 25, 2019 (edited) 2 hours ago, majqq said: Well i should care about it, because onClientRender is the one and only event which use a lot of CPU. It's used to draw icons, vehicle menu, speedometer and other things. And i want that my code looks clearly, and works efficiently, as far it's possible. ipairs it's for iterating through numbers, and pairs iterates through all keys? Tell you the truth.. The slowest part may be the dx functions.. Edited January 25, 2019 by thisdp 1 Link to comment
Moderators IIYAMA Posted January 25, 2019 Moderators Share Posted January 25, 2019 (edited) 3 hours ago, majqq said: Well i should care about it, because onClientRender is the one and only event which use a lot of CPU. It's used to draw icons, vehicle menu, speedometer and other things. First make it, then optimise it. 3 hours ago, majqq said: And i want that my code looks clearly, and works efficiently, as far it's possible. ipairs it's for iterating through numbers, and pairs iterates through all keys? ipairs, pairs (and next) loops are function based loops. They process tables with their own rules. ipairs loops through numbers starting from 1. It will stop looping when there is no value at the next index. {1, 2, nil, 4} With this table it will loop only 2 items. It has as benefit to loop through error sensitive tables, which can skip the damaged data. (Not that I ever had to deal with a damaged table in my life...) pairs loops through all indexes. It doesn't matter if it is a number, string, userdata, table or even a function. The only thing it can't do is looping it in order. But do you really think that there are no other, not function based loops? Here is a list of loops that do not make use of lua functions: https://www.tutorialspoint.com/lua/lua_loops.htm Edited January 25, 2019 by IIYAMA Link to comment
Scripting Moderators ds1-e Posted January 25, 2019 Author Scripting Moderators Share Posted January 25, 2019 4 hours ago, IIYAMA said: First make it, then optimise it. ipairs, pairs (and next) loops are function based loops. They process tables with their own rules. ipairs loops through numbers starting from 1. It will stop looping when there is no value at the next index. {1, 2, nil, 4} With this table it will loop only 2 items. It has as benefit to loop through error sensitive tables, which can skip the damaged data. (Not that I ever had to deal with a damaged table in my life...) pairs loops through all indexes. It doesn't matter if it is a number, string, userdata, table or even a function. The only thing it can't do is looping it in order. But do you really think that there are no other, not function based loops? Here is a list of loops that do not make use of lua functions: https://www.tutorialspoint.com/lua/lua_loops.htm By the way, there is other way than onClientRender to keep image stay visible, and refresh it frequently, so i can reduce usage of player CPU's? Link to comment
Moderators IIYAMA Posted January 25, 2019 Moderators Share Posted January 25, 2019 1 hour ago, majqq said: By the way, there is other way than onClientRender to keep image stay visible, and refresh it frequently, so i can reduce usage of player CPU's? You can use a GUI element: https://wiki.multitheftauto.com/wiki/Client_Scripting_Functions#Static_Images But the quality will not be the same. Check also rendering with textures, you can boost the performance considerably: https://wiki.multitheftauto.com/wiki/DxCreateTexture 1 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