Solved Player car in scenario changing ID

Discussion in 'Mod Support' started by Darthbob555, Aug 8, 2016.

  1. Darthbob555

    Darthbob555
    Expand Collapse

    Joined:
    Feb 14, 2016
    Messages:
    283
    To get multiple AI working in my scenario, I have used this command in the ,JSON file:

    Code:
    "command":"ai.setTargetVehicleID(2465)"
    This works very well and is exactly what I wanted it to do. The parameter given is the player Id so that the vehicles (AI) chase the player and not themselves. However, the player ID keeps changing when I restart the scenario, so I cannot replicate the AI chasing me without then changing the number in the .JSON every time I want to load up the scenario.

    Is this a bug or am I accidentally randomizing the player ID in some way?
    --- Post updated ---
    Upon further investigation, it seems that it randomly reasigns the IDs on the scenario load up, sometimes the correct ones, sometimes another.

    How can I make sure that the ID of the player vehicle is the same or can I set the target for the ai to be something like "getPlayerId()" - so that any ID that the player vehicle is set to will be targeted.
     
    #1 Darthbob555, Aug 8, 2016
    Last edited: Aug 9, 2016
  2. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,619
    Something similar to this would work in a Lua script:
    Code:
    "command":"ai.setTargetVehicleID(scenetree.findObject("scenario_player0"))"
    scenetree.findObject() with whatever the name of the player vehicle is should get you the ID. Exactly how it should be formatted for the JSON scenario file isn't something I've played with.
     
  3. Darthbob555

    Darthbob555
    Expand Collapse

    Joined:
    Feb 14, 2016
    Messages:
    283
    Thanks for the reply :). I played around with it in the .JSON but couldn't get it to work.

    You said that it works in Lua script, which would still do the trick if I call it on scenario startup. I couldn't get it to work in the Lua file so i was wondering if you could give me an example of what you meant.
     
  4. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,619
    Sorry, I definitely gave you the wrong code. That function returns an object, you want an ID...

    Instead let's try this:
    be:getPlayerVehicleID(0)

    That isn't exactly what we want either. We want to know an object's name and set it to the AI target based on that information. So we must go from knowing the name, to finding the object, to finding the ID of the object, and finally to setting the ID.

    I believe that this should work in your JSON file:
    Code:
    "command":"ai.setTargetVehicleID(scenetree.findObject("scenario_player0"):getID())"
    Edit: If not, post up or PM your stuff. I don't have a scenario handy to play with this stuff in.
     
  5. Darthbob555

    Darthbob555
    Expand Collapse

    Joined:
    Feb 14, 2016
    Messages:
    283
    Thank you for your response :). I tried this and it wouldn't allow me to startup ANY scenario. As expected, it didn't like the quotation marks being put in four times and so I replaced them with apostrophes, to no avail.

    Here is my testing scenario, attached below. Let me know if you need anymore information.
     

    Attached Files:

  6. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,619
  7. Darthbob555

    Darthbob555
    Expand Collapse

    Joined:
    Feb 14, 2016
    Messages:
    283
    I agree - it does look like a bug as that should either work in both of neither case, not one or the other. Thank you for your support :)
     
  8. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,619
    Unfortunately what you want to do seems to require some fancy footwork.
    • "Command" clearly runs inside the Vehicle Lua VM
    • AFAIK the functions you need to determine the vehicle ID for the target are all inside the GE Lua VM
    • getting data back and forth is hard: 1 / 2
    Moving this into a Lua script for the scenario won't make it less complex, but it will at least avoid the quoting problem I mentioned above.
     
  9. Darthbob555

    Darthbob555
    Expand Collapse

    Joined:
    Feb 14, 2016
    Messages:
    283
    So you're saying that it would be easier (by way of avoiding the above problem) to use Lua?

    I tried some commands in Lua script earlier today:

    Code:
    
    local function onPreRender(dt)
        local ai1 = scenetree.findObject("scenario_player1")
        ai1:queueLuaCommand('ai.setTarget("scenario_player0")')
    end
    
    return {
        onPreRender = onPreRender
    }
    I know that Scepheo managed to do something like this in his demolition derby events where the game would choose whether to make an AI car follow a certain node or another car (find attached file - trimmed to single method with the relevant information). Not quite sure how it works but I believe he gets the object of the player/another AI car, gets its position and sets the AI target to that position.

    Might be worth looking it over?
     

    Attached Files:

  10. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,619
    setTarget appears to be intended for use with waypoints (it's undocumented, like most functions). Whatever Scepheo was doing doesn't work right now - see Scepheo's post here: http://www.beamng.com/threads/mazes.28804/#post-415533 If Scepheo is frustrated trying to get his method to work, I'm not making an attempt myself. Scepheo is much more knowledgeable about these things than I am.

    With that said, we're actually not far off from having the functions I suggested work if we do it inside a Lua file.
    • ... and to be fair, it was easier just to borrow code from @Scepheo's latest demolition derby. :) :-D
    • I borrowed only the bare minimum of code to achieve what you want.
    • I did leave in the redundant setting of AI mode... you can delete it from the JSON or from the LUA. I wouldn't leave it that way but it works. (aiAggression is also set in Lua although it could be set in the JSON. Set this stuff wherever you want, just don't set it twice.)
    • I modified you prefab so that the other cars would be named "opponent1" and etc to fit with Scepheo's code, which I think promotes clarity.
    • I also deleted one of the cars - you can put it back. 4 cars runs better than 5 for me. (It's a moot point anyway until other things are done - the AI just crashes into the center.) If you add a 4th opponent back in be certain to increase the "opponentCount" variable in the Lua.
    • Note that this code runs in "onRaceStart" and not "onPreRender". onRaceStart is the appropriate place for this.
     

    Attached Files:

    • Like Like x 1
  11. Darthbob555

    Darthbob555
    Expand Collapse

    Joined:
    Feb 14, 2016
    Messages:
    283
    I thank you very much for this :). It seems to work perfectly and I couldn't have asked for more. I thank you for your time and effort and appreciate your time to help me :)

    (case solved)
     
  12. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,619
    You're welcome! I recommend dropping Scepheo a line to say how his code helped you / us - and to make sure he's happy with the borrowing. ;-)
     
  13. Darthbob555

    Darthbob555
    Expand Collapse

    Joined:
    Feb 14, 2016
    Messages:
    283
    Of course, was planning to :)
     
    • Like Like x 1
  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