Solved Sending data to angular .js file on Scenario Start called.

Discussion in 'Mod Support' started by bidwars, Feb 16, 2017.

  1. bidwars

    bidwars
    Expand Collapse

    Joined:
    Sep 23, 2014
    Messages:
    171
    I need some help on how to send some data right after the Scenario Start is pressed or called. I need this to be sent before anything else happens in the scenario.

    I have listed the HTML file and .js file that my scenario uses.
    I would like to not use a ng-change in the html file and rather have it call myFunction(selectedItem) after the Start button is pressed for the scenario. Is there anyone good with angular.js that could help me out?

    *******html file*************
    <div ng-controller="myController">
    <p>My Question</p>
    <select
    ng-model="selectedItem"
    ng-options="o.id as o.name for o in list"
    ng-change="myFunction(selectedItem)">
    </select>
    </div>

    *********.js file********
    .controller('myController',['$log', '$scope', '$state', 'bngApi', function ($log, $scope, $state, bngApi) {
    var vm = this;

    $scope.list =[
    {id:1, name:"Number1"},
    {id:2, name:"Number2"}

    $scope.selectedItem = 1;

    $scope.myFunction = function(arg1) {
    console.log("calling function with arg: ", arg1);
    // call with no return value:
    bngApi.engineLua('my_lua_module.myFunction(' + bngApi.serializeToLua(arg1) + ')');
    }
    }])
     
  2. meywue

    meywue
    Expand Collapse
    Administrator
    BeamNG Team

    Joined:
    Nov 19, 2015
    Messages:
    340
    Hey,

    unfortunately there's no function/event which is called right before everything's else is happening. I'm not a javascript pro at all, so I asked one of our UI programmer and he gave me some hints for possible solutions.

    But all these possible solutions might not perfectly fit cause we cannot be certain that it will be the very first thing that gets called.

    It would be very helpful if you provide us the purpose of this?!

    Anyways here we go:

    1. You can create a custom lua function for your scenario which has the onScenarioUIReady(state) function. This one gets called a few times during scenarios. E.g. if the state == 'start' the player just pressed the start button. Anyways still not certain that it's the very first thing that get called after clicking on the button.

    2. You could try to find the start button via javascript and try listen that it was clicked.

    3. You could override the scenariocontrol.js to write your own trigger/event. This is always a bad idea since it might be broken once we change something in this code on our side. Also we cannot approve mods that override stock files. ;)

    If you want to guarantee that your function is the very first that gets called then you need to work with ng-change and ng-blur, he said. And that's the point where I'm out cause I never heard of that stuff before :p

    Cheers!

    If you have questions don't heistate to ask.
     
    #2 meywue, Feb 20, 2017
    Last edited: Feb 20, 2017
  3. bidwars

    bidwars
    Expand Collapse

    Joined:
    Sep 23, 2014
    Messages:
    171
    Thank's for the reply! So what I am trying to do is ask what configuration the user would like for each vehicle in the scenario. Then after the user clicks start it will load the configuration. I already use the ng-change event on the ui to send the reply back to my lua file. I cannot use that because the user could select other options on the page before clicking "start". So I don't load anything until after they are finished making selections. For example they want 3 vehicles, without bumpers. They might change their mind before clicking start so this is why I don't want to load anything yet.
    1) onScenarioUIReady(state) - This is the first thing I tried. It does not seem to fire any of my code until after everything else from scenarios is called.
    2) This might be the best way. Maybe the UI programmers could help here.
    3) I already thought about this but I would take a guess that it would interfere with the start button.


    Here is the other parts of the code that might help you answer this..

    Based on what the user selects I use function createVehicles for the number of vehicles "vehCount". At this point it just creates "cones". "I created cones because they spawn quickly."


    local function createVehicles(preFabName, vehCount)
    -- Create New Prefab Objects
    local v = createObject('Prefab')
    v.filename = "levels/derby/derby_vehicles_"..vehCount..".prefab"
    v.loadMode = 1 --'Manual'
    v.canSave = true
    v.canSaveDynamicFields = true
    v:registerObject(preFabName)
    log('D', 'scenarios.spawnPrefab', 'loading prefab '..preFabName);
    v:load();
    scenetree.ScenarioObjectsGroup:addObject(v.obj)
    -- update static collision with new prefabs
    be:reloadCollision()
    end

    The "cones" are created so then I replace them all with the correct configuration for the vehicle. The problem is it needs time to reload each vehicle with the correct configuration. This does not finish until the scenario has already went past "GO!".

    local function partsConfig(vName)
    TorqueScript.eval([[
    ]]..vName..[[.JBeam = "]] .. JBeamN .. [[";
    ]]..vName..[[.partConfig = "levels/derby/]]..vehicleConfig..[[_vehicles/derby_]]..JBeam..[[.pc";
    ]]..vName..[[.requestReload();
    ]])
    end

    My idea to help with mods loading:
    1) Maybe have the scenario UI popup before the scenario spawns any vehicles for the scenario.
    2) Have an onScenarioUIReady(state) of Loading so that anything mod makers want to add to the scenario can be added after start is clicked. I would then send back the "Start" command in the mod and show a loading screen to the user.
     
  4. bidwars

    bidwars
    Expand Collapse

    Joined:
    Sep 23, 2014
    Messages:
    171
    I was able to figure out a way to make this work.
    If you set the scenario.state back to pre-running. It will then wait to load all of the vehicles.
    scenario.state = 'pre-running' --set the senario back to pre-running. extensions.hook('onScenarioChange', scenario)

    Then I started it back up again using.
    scenarios.onScenarioUIReady('play')
     
  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