Cronoss Posted February 23, 2022 Share Posted February 23, 2022 I'm very worried about the optimization in my server and I was wondering how to test it. Also maybe some advices about stuff like this -> If in my script there is a value that gets the "ped ocuppied vehicle" but then I get the ped occupied vehicle again in another function, that would cause problems? How could I solve it if I want to be sure about the information of the player? First function: function toggleEngine(playerSource, cmd) local vehicle = getPedOccupiedVehicle(playerSource) if (vehicle) then Second function: function toggleLights(playerSource, cmd) local vehicle = getPedOccupiedVehicle(thePlayer) if (vehicle) then Also, calling the database in different functions would cause bad optimization / lag ? Example: function toggleLock(playerSource, cmd) ---------Toggle lock function local owner = tostring(getPlayerName(playerSource)) local vehicle = getNearestVehicle( playerSource, 5 ) or getPedOccupiedVehicle(playerSource) if (vehicle) then local nameplate = getVehiclePlateText( vehicle ) local ownerReq = exports.mysql:_Query("SELECT * FROM vehicles WHERE plate=?", nameplate) ------"First" time using the database to compare the plate if (ownerReq) then ----------Toggle engine function----------- local nameplate = getVehiclePlateText( vehicle ) local ownerReq = exports.mysql:_Query("SELECT * FROM vehicles WHERE plate=?", nameplate) --second time using the database in ANOTHER FUNCTION if (ownerReq) then if(#ownerReq > 0) then Link to comment
Dzsozi (h03) Posted February 24, 2022 Share Posted February 24, 2022 9 hours ago, Cronoss said: First function: function toggleEngine(playerSource, cmd) local vehicle = getPedOccupiedVehicle(playerSource) if (vehicle) then Second function: function toggleLights(playerSource, cmd) local vehicle = getPedOccupiedVehicle(thePlayer) if (vehicle) then The second function has a problem returning the occupied vehicle, I don't know if that could be the cause of the problem you are having, or is that just a typo while you were writing the examples on forum. function toggleLights(playerSource, cmd) local vehicle = getPedOccupiedVehicle(playerSource) -- use playerSource instead of thePlayer, thePlayer is not defined so this will return an error and toggling lights won't happen if (vehicle) then 9 hours ago, Cronoss said: Also, calling the database in different functions would cause bad optimization / lag ? Example: function toggleLock(playerSource, cmd) ---------Toggle lock function local owner = tostring(getPlayerName(playerSource)) local vehicle = getNearestVehicle( playerSource, 5 ) or getPedOccupiedVehicle(playerSource) if (vehicle) then local nameplate = getVehiclePlateText( vehicle ) local ownerReq = exports.mysql:_Query("SELECT * FROM vehicles WHERE plate=?", nameplate) ------"First" time using the database to compare the plate if (ownerReq) then ----------Toggle engine function----------- local nameplate = getVehiclePlateText( vehicle ) local ownerReq = exports.mysql:_Query("SELECT * FROM vehicles WHERE plate=?", nameplate) --second time using the database in ANOTHER FUNCTION if (ownerReq) then if(#ownerReq > 0) then Yes, this would cause lag and lots of unnecessary data flow. You don't have to get a mysql result everytime you lock/start a vehicle. If I were doing toggleLock and toggleEngine functions for vehicles, I would just simply use them when a player types a command / presses a key, as you are doing, inside those toggleLock and toggleEngine functions simply use setVehicleLocked and setVehicleEngineState like function toggleLock(vehicle, state) if not vehicle then return false end setVehicleLocked(vehicle, state) return true end For the command you would do function lockCommand(player, command) local vehicle = getPedOccupiedVehicle(player) if vehicle then toggleLock(vehicle, not isVehicleLocked(vehicle)) end end addCommandHandler("lockveh", lockCommand) Keep the actual toggle functions seperate from the command functions, that way it will be easier for you to see, utilize and use those functions later as well. Then have a different save resource or function to save the data to mysql. You can make a timer every 5 seconds to call the saving, and inside the save function you would get every desired vehicle and their state of engine and locks to update mysql data with the new ones. This would make data usage and network flow much more less in my opinion, other than calling for mysql everytime a player types a command; imagine if they start spamming. Of course you can make anti-spam checks, but overall that way would be much more inefficient. Also there is a resource that you can use to check the usage of resources: performancebrowser - Multi Theft Auto: Wiki I believe it is already included in default MTA resources. 1 Link to comment
Cronoss Posted February 24, 2022 Author Share Posted February 24, 2022 Quote I would just simply use them when a player types a command / presses a key, as you are doing, inside those toggleLock and toggleEngine functions simply use setVehicleLocked and setVehicleEngineState like I understand this part, but I also need the sql consult to verify if the player it's the owner of the vehicle, that's why I call it almost every 5 seconds in the script but I don't know how to verify the player only once Link to comment
Cronoss Posted February 24, 2022 Author Share Posted February 24, 2022 I figured out, no need for more answers, thank you 1 Link to comment
Dzsozi (h03) Posted February 26, 2022 Share Posted February 26, 2022 setElementData might come handy in these cases. Good luck with scripting! 1 Link to comment
Cronoss Posted February 27, 2022 Author Share Posted February 27, 2022 Sorry for coming back... I have a new question. Is it more efficient save the data with a "timer function" or save the data when the player leaves? Link to comment
Moderators IIYAMA Posted February 27, 2022 Moderators Share Posted February 27, 2022 42 minutes ago, Cronoss said: Sorry for coming back... I have a new question. Is it more efficient save the data with a "timer function" or save the data when the player leaves? When the player leaves. But the downside is of that is the chance of data loss. (when the server crashes) Yet, that part should be considered more as a feature than a must in my opinion. The most logic timing is when the player leaves. For some data it is not an option to make use of the onPlayerQuit event timing, for example the data that is only stored at the client their PC shouldn't/couldn't be send over to the server when a player quits. 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