WIP Beta released Part Randomizer...?

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

  1. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    It selects a random part from all the available parts you can put on the vehicle (not parts list from current vehicle).
     
    • Like Like x 1
  2. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    Wait, does it select a random part on the current part?
     
  3. SuperShep1

    SuperShep1
    Expand Collapse

    Joined:
    Apr 14, 2019
    Messages:
    2,682
    so can i download this and use it or is it just code at the moment
     
  4. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    It selects a random part from all the possible parts for the vehicle. Its not getting parts from the current vehicle part list.
     
  5. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    it's only just testing code
     
  6. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    Wait, all possible parts from the vehicle? I want it so it chooses a random selection from the current part. I don't want a turbo set as the door, lol.
    Also my lua doesn't seem to be working. I'm creating the magic that sets the random stuff, but nothing happens. The car does reload, though. I looked in the console and it just appeared to be spawning a stock pickup. Fix...?
    Code:
    local M = {}
    
    local function rolldice()
        print("if you see this in console that means this works")
        --Gets vehicle's name
        local carname = be:getPlayerVehicle(0):getJBeamFilename()
    
        local config = extensions.core_vehicle_partmgmt.getConfig()
    
        --Cycle through each slot
        for slot_name, curr_part in pairs(config.parts) do
          print(slot_name)
          --Get all available parts
            local all_parts = require('jbeam/io').getAvailableParts(extensions.core_vehicle_manager.getPlayerVehicleData().ioCtx)
            print (all_parts)
    
            --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)]]
            config.parts.slot_name = random_part
        end
        extensions.core_vehicle_partmgmt.setPartsConfig(config.parts, true)
    end
    
    M.rolldice = rolldice
    
    return M
     
    #26 umustbeloggedintododat, Apr 9, 2021
    Last edited: Apr 9, 2021
  7. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    All the parts that are available for their slots I think. So every part of the vehicle could change into another part that is available instead of it.
     
    • Like Like x 1
  8. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    Wait nvm I think I'm getting there, but I still don't got it. It says it can't find the part "table: (insert random 64-bit string here)" wasn't found. Why is it finding some strange 64-bit string instead of a part name?
    Code:
    local M = {}
    
    local function rolldice()
        print("if you see this in console that means this works")
        --Gets vehicle's name
        local carname = be:getPlayerVehicle(0):getJBeamFilename()
    
        local config = extensions.core_vehicle_partmgmt.getConfig()
    
        --Cycle through each slot
        for slot_name, curr_part in pairs(config.parts) do
          print(slot_name)
          --Get all available parts
            local all_parts = require('jbeam/io').getAvailableParts(extensions.core_vehicle_manager.getPlayerVehicleData().ioCtx)
            print (all_parts)
    
            --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)]]
            config.parts[slot_name] = random_part
        end
        extensions.core_vehicle_partmgmt.setPartsConfig(config.parts, true)
    end
    
    M.rolldice = rolldice
    
    return M
     
  9. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    ?
     
    • Agree Agree x 1
  10. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Okay let me explain again. So there is a list of all the possible parts that you can put on the vehicle. And that code randomly chooses a part from that list.

    So the numbers you are seeing is the memory address of a Lua table. So if you want to look at the contents of the table, use the dump() function. If you want to get a random part for a specific slot, then you should filter through the "all_parts" table for parts that have that specific slot and create a subtable of said parts. And then randomly pick a part from that subtable. Use the dump() function to see how the table is structured to figure out how to implement this. I've basically given you everything you need so you should be able to implement the rest of the logic.
     
    #30 angelo234, Apr 10, 2021
    Last edited: Apr 10, 2021
  11. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    sorry I said "?" because you weren't responding
    I'm not that experienced at beamng lua, or lua in general.
    I know how to use pairs, but how would I be able to filter the slot? BeamNG lua is so confusing to me.
     
    #31 umustbeloggedintododat, Apr 10, 2021
    Last edited: Apr 10, 2021
  12. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Well I think it would be best to learn some Lua before coding then: https://www.tutorialspoint.com/lua/index.htm

    --- Post updated ---
    Actually you should use this command instead as it actually gives you what parts you can be put in each vehicle's slot.
    Code:
    local all_slots = require('jbeam/io').getAvailableSlotMap(extensions.core_vehicle_manager.getPlayerVehicleData().ioCtx)
    So all you need to do is to pick a random part for each slot.
     
    #32 angelo234, Apr 11, 2021
    Last edited: Apr 11, 2021
  13. pss5156

    pss5156
    Expand Collapse

    Joined:
    May 7, 2017
    Messages:
    149
    Make sure it doesn't select 'empty' by random :p
    Some parts will be a disaster and won't even make a vehicle start, so at least make a match like, diesel engine - diesel fuel tank stuff.
     
  14. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    --- Post updated ---
    Actually you should use this command instead as it actually gives you what parts you can be put in each vehicle's slot.
    Code:
    local all_slots = require('jbeam/io').getAvailableSlotMap(extensions.core_vehicle_manager.getPlayerVehicleData().ioCtx)
    So all you need to do is to pick a random part for each slot.[/QUOTE]
    how would I get it to work for slot_name?
    --- Post updated ---
    also it doesn't work. table:dump() doesn't seem to exist.
    no idea if this is correct or not
    Code:
    local M = {}
    
    local function rolldice()
        print("if you see this in console that means this works")
        --Gets vehicle's name
        local carname = be:getPlayerVehicle(0):getJBeamFilename()
    
        local config = extensions.core_vehicle_partmgmt.getConfig()
    
        --Cycle through each slot
        for slot_name, curr_part in pairs(config.parts) do
          print(slot_name)
          --Get all available parts
            local all_slots = require('jbeam/io').getAvailableSlotMap(extensions.core_vehicle_manager.getPlayerVehicleData().ioCtx)
            print (all_slots)
    
            --Get random part
            local keyset = {}
            for k in pairs(all_slots) do 
                table.insert(keyset, k)
            end
            local go = keyset:dump()
            local random_part = all_slots[go[math.random(#go)]]
            config.parts[slot_name] = tostring(random_part)
        end
        extensions.core_vehicle_partmgmt.setPartsConfig(config.parts, true)
    end
    
    M.rolldice = rolldice
    
    return M
     
  15. Phym

    Phym
    Expand Collapse

    Joined:
    Jan 1, 2020
    Messages:
    1,395
    Now that’s something I wanna see!
     
  16. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    yeah once i get this code working
     
  17. Phym

    Phym
    Expand Collapse

    Joined:
    Jan 1, 2020
    Messages:
    1,395
    Well, good luck, and may the programming gods be in your favor
     
  18. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    ayes
     
  19. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    how would I get it to work for slot_name?
    --- Post updated ---

    also it doesn't work. table:dump() doesn't seem to exist.
    no idea if this is correct or not
    Code:
    local M = {}
    
    local function rolldice()
        print("if you see this in console that means this works")
        --Gets vehicle's name
        local carname = be:getPlayerVehicle(0):getJBeamFilename()
    
        local config = extensions.core_vehicle_partmgmt.getConfig()
    
        --Cycle through each slot
        for slot_name, curr_part in pairs(config.parts) do
          print(slot_name)
          --Get all available parts
            local all_slots = require('jbeam/io').getAvailableSlotMap(extensions.core_vehicle_manager.getPlayerVehicleData().ioCtx)
            print (all_slots)
    
            --Get random part
            local keyset = {}
            for k in pairs(all_slots) do
                table.insert(keyset, k)
            end
            local go = keyset:dump()
            local random_part = all_slots[go[math.random(#go)]]
            config.parts[slot_name] = tostring(random_part)
        end
        extensions.core_vehicle_partmgmt.setPartsConfig(config.parts, true)
    end
    
    M.rolldice = rolldice
    
    return M
    [/QUOTE]

    dump() function is used to print out the contents of a variable (e.g. elements of a table). Use it like this:
    Code:
    dump(var_name)
    So make a subtable of available parts for a given slot, get random part from that subtable, and set the current slot to the part. Make use of the "dump" function to see how the tables are structured to see how to access elements of the table, so you can figure out how to code this logic.
     
  20. umustbeloggedintododat

    umustbeloggedintododat
    Expand Collapse

    Joined:
    Feb 16, 2019
    Messages:
    1,381
    dump() function is used to print out the contents of a variable (e.g. elements of a table). Use it like this:
    Code:
    dump(var_name)
    So make a subtable of available parts for a given slot, get random part from that subtable, and set the current slot to the part. Make use of the "dump" function to see how the tables are structured to see how to access elements of the table, so you can figure out how to code this logic.[/QUOTE]
    Alrighty, I see how it is structured...
    It's like a pc file.
    Code:
    "part", "part", "part" etc
    But it doesn't sort it by what part it belongs to. It's scrambled. I need some help.
    The problem is that I know how to code the basic lua logic, but I don't know how to code BeamNG lua. It's not clear to me, and I'm not using that outdated wiki again.
     
  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