Solved Problem with calling a function in mainlevel.lua

Discussion in 'Mod Support' started by Car Devastator, Nov 26, 2022.

  1. Car Devastator

    Car Devastator
    Expand Collapse

    Joined:
    May 5, 2016
    Messages:
    66
    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
    
    
    
     
  2. Car Devastator

    Car Devastator
    Expand Collapse

    Joined:
    May 5, 2016
    Messages:
    66
    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
     
  3. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,319
    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.
     
    • Like Like x 2
  4. Car Devastator

    Car Devastator
    Expand Collapse

    Joined:
    May 5, 2016
    Messages:
    66
    Thank you so much! That's really valuable knowledge
     
  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