WIP Beta released Part Randomizer...?

Discussion in 'Content Creation' started by umustbeloggedintododat, Apr 4, 2021.

  1. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    I'm thinking of making an app that chooses a random part for each slot in the vehicle. Imagine how many crazy contraptions will be made from that! Sadly, I don't know how to code apps. Is there anything that will direct me in the right way?
     
    • Like Like x 7
  2. SuperShep1

    SuperShep1
    Expand Collapse

    Joined:
    Apr 14, 2019
    Messages:
    2,682
    very good idea
     
    • Agree Agree x 6
  3. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,053
    I would do it if I knew how to but sadly I have no idea about apps
     
    • Agree Agree x 1
  4. Penguin72

    Penguin72
    Expand Collapse

    Joined:
    Dec 11, 2020
    Messages:
    729
    If you want, you could use the wiki to find how to make a mod like that but it's kinda hard to learn anything from there.
     
    #4 Penguin72, Apr 7, 2021
    Last edited: Apr 7, 2021
  5. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    I think the best way is to look at other people's UI app mods and the game's own UI apps (that's basically how I learned). This wiki article basically highlights the basic structure of the app: https://wiki.beamng.com/My_First_App

    I found some files that deal with vehicle customizing here:
    • "BeamNG.drive\ui\modules\vehicleconfig\vehicleconfig.js"
    • "BeamNG.drive\lua\ge\extensions\core\vehicle\partmgmt.lua"

    And I found some functions/variables in them that probably will be helpful to get and set config parts:
    Code:
    --Gets the current vehicle's config
    local config = extensions.core_vehicle_partmgmt.getConfig()
    
    --Contains parts of vehicles
    config.parts
    
    --Sets the current vehicle with the new config
    extensions.core_vehicle_partmgmt.setPartsConfig(config.parts, true)
    
    Here's an example of getting the parts list of the ETK800, removing the FL door, and applying the new config (you can use GameEngine Lua console to execute these functions):

    Code:
    --Get current config of ETK 800
    config = extensions.core_vehicle_partmgmt.getConfig()
    
    --Sets FL door to empty
    config.parts.etk800_door_FL = ""
    
    --Set new config
    extensions.core_vehicle_partmgmt.setPartsConfig(config.parts, true)
    
    
    Good luck! :)
     
    • Like Like x 1
  6. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    Thanks for the info, but it doesn't seem to work for me. No new configs get generated, nothing happens.
    Also here's some things i need to figure out.
    • Get current vehicle name
    • Get all parts (table.insert)
    • Cycle through each part (for i, v in ipairs(config.parts) do?)
    • Edit: Also get all available part options for the part. Is there a way to make a table including all part options?
    • Choose random part (Maybe math.random? I don't know how to make it choose a random part though.
     
  7. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    Its not supposed to generate any new config files anyways. Did you run those 3 commands in the GE-Lua (GameEngine Lua) console since it doesn't work in Vehicle Lua console?

    Code:
    --Gets vehicle's name
    be:getPlayerVehicle(0):getJBeamFilename()
    
    --Cycle through each slot
    for slot_name, curr_part in pairs(config.parts) do
      print(slot_name)
    end
    
    --Get all available parts
    local all_parts = require('jbeam/io').getAvailableParts(extensions.core_vehicle_manager.getPlayerVehicleData().ioCtx)
    
    --Get random part
    local keyset = {} 
    for k in pairs(all_parts) do  
      table.insert(keyset, k)
    end
    local random_part = all_parts[keyset[math.random(#keyset)]]
    
    
     
  8. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    Woops, thought I had to run all the code at once.

    Also how would I be able to fit this code into an app? I've noticed that app code runs on a different language (JavaScript), is there a way to make it load the lua inside the js?
     
  9. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    You can call GameEngine Lua commands with JS using the following (they need to be inserted as a string):

    Code:
    --This runs game engine lua command
    bngApi.engineLua('the game engine lua command')
    
    --This runs game engine lua command and retrieves it returned value
    bngApi.engineLua('the game engine lua command', function(data) {
      var val_returned = data
    });
    
     
  10. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    o crap
    this is harder than I imagined
    so each line has to be that?
     
  11. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    Well you could create a separate Lua file and do most of the Lua logic there by putting all these Lua commands into larger functions and call those functions from JS so you wouldn't have to make so many bngApi.engineLua JS calls.

    So create a folder directory in the mod folder like so: "your_mod_name/scripts/your_mod_name" and create two Lua files in there:
    • modScript.lua
    Code:
    registerCoreModule('scripts/your_mod_name/extension')
    • extension.lua
    Code:
    local M = {}
    
    local function someFunction()
    end
    
    M.someFunction = someFunction
    
    return M
    And then you would call these Lua functions in JS like so:

    Code:
    bngApi.engineLua('scripts_your__mod__name_extension.someFunction()')
     
  12. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    so I would put the random part code inside somefunction?
     
  13. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    Yeah basically or however you want to structure the code to have more public interfaces (more someFunction functions) to call if you need to or to have more local functions to organize the code.
     
  14. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    i have no idea what i'm doing
    the button wont appear and im getitng this
    heeelp.png
    the app is called crazycontraptions
    .js
    Code:
    angular.module('beamng.apps')
    .directive('crazycontraptions', ['bngApi', function (bngApi) {
      return {
        template:  'modules/apps/crazycontraptions/template.html',
        replace: true,
        restrict: 'EA',
        link: function (scope, element, attrs) {     
          scope.scrambleconfig = function () {
            bngApi.engineLua('scripts_crazycontraptions.test()')
          };
        }
      };
    }])
    .json
    Code:
    {
      "name" : "Crazy Contraptions",
      "author": "beamng",
      "version": "0.2",
      "description": "Creates a crazy thing out of random parts.",
      "directive": "crazycontraptions",
      "domElement": "<crazycontraptions></crazycontraptions>",
      "css": { "width": "200px", "height": "200px", "min-width": "100px", "min-height": "100px", "top": "200px", "right": "200px"}
    }
       
    .html
    Code:
    <div style="height:100%; width: 100%;"layout="column">
      <md-button ng-click="scramble('scrambleconfig');" class="md-raised md-primary" flexmd-no-ink>Scramble Config</md-button>
    </div>
     
  15. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    You had some missing ending brackets so it couldn't compile. And I'm not so sure what your whole folder directory looks like so just in case you did something wrong I added a zip file of what it should look like.
     

    Attached Files:

  16. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    ok I got the button to show up now it doesn't want to work
    in the lua I told it to print slot_name and all_parts
    but nothing happens
    no error
    help?
    (btw i used some code from bread's random spawner)
     

    Attached Files:

  17. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    In the html file, the md-button ng-click part is written wrong so change it to:
    Code:
    <md-button ng-click="scrambleconfig()" class="md-raised md-primary" flexmd-no-ink>Scramble Config</md-button>
     
  18. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    doesn't do it
    in the lua i told it to print something
    but it doesn't print
    help?
    a
     

    Attached Files:

  19. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    I downloaded the zip and it still has the old html code. It worked for me when I used

    Code:
    <md-button ng-click="scrambleconfig()" class="md-raised md-primary" flexmd-no-ink>Scramble Config</md-button>
    instead of

    Code:
    <md-button ng-click="scramble('scrambleconfig');" class="md-raised md-primary" flexmd-no-ink>Scramble Config</md-button>
     
  20. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    weird, thought i put that there

    also what does "random_part" do? if it just selects a random slot in the part list then i don't need that, i need it to go through all available parts, then for each slot, select a random part for that slot.
     
  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