PROXIMITY DETECTION FOR EVENTS

Detecting to see if the Hero is within a given radius of an event

• kentona
• 06/28/2007 12:00 AM
• 6886 views
This simple tutorial will show you how to code a parallel event that detects whether or not the hero comes within a specified radius relative to the center of the event.

This detecting event can be used in a number of situations, including (but not limited to) monster movement AI, detecting traps, NPC movement or finding hidden items.

The code itself is based on simple circle geometry.

You need:
3 Variables
1 Event

Steps:
1. Create a new event
2. Set the event as a Parallel Process
3. Create 3 Variables: LocationX and LocationY and Radius
4. Insert this code in the event's code window:

``` LocationX: Set, This Event X Coord.
LocationY: Set, This Event Y Coord.
LocationX: -, Hero X Coord.
LocationY: -, Hero Y Coord.
LocationX: *, V0001 LocationX
LocationY: *, V0002 LocationY
LocationX: +, V0002 LocationY
: Branch If LocationX is V0003 Radius  Less/Equal
:  COMMENT: Hero is within a radius of 5 of the event
: Else Handler
:   COMMENT: Hero is outside of the radius
: End
```

The formula is:
(X_event - X_hero)^2 + (Y_event - Y_hero)^2 lessthan_equalto Radius^2

I arbitrarily used 5 as my test radius, but it can be set to whatever you prefer. The event will "trigger" when the hero comes within 5 tiles of the event in a rough circle around the event.

Expanding on this concept:
For monster AI, you could have your code inside of a monster's event, and if the hero comes within the monster's detection radius, have him start running towards the hero. The same idea can be applied to NPCs.

For an Awareness type ability, the event can be set as invisible to start with, but once within the radius the code can change the event's graphic to indicate that an item/object is found. For traps, you could implement a trap difficulty variable.

For example, say the Hero has an Awareness ability of 5, and the Trap's difficulty is 3. In the above code, you could change it so that the Radius varible is set to the Hero's awareness minus the Trap's difficulty:

...etc...

So now the Hero has to come within 2 tiles to detect the trap. If the Trap's difficulty is greater than the Hero's Awareness, set the Radius to zero:

: Branch If Radius Less Than 0
: End
...etc...

Conclusion
Happy detecting and thanks for reading!

Posts

Pages: 1
Erynden
Gamers don't die, they respawn.
1702
How would you code it to make it so that when/if the monster event touches the hero, a battle begins?
Erynden
Gamers don't die, they respawn.
1702
Sorry, for both double posting and not being specific. Like, with you code, how would it be coded if you had it to where when the monster detects you within that radius he goes after you, but if the hero and that monster event touches a battle starts as oppose to evading the event and not have a battle.

Cause I want the monster to go after the hero within the radius, but give the hero a chance to evade the monster and not battle once he is within that radius.
kentona
I am tired of Earth. These people. I am tired of being caught in the tangle of their lives.
21227
@Jakester: You could have the ~insert your code here~ portion turn on a switch, and on page 2 have it enabled by the switch, and just make it a normal "Move Toward Hero" event, On Hero Touch, and in the code section have "Start Battle: Slime". Page 1 would just have the monster sitting there.

Also, you would have the proximity code in a separate Parallel event. So the first part would be:

LocationX: Set, Slime Event X Coord.
LocationY: Set, Slime Event Y Coord.

and the second section:

: Branch If LocationX is V0003 Radius Less/Equal

: COMMENT: Hero is within a radius of 5 of the event

: Switch SlimeChasesYou! ON

: Else Handler

: COMMENT: Hero is outside of the radius

: Switch SlimeChasesYou! OFF

: End
Finally got around to using this. Thanks! My Star Trek doors work perfectly :D
pianotm
The TM is for Totally Magical.
26008
Never mind, I figured it out.
kentona
I am tired of Earth. These people. I am tired of being caught in the tangle of their lives.
21227
In my games that had button presses to trigger actions, I had a different common event as a parallel process. Was there something specific you were thinking?
pianotm
The TM is for Totally Magical.
26008
I figured it out! I just had to put a wait for key input process in the conditional branch.
Pages: 1