Dzsozi (h03) Posted September 21, 2018 Share Posted September 21, 2018 Hello! I would like to use tables for my vehicle system to save data, since I really want to have a good performance. Whatever, so I tried using toJSOn and fromJSON but I don't really understand how that works. I want to save every vehicles' blinker state/side when the resource stops and set it back when the resource starts. How can I do that with tables? Here's my current code, I tried lots of different ways but couldn't manage to get it working. I would appreciate if someone could help me out! local function toggleIndicator(key,keyState) if isTimer(spamTimer) then return end for k, vehicle in pairs(getElementsByType("vehicle")) do local driver = getVehicleOccupant(vehicle) if driver == localPlayer then if (keyState == "down") then spamTimer = setTimer(function() spamTimer = nil end, 1000, 1) indicatorInfo[vehicle] = indicatorInfo[vehicle] or {} local indicatorState = indicatorInfo[vehicle]["side"] or nil if indicatorState then --removeEventHandler("onClientRender", getRootElement(), startIndicator) indicatorInfo[vehicle]["side"] = nil handleIndicatorLights() return end if key == blinkerLeft then indicatorInfo[vehicle] = { ["side"] = "left", } elseif key == blinkerRight then indicatorInfo[vehicle] = { ["side"] = "right", } else indicatorInfo[vehicle] = { ["side"] = "both", } end end end end end addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function() bindKey(blinkerLeft, "down", toggleIndicator) bindKey(blinkerRight, "down", toggleIndicator) bindKey(blinkerBoth, "down", toggleIndicator) for k, vehicle in pairs(getElementsByType("vehicle")) do indicatorInfo[vehicle] = indicatorInfo[vehicle] or {} indicatorInfo[vehicle]["side"] = fromJSON(data) end end ) addEventHandler("onClientResourceStop", getResourceRootElement(getThisResource()), function() for k, vehicle in pairs(getElementsByType("vehicle")) do if indicatorInfo[vehicle] then local data = toJSON({indicatorInfo[vehicle]["side"] or nil}) end end end ) I get this error with the current code, but I don't know how to fix it: Bad argument @ 'fromJSON' [Expected string at argument 1, got nil] Link to comment
MIKI785 Posted September 21, 2018 Share Posted September 21, 2018 I don't see you saving it anywhere though. addEventHandler("onClientResourceStop", getResourceRootElement(getThisResource()), function() for k, vehicle in pairs(getElementsByType("vehicle")) do if indicatorInfo[vehicle] then local data = toJSON({indicatorInfo[vehicle]["side"] or nil}) end end end ) You store the data in a local data and that's it. So the data is then lost, it never gets saved anywhere. addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function() bindKey(blinkerLeft, "down", toggleIndicator) bindKey(blinkerRight, "down", toggleIndicator) bindKey(blinkerBoth, "down", toggleIndicator) for k, vehicle in pairs(getElementsByType("vehicle")) do indicatorInfo[vehicle] = indicatorInfo[vehicle] or {} indicatorInfo[vehicle]["side"] = fromJSON(data) end end ) Here the data is undefined/nil. You never give it a value. You should save the data somewhere, database would be the best but if that's too complex then try simply saving it into a file and then read that file when the resource starts. Link to comment
Dzsozi (h03) Posted September 21, 2018 Author Share Posted September 21, 2018 (edited) If I save the data in a file how should I do that? Every vehicle’s data in a new line? Or how should I get which vehicle has the data? Database seems a bit more complicated and also I don’t want to get it on server side. I am looking for the best performance for my scripts. What do you think which is the most performance friendly solution for that? Edited September 21, 2018 by Dzsozi (h03) Link to comment
JeViCo Posted September 21, 2018 Share Posted September 21, 2018 11 minutes ago, Dzsozi (h03) said: If I save the data in a file how should I do that? Every vehicle’s data in a new line? Or how should I get which vehicle has the data? Database seems a bit more complicated and also I don’t want to get it on server side. I am looking for the best performance for my scripts. What do you think which is the most performance friendly solution for that? (server-side !!!) use setElementData for each vehicle. Load your data after on resource start and remove it (removeElementData) Link to comment
Dzsozi (h03) Posted September 21, 2018 Author Share Posted September 21, 2018 2 minutes ago, JeViCo said: (server-side !!!) use setElementData for each vehicle. Load your data after on resource start and remove it (removeElementData) I don't really want to use setElementData, I rather prefer tables on client side for this, if is that possible. Probably it is, since the script works right now, I just need to know how to store the vehicles' data when I stop/start/restart the resource. But I don't think that I need element datas on server side for that, since this is not something like I would need to be aware of to people change it's data value with cheat engine or something. Link to comment
JeViCo Posted September 21, 2018 Share Posted September 21, 2018 (edited) 3 hours ago, Dzsozi (h03) said: I don't really want to use setElementData, I rather prefer tables on client side for this, if is that possible. Probably it is, since the script works right now, I just need to know how to store the vehicles' data when I stop/start/restart the resource. But I don't think that I need element datas on server side for that, since this is not something like I would need to be aware of to people change it's data value with cheat engine or something. This might be a little bit offtopic but i 100% understand you - that's quite nasty problem. Anyway you can't avoid it all the time. This page may help you. Also you can attach other information elements such as ID, type, etc so you can manage them with the least risk Edited September 21, 2018 by JeViCo Link to comment
pa3ck Posted September 22, 2018 Share Posted September 22, 2018 There are couple of questions that need to be answered first... you only mention client side, so you don't care whether the blinkers are same for everyone? If so, you can use XML or JSON string in a client file - remember that fromJSON takes a string and converts it into a LUA table and toJSON converts a LUA table to a string that you can store in a file, once you converted the string back to LUA table, you can handle it as any other table. Link to comment
MIKI785 Posted September 22, 2018 Share Posted September 22, 2018 I just noticed that he uses an element to index the table, that can't be saved into a file. You'd have to use a different way of knowing which vehicle you're talking about. setElementID and getElementByID could be the answer, 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