Entfernung zwei Personen - Distance between two characters

  • #1, by thomas-fringsFriday, 16. April 2021, 08:47 3 years ago
    I'm just taking my first steps with Visionaire and Lua. Lua is not easy to understand. I could not find a simple solution in the forum.

    How can I determine the distance between two characters and then start an action?

    My first thought would be:
    function queryDistance()
    
     Person_A = CharacterPosition from Person A
     Person_B = CharacterPosition from Person B
    
     distance = (Person_A - Person_B)
    
      if distance <= 50 then
       startAction("Actions[]")
      end
     end
    registerEventHandler("mainLoop", "queryDistance")

    What would the script look like? I would be glad to receive an answer.


    Deutsch:
    Ich mache gerade meine ersten Schritte mit Visionaire und Lua. Lua ist nicht gerade einfach zu verstehen. (im Gegensatz zu einer C#-Programmierung in Unity)

    Wie kann ich die Entfernung zwischen zwei Charakteren ermitteln, um anschließend eine Aktion zu starten?


    Wie würde das Skript dazu aussehen? Über eine Antwort würde ich mich freuen.

    Newbie

    21 Posts


  • #2, by esmeraldaFriday, 16. April 2021, 09:06 3 years ago
    I suck at lua (and scripting in general) so you will have to wait for some else to help you here.
    But you can take a look at the luadocs, if you haven't already:

    The position of a character should be something like: Characters["Person_A"].Position


    Are both of the characters moving? If not - I would go the easy route and use an action area. When the moving character enters the area start the action.

    Edit:
    ... just did a little check:

    function queryDistance()
     local Person_A = Characters["Person_A"].Position
     local Person_B = Characters["Person_B"].Position
    local distance_x = Person_A.x - Person_B.x
     local distance_y = Person_A.y - Person_B.y
      if distance_x <= 50 and distence_y <= 50 then
       startAction("Actions[]")
      end
     end
    registerEventHandler("mainLoop", "queryDistance")


    should work. But you would need to add a condition to stop the loop

    Key Killer

    513 Posts

  • #3, by thomas-fringsFriday, 16. April 2021, 09:59 3 years ago
    I had that idea with "action area", too. 

    But in my case, both characters move.

    Newbie

    21 Posts

  • #4, by thomas-fringsFriday, 16. April 2021, 10:16 3 years ago
    Hey, not bad! You've done that well.

    I have tried the code and it works. I have also implemented a condition to stop.

    The value of the distance also seems to go negative. But the value should always be positive. Otherwise the action is started by the negative value.

    Newbie

    21 Posts

  • #5, by esmeraldaFriday, 16. April 2021, 10:17 3 years ago
    better make it:
    local distance_x = math.abs(Person_A.x - Person_B.x)
    and the same for _y else it will be triggered at any distance if the result is negative

    Key Killer

    513 Posts