Solved Changing vehicle during scenario desactivate checkpoints

Discussion in 'Mod Support' started by RyvyLo, Jul 11, 2016.

  1. RyvyLo

    Expand Collapse

    May 15, 2014
    Hello !

    I'm trying to make a scenario where the player's car changes automatically when driving through a certain checkpoint. However, as soon as the vehicle is changed driving through the next checkpoint doesn't trigger it.

    Any idea for a solution ?

    Attached Files:

  2. Scepheo

    Expand Collapse

    Feb 10, 2015
    This is because the waypoint tracking is done by the vehicle, so resetting it makes it lose that information. If you look at lua/ge/extensions/scenario/race.lua:141, you'll find the following bit of code (where bo is the player vehicle and nwp the next waypoint):
    -- aim for the next waypoint
    bo:queueLuaCommand('scenario.setTrigger("race.onScenarioVehicleTrigger", ' .. serialize(nwp.pos) .. ',' .. nwp.radius .. ');')
    You'll have to call similar code after reloading the player vehicle.
    • Like Like x 1
  3. RyvyLo

    Expand Collapse

    May 15, 2014
    Thanks ! But sadly I didn't managed to get it working, I have a lua error each time.

    Code I added to my lua :
    -- Vehicle change at the 1st waypoint
        if data.waypointName == 'testwp1' then
                scenario_player0.JBeam = "sunburst";
                scenario_player0.partConfig = "vehicles/sunburst/sport_S_M.pc";
            log('I', 'test.vehicleChanger', 'Sunburst spawned')
            --Getting the vehicle id
            local vid = scenario.vehiclesByID[id]
            local bo = be:getObjectByID(vid)
            if not bo then return end
            local nwp = "testwp2"
            local pos = "vec3(0,-50,0)"
            local radius = "4.3301272392273"
            bo:queueLuaCommand('scenario.setTrigger("race.onScenarioVehicleTrigger",' .. pos .. ',' .. radius .. ');')
    Here is the part of the log where the error is written :
    19.61711|I|GELua.test.vehicleChanger|Sunburst spawned
    19.61739|E|GameEngineLua|*** FATAL LUA ERROR: levels/smallgrid/scenarios/testScenario.lua:16: attempt to index global 'scenario' (a nil value)
    Stack Traceback
    (1) Lua local 'fct' at file 'levels/smallgrid/scenarios/testScenario.lua:16'
      Local variables:
      data = table: 0x5f8a1120  {waypointName:testwp1, time:3.3834845097736, vehicleName:scenario_player0 (more...)}
      goal = nil
      (*temporary) = nil
      (*temporary) = string: "I"
      (*temporary) = string: "test.vehicleChanger"
      (*temporary) = string: "Sunburst spawned"
      (*temporary) = number: 7.50616e-313
      (*temporary) = number: 1.14048e+152
      (*temporary) = userdata: 0x5f831c38
      (*temporary) = string: "I"
      (*temporary) = string: "test.vehicleChanger"
      (*temporary) = string: "Sunburst spawned"
      (*temporary) = table: 0x5f806a78  {__gc:function: 0x5f8077b0, findObjectByIdAsTable:function: 0x5f808050, log:function: 0x5f807688 (more...)}
      (*temporary) = table: 0x5f806a78  {__gc:function: 0x5f8077b0, findObjectByIdAsTable:function: 0x5f808050, log:function: 0x5f807688 (more...)}
      (*temporary) = string: "attempt to index global 'scenario' (a nil value)"
    (2) Lua field 'hook' at file 'lua/common/extensions.lua:159'
      Local variables:
      func = string: "onRaceWaypoint"
      (for generator) = C function: next
      (for state) = table: 0x5fa22978  {1:function: 0x5fa23ae8, 2:function: 0x5f8bf778, 3:function: 0x5fa91670 (more...)}
      (for control) = number: nan
      _ = number: 4
      fct = Lua function 'onRaceWaypoint' (defined at line 1 of chunk levels/smallgrid/scenarios/testScenario.lua)
    (3) Lua field 'onScenarioVehicleTrigger' at file 'lua/ge/extensions//scenario/race.lua:108'
      Local variables:
      id = number: 1878
      w = table: 0x5f94f0f0  {nextWp:table: 0x5f92f5f0, lap:0, cur:1, next2:2, next2Wp:table: 0x5f8361f8 (more...)}
      vid = number: 1878
      bo = userdata: 0x5f8a7090
      lapDiff = number: 0
    (4) main chunk of [string "line"] at line 1
    19.61742|E|GameEngineLua|*** while executing this line: race.onScenarioVehicleTrigger(1878)
    19.61743|E|GameEngineLua|*** in chunk: line
    19.64238|D|libbeamng.engine|deleting object slot 0 ...
    19.65858|I|BeamNGVehicle::spawnObject|Spawning object 'sunburst' with config: 'vehicles/sunburst/sport_S_M.pc'
    19.69430|D|libbeamng.engine|creating new object in slot 0 ...
    19.70393|D|libbeamng.lua.V.default.init|spawning vehicle vehicles/sunburst/
    19.75166|D|libbeamng.lua.V.default.init|  using partconfig filename vehicles/sunburst/sport_S_M.pc - path:vehicles/sunburst/
    19.75255|D|libbeamng.lua.V.jbeam.loadDirectories|set vehicle directory to vehicles/sunburst/
    The log points the "race.onScenarioVehicleTrigger" as the source of the error, however I didn't managed to understand what this variable is ( I didn't found any "race" table mentioned elsewhere EDIT : apparently this is a function and not a table, but I still don't understand why it doesn't work).
    One last point that also seems weird to me : on the log I can see that the sunburst is changed after the error appearing, but the code that produce the error is after the vehicle change.
    #3 RyvyLo, Jul 12, 2016
    Last edited: Jul 13, 2016
  4. meywue

    Expand Collapse
    BeamNG Team

    Nov 19, 2015

    ok unfortunately it's not possible for now to change the vehicle without breaking the checkpoint stuff. Currently the vehicle itself is storing the data about the next checkpoint which you have to reach, so there's no superior player class which is sotring such data. So changing the vehicle results in the loss of the next checkpoint. So without changing default code it's possible for now.

    Forutnately in the meantime one of the devs modified the function so the checkpoint data will be cached in a variable and passed to the next car afterwards. In the prospective update you just have to change the vehicle via Lua and it the checkpoint stuff will work automatically.
    • Like Like x 1
  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