Helo. I made this kind of code and it seems I did something wrong with calling my function, because my map can't load anymore. I've tested it out and it seems the function itself doesn't cause any trouble but only when I try to call it. The script is rather simple, there is a timer for 4 cars (2 of each team) which forces vehicles on the map to keep moving, to avoid blocking / stopping / going too slow and resets a parcticular vehicle when it doesn't move for 3 seconds. Maybe I shouldn't script it in mainlevel.lua? It's quite obvious there is always a better solution. Code: local M ={} --Red Team local firstRedCar local secondRedCar --Blue Team local firstBlueCar local secondBlueCar --Timers local moveTimer = {0, 0, 0, 0} local startMoveTimer = {false, false, false, false} local function onClientStartMission() --car initialization firstRedCar = scenetree.findObject("car_red1") secondRedCar = scenetree.findObject("car_red2") firstBlueCar = scenetree.findObject("car_blue1") secondBlueCar = scenetree.findObject("car_blue2") end local function stopCars(car, idx) if car:getVelocity().x == 0 or car:getVelocity().y == 0 then startMoveTimer[idx] = true end if startMoveTimer[idx] then moveTimer[idx] = moveTimer[idx] + dt else moveTimer[idx] = moveTimer[idx] end if moveTimer[idx] >= 3 then car:requestReset() moveTimer[idx] = 0 end if car:getVelocity().x > 0.1 or car:getVelocity().y > 0.1 then moveTimer[idx] = 0 end return 0 end local function onUpdate(dt) --Calling a function for each car stopCars(firstRedCar, 0) stopCars(secondRedCar, 1) stopCars(firstBlueCar, 2) stopCars(secondBlueCar, 3) end M.onClientStartMission = onClientStartMission M.onUpdate = onUpdate return M
I just realized there is no 0 index in lua, but I changed it and it didn't solve the problem so need to keep looking
In order to call a function from another file you need to also list it in the public interface (usually down at the bottom). In this case you need: Code: M.onClientStartMission = onClientStartMission M.onUpdate = onUpdate M.stopCars = stopCars You can then call mainLevel.stopCars(args) from e.g. a lua trigger.