Saving the odometer state

Discussion in 'Content Creation' started by NOCARGO, Apr 21, 2019.

  1. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Hi,
    I wanted to reply to an older thread in vehicle suggestions and ideas but then I saw the notice that it
    would be better to start a new thread. Indeed there still is no sign of saveable overall distance.
    I am trying to make something work, no success so far. There is probably a good reason why there isn't a
    mod for this out yet ? In a specific vehicle lua I am able to assign a file input to a variable though I haven't succeeded yet in making this variable actually use this file as a number value. I think it just reads the file but if I put the variable in an equation to link it to the total distance I get vehicle error in game. Hope this thread
    is placed in the right section because I'm actually trying to make it work. If only I knew whether it's possible
    to let BeamNG read and write variable values from within specific vehicle lua AND over the same text file :)

    NC

    PS Happy Easter everybody !
     
  2. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    So essentially you have variable like odometer, you add to that during the game and also write that to file each time you update variable?

    At initialize stage it should then read value from text file and in update stage it should just add and write file again, I guess?

    Have you based odometer to how this mod handles odometer?:
    https://www.beamng.com/resources/satsuma-210-58.2009/

    How are you writing to file, are you using dumpToFile? Like example of Torsion here https://www.beamng.com/threads/lua-help.56996/#post-905294

    What you are using to read from the file?
     
  3. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Hi, yes I am using the Satsuma's odo as a base. It's already in my mod ( of mod :) ) for a while.
    My programming is quite poor but if I'm really after something then usual I persist until result. All my other
    (digital) gauges are working accurately and as desired, except the overall odo is manual (80's style) hence
    the Satsuma's.
    (Oh, I would like to be able to reset the trip counters too of course but once the odo is saveable that will be
    similar I guess)

    I don't know, I've tried several methods so far, it's not working yet. No reading or writing to variable so far. I've been
    trying things like io.input / io.output / io.read / io.write / io.open
    also these readFile and writeFile functions I found in the wiki (under lua reference). It's very hard to get something working.
    Last thing I tried was turning the specific vehicle's lua in to a controller type (M="auxiliary", relevantDevice = "nil").
    Also not working. I should look into this dumpToFile method when I have time, maybe that works.
    One of my big questions now is : Is the vehicle lua sandboxed ?
    For instance, one command was working : I tried writing to file and then I saw in console that it was writing to stdOut.
    I guess you know what stdOut is ? So it wasn't possible to change io.output and console says : libbeamng luaSandbox stdOut "my message"

    Then I started searching online about what to do about certain restrictions connected to luaSandbox wich seems to be an external
    PHP library ? Anyway, things got complicated from there on :)
    I will try if dumpToFile gives any success asap :)
     
    #3 NOCARGO, Apr 24, 2019
    Last edited: Apr 24, 2019
  4. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    I don't remember for certain, but I think that there were some limitations what you could do for safety reasons, I did try to find a post, but could not find it. I think there was limitations of where you could save files and also there could be more security related safeties, but I don't really have any LUA experience, I know only little bits I have read, but it is still big unknown I'm trying to start learning (might stay that way for years for me).

    Hopefully someone who knows more can fill the blanks here.
     
  5. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Indeed safety and security reasons are why it's so difficult to read and write towards a variable. I also read that this goes as well
    for javascript. You see, lua and java(script) are far more used then inside beamNG, mostly web and server applications. It's like
    I read a while ago on this forum that someone stated "then people would be using the gps screen to watch YouTube videos. ;)

    Anyway, there's always a workaround so sooner or later I or someone else (probably the latter) will find a way to make the odo
    saveable, and probably even without jeopardizing safety and security. If I discover a way to make this I will post. :)
     
  6. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Alright, don't ask me how ( just kidding, you always can ) but so far I managed to sort of get a single line in a file that gives
    driven distance after my session. It's very very messy ( but not so after refining ) but it works and the file seems to be able to
    bear any extension, which I think is good news.

    Now, I know about those I/O restrictions within BeamNG and I understand why but, maybe someone knows if it still might be
    possible to let's say, using the Satsuma's odometer's lua file (created by MetalMuncher), telling the var that says 'local distanceTotalm = 0'
    to fetch it's value from another lua file (or json, or txt etc) in the same folder, in fact by fetching I mean adding to the exisiting value ?

    I think that would be nice way to make the odo countable. If it's not possible I'll have to move on and search for another approach.
    After I got this far I already have something in mind but it would be more workaround then just let the local file ( odometer.lua ) handle it. :)

    NC
     
  7. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    Hmm, if you can read value to some variable, then something like this should give increasing distance, imo.
    local readValue = readDataCommand
    distanceTotalm = distanceTotalm + readValue

    Often I have found out that when doing math, it is best to read value to variable before doing math, in some other language at least, but not sure if that will solve puzzle you have right now.
     
  8. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Oh but your reasoning is right on the spot :) That's just what I meant by 'fetch-the-value-and-add-to-existing one :D
    The nut to crack here is : local readValue = readDataCommand
    It's the 'read' behind the '=' that becomes more difficult. Can we 'read' in the first place from within this lua ?
     
  9. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Got it working :D
    For now just on my favourite car AND for the time being it's not recording distance if I exit the session in game and drive a new
    session. In other words it records my last session in game so far. The testing result so far is the following :
    I boot up BeamNGdrive , get the odo, drive around, bail BeamNGdrive (and even reboot pc), restart BeamNGdrive,
    last overall distance appears on odo, drive around and so on.. I'm sure I will get some more refinement out of the method
    I'm currently using.

    If someone wants to know how this works please feel free to ask but for now this is NO orthodox way of implementing code
    into the game and I'm not sure I'll be able to explain this to someone who doesn't really knows programming as I myself
    belong to that kind of someone heh... but I can explain how I did it. :)

    NC
     
    • Like Like x 2
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice