Unsolved Scenario Get Fuel Level?

Discussion in 'Mod Support' started by Occam's Razer, May 11, 2018.

  1. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,152
    Is there a way to get the fuel in a vehicle through the scenario lua? I know how to set it in the .json, but I'm unsure about getting and setting it in scenario scripts.
     
  2. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,152
    Okay, I did some fiddling, and it looks like queuing the fuel level as a lua command can set the fuel level at runtime:

    Code:
    helper.queueLuaCommandByName('scenario_player0', 'energyStorage.getStorage("mainTank"):setRemainingVolume(1)')
    I still can't figure out what the trick for getting the fuel level is, though. fuelTank.lua does make mention of storage.remainingVolume. Whether this can be accessed from an external script, and how, is the problem. I don't think I can use queueLuaCommandByName, though, as I believe that's for causing the vehicle to perform actions (such as turning on a lightbar), and not for retrieving a variable.
     
  3. meywue

    meywue
    Expand Collapse
    Administrator
    BeamNG Team

    Joined:
    Nov 19, 2015
    Messages:
    339
    The solution is queuing and queuing again.

    In the end it should look like this (more or less). You should have a function that gets queued from vehicle lua again to pass the value you need:

    Code:
    
    local horn = 0
    
    local function setHorn(value)
        horn = value
    end
    
    playerVehicle:queueLuaCommand('obj:queueGameEngineLua(\'mainLevel.setHorn(\'.. tostring(electrics.values.horn) ..\')\')')
    
    
     
    • Like Like x 1
    • Agree Agree x 1
  4. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,152
    Thanks, but I can't seem to get the queue line to work. I've got this (I'm keeping it as the horn for now, to rule out energy storage-related problems):

    Code:
    local playerCarData = map.objects[map.objectNames['scenario_player0']]
    
    playerCarData:queueLuaCommand('obj:queueGameEngineLua(\'mainLevel.setHorn(\'.. tostring(electrics.values.horn) ..\')\')')
    But I keep getting an "attempt to call method 'queueLuaCommand' (a nil value)" error. If there was something else in the line I was supposed to replace, I'm unsure as to what it is.
     
  5. meywue

    meywue
    Expand Collapse
    Administrator
    BeamNG Team

    Joined:
    Nov 19, 2015
    Messages:
    339
    I'm afraid that playerCarData has either the wrong type or it's nil. Try using...

    Code:
    playerCarData = scenetree.findObject('scenario_player0')
    ... or...
    Code:
    playerCarData = be:getPlayerVehicle(0)
    Also use dump as much as possible to see if your variable is nil or whatsoever..

    Code:
    dump(playerCarData)
    And to figure out the fields and functions of a certain object use getmetatable

    Code:
    dump(getmetatable(playerCarData))
     
    #5 meywue, May 15, 2018
    Last edited: May 15, 2018
    • Informative Informative x 1
  6. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,152
    Yeah, that one works, I'm too used to getting vehicles the other way.

    Noted, that sounds very useful.

    I'm still having a bit of trouble, though. I've now got:

    Code:
    player = scenetree.findObject('scenario_player0')
    
    player:queueLuaCommand('obj:queueGameEngineLua(\'map.setHorn(\'.. tostring(electrics.values.horn) ..\')\')')
    But the script now says that setHorn() is nil (although dump correctly identifies it as a function). 'map' works, where 'mainLevel' did not, but shouldn't the object to run the function be the scenario, not the map?
     
  7. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,152
    Okay, I've narrowed the problem down to this part here:

    player:queueLuaCommand('obj:queueGameEngineLua(\'mainLevel.setHorn(\'.. tostring(electrics.values.horn) ..\')\')')

    I understand how the script works as a whole, but I don't understand what I'm supposed to put here. The above suggestion is a script taken from Nadeox's simple_map, and mainLevel is the .lua file's name. But I've tried creating a file called mainLevel and putting it in the same relative spot, with the setHorn function inside. It doesn't work. I've tried replacing mainLevel.setHorn with refuelTest (the scenario's name).setHorn. No dice. I've tried just plain using setHorn(), with no file specified. Needless to say, I'm not getting anywhere.
     
  8. meywue

    meywue
    Expand Collapse
    Administrator
    BeamNG Team

    Joined:
    Nov 19, 2015
    Messages:
    339
    Ah yep... sorry.

    mainLevel is the alias of the level specific lua module (loaded with a freeroam map like Nadeox' simple map) in the global lua table.

    You should use scenario_yourluafilename.setHorn() instead ( Given that your lua filename is the same as your scenario filenames so the game is loading it automatically and creates the global alias in the table for you ).

    To check you can dump(extensions) to see the loaded extensions and their global aliases like this:
     
    #8 meywue, May 17, 2018
    Last edited: May 17, 2018
    • Like Like x 1
  9. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,152
    Oh, thank you, I was going crazy over that. Now to try the fuel-related stuff. After some sleep.
     
  10. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,152
    Soooo... I was going to come here to say that I started experimenting with making the code fuel-specific. I was going to post this code here:

    Code:
    player:queueLuaCommand('obj:queueGameEngineLua(\'scenario_refuelTest.setHorn(\'.. tostring(energyStorage.getStorage("mainTank").remainingVolume) ..\')\')')
    And I was going to say that it was raising an error, in spite of the fact that the error message clearly contained an accurate measure of the fuel in the tank. But for some reason, this same code that didn't work yesterday has now started working. So, yeah, use it as you please.

    Computers are weird.

    Edit: Nope, it's back.

    screenshot_00660.png
     
    #10 Occam's Razer, May 19, 2018
    Last edited: May 19, 2018
  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