How to get file size under Lua/

Discussion in 'Programming' started by jimmyking, Feb 29, 2024.

  1. jimmyking

    jimmyking
    Expand Collapse

    Joined:
    Mar 9, 2020
    Messages:
    93
    its possable as its under lua/ge/ge_utils


    ill have already tried to use it but failed as I can't get it to take a file path like
    filename = "D:/Program Files/steam/steamapps/common/BeamNG.drive/lua/vehicle/hydros.lua"
    then if I tried printing f I got nil

    or am i doing something wrong? :thinking emoji:
     
    #1 jimmyking, Feb 29, 2024
    Last edited: Feb 29, 2024
  2. SineMatic

    SineMatic
    Expand Collapse

    Joined:
    Nov 17, 2022
    Messages:
    62
    Only BeamNG.drive directory is exposed to the game engine. So try using lua/vehicle/hydros.lua path instead
     
    • Agree Agree x 1
  3. jimmyking

    jimmyking
    Expand Collapse

    Joined:
    Mar 9, 2020
    Messages:
    93
    thanks, I got it working with
     
    • Like Like x 1
  4. daniel-w

    daniel-w
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 28, 2017
    Messages:
    284
    Just a quick heads up, io operations can be quite slow, so try to avoid doing it every update. Glad to see you've got it working though
     
  5. jimmyking

    jimmyking
    Expand Collapse

    Joined:
    Mar 9, 2020
    Messages:
    93
    Yeah I'm bumping but now I'm trying to use it from ge_utils.lua with getFileSize() intend of having
    Code:
    f = io.open(filename, "r")
    res = f:seek("end")
    in the lua file so I tried using:
    Code:
    local res = obj:queueGameEngineLua('getFileSize("lua/vehicle/hydros.lua")')
    and
    Code:
    obj:queueGameEngineLua('res = getFileSize("lua/vehicle/hydros.lua")')
    but whatever I tried I couldn't get a return value
     
  6. r3eckon

    r3eckon
    Expand Collapse

    Joined:
    Jun 15, 2013
    Messages:
    635
    You can't get return values from queued commands to another lua vm. When you really need to you can get values between vlua and gelua by queuing a ge command from vlua, that will itself queue a vlua command with the return value as a parameter.

    But your simplest option here is to take that getFileSize function from ge_utils.lua and copy it into a vlua script (ex: /lua/vehicle/extensions/myutils.lua) like this:
    Code:
    local M = {}
    
    local function getFileSize(filename)
      local res = -1
      local f = io.open(filename, "r")
      if f == nil then
        return res
      end
      res = f:seek("end")
      f:close()
      return res
    end
    
    M.getFileSize = getFileSize
    
    return M
    
    Then from a vlua script you can do this:
    Code:
    local utils = require("extensions/myutils")
    local size = utils.getFileSize("file path")
     
  7. jimmyking

    jimmyking
    Expand Collapse

    Joined:
    Mar 9, 2020
    Messages:
    93
    thanks, it works
     
  8. jimmyking

    jimmyking
    Expand Collapse

    Joined:
    Mar 9, 2020
    Messages:
    93
    BUMP! I found a way to get the file size from the game engine and return it to the vehicle Lua, and it's this:

    Code:
    obj:queueGameEngineLua("be:getPlayerVehicle(0):queueLuaCommand('FileSize(' .. getFileSize(\"lua/vehicle/hydros.lua\") .. ')')")
    
    function FileSize(FS)
    print(FS)
    end
    Yeah, it was that simple, or I guess complex, so hopefully this can help someone else
     
  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