how to compare position between mc and other characters?

  • #1, by nerdThursday, 13. December 2018, 09:36 5 years ago
    Since I'm trying to make a basic beat em up system, I'd need a way to compare mc and opponent position so that if mc is punching and y x cordinate beetween him and an opponent are between a determined range the punch triggers a pain animation to the opponent. How can I achieve this with a script?

    Forum Fan

    147 Posts


  • #2, by afrlmeThursday, 13. December 2018, 13:07 5 years ago
    definition script
    function compareCharDist(atk, def)
    
     if Characters[atk].Position.x < Characters[def].Position.x and (Characters[def].Position.x - Characters[atk].Position.x) <= 100 then
    
     -- hit (attacker is on left)
    
    elseif if Characters[atk].Position.x > Characters[def].Position.x and (Characters[atk].Position.x - Characters[def].Position.x) <= 100 then
    
     -- hit (attacker is on right)
    
     end
    
    end

    usage: (execute a script)
    compareCharDist("Tom", "Harry")

    Imperator

    7278 Posts

  • #3, by NigecThursday, 13. December 2018, 13:18 5 years ago
    I've been trying that setDelay example to see if I could detect when a character is in range and it seems to work well BUT I'm really not sure if its a good idea to have it running for an extended period of time also the range depends on how fast the delay runs.
    a delay of 250ms is enough if you check for a a character 50px away..

    Key Killer

    627 Posts

  • #4, by nerdThursday, 13. December 2018, 13:27 5 years ago
    Thank you I am gonna try it

    Forum Fan

    147 Posts

  • #5, by afrlmeThursday, 13. December 2018, 13:48 5 years ago
    I've been trying that setDelay example to see if I could detect when a character is in range and it seems to work well BUT I'm really not sure if its a good idea to have it running for an extended period of time also the range depends on how fast the delay runs.
    a delay of 250ms is enough if you check for a a character 50px away..

    Hence check directly via the relevant animation frame. Why bother with delays, when you can sync much easier by checking on the relevant animation frames?

    Imperator

    7278 Posts

  • #6, by nerdThursday, 13. December 2018, 14:26 5 years ago
    Sorry for the naive question, but where do I write the definition script? Does it not go in "exectute script" right?

    Forum Fan

    147 Posts

  • #7, by afrlmeThursday, 13. December 2018, 15:22 5 years ago
    In the script section of the editor. create a new script. by default it will be a definition type script.

    There are 2 script types...
    1. Definition: scripts of this type will be executed on game load - useful for declaring variables & functions.
    2. Execution: scripts of this type will be executed when called - useful for small/simple single use scripts.

    Imperator

    7278 Posts

  • #8, by afrlmeThursday, 13. December 2018, 15:25 5 years ago
    In the script section of the editor. create a new script. by default it will be a definition type script.

    There are 2 script types...
    1. Definition: scripts of this type will be executed on game load - useful for declaring variables & functions.
    2. Execution: scripts of this type will be executed when called - useful for small/simple single use scripts.

    Quick note: the script I pasted was an example. It won't do anything unless you edit it & tell it what it's supposed to do. Such as play hurt animation of the opponent... maybe something along the lines of this...
    startAnimation(Characters[def].CurrentOutfit.CharacterAnimations["hurt"])

    Imperator

    7278 Posts

  • #9, by nerdFriday, 14. December 2018, 09:15 5 years ago
    function compareCharDist(MC,opponent)
    
    NearOpponentStandingRight =  getObject("Conditions[NearOpponentStandingRight]")
    NearOpponentStandingLeft =  getObject("Conditions[NearOpponentStandingLeft]")
    
    
     if Characters[MC].Position.x < Characters[opponent].Position.x and (Characters[opponent].Position.x - Characters[MC].Position.x) <= 100 then
    
    
    setCondition("nearEnough", true)
    NearOpponentStandingLeft:setValue(VConditionValue, true)
    NearOpponentStandingRight:setValue(VConditionValue, false)
    -- hit (attacker is on left)
    
    elseif
    
    
    Characters[MC].Position.x > Characters[opponent].Position.x and (Characters[MC].Position.x - Characters[opponent].Position.x) <= 100 then
    
    
    setCondition("nearEnough", true)
    NearOpponentStandingRight:setValue(VConditionValue, true)
    NearOpponentStandingLeft:setValue(VConditionValue, false)
    
     
    -- hit (attacker is on right)
     end
    end
    
    
    
    
    compareCharDist("MC", "opponent")

    I was wondering if it's ok, I added two variables that switch once the distance is in range.
    I added the execute script to a keyboard input to run it and check if the variable switch but it doesn't work. I also substitute the character names of the function, do they need quotation marks?

    edit: ignore "setCondition("nearEnough", true)" which I paste/copied by error

    Forum Fan

    147 Posts

  • #10, by nerdFriday, 14. December 2018, 10:01 5 years ago
    function distanceScriptTry (mc, opponent)
    
    standingRight =  getObject("Conditions[standingRight]") 
    standingLeft =  getObject("Conditions[standingLeft]")
    mc = getObject("Characters[MC]")
    opponent = getObject("Characters[opponent]")
    
    
    
    if mc.Position.x + opponent.Position.x >=1 
    
    and
    
    mc.Position.x + opponent.Position.x <=300
    
    then
    
    standingRight:setValue(VConditionValue, true)
    
    else
    
    standingRight:setValue(VConditionValue, false)
    
    end
    
    if mc.Position.x - opponent.Position.x <=-1 
    
    and
    
    
    mc.Position.x - opponent.Position.x >=-300
    
    then
    
    standingLeft:setValue(VConditionValue, true)
    
    else
    
    standingLeft:setValue(VConditionValue, false)
    
    end
    end
    
    
    I also made this scritp which works for the left side, the standingLeft variable works and switches when distance is on range, but the standingRight variable doesn't work

    Forum Fan

    147 Posts

  • #11, by nerdFriday, 14. December 2018, 10:36 5 years ago
    This works:

    function distanceScriptTry (mc, opponent)
    
    standingRight =  getObject("Conditions[standingRight]") 
    standingLeft =  getObject("Conditions[standingLeft]")
    mc = getObject("Characters[MC]")
    opponent = getObject("Characters[opponent]")
    
    
    if mc.Position.x < opponent.Position.x
    
    and
    
    mc.Position.x - opponent.Position.x >=-300
    
    then
    
    standingLeft:setValue(VConditionValue, true)
    
    else
    
    standingLeft:setValue(VConditionValue, false)
    
    end
    
    if mc.Position.x > opponent.Position.x 
    
    and
    
    mc.Position.x - opponent.Position.x <=300
    
    then
    
    standingRight:setValue(VConditionValue, true)
    
    else
    
    standingRight:setValue(VConditionValue, false)
    
    end
    end
    
    

    Forum Fan

    147 Posts