Advertisement

scripting, triggers and events

Started by July 01, 2011 06:45 PM
4 comments, last by Ashaman73 13 years, 7 months ago
[font="Verdana"]Hello! [/font]
[font="Verdana"]
[/font]
[font="Verdana"]I'm pretty new to AI programming, but I am familiar with event driven development. My game currently has a system for Behavior action events which can be anything that defined a behavior of an object. For example, AnimationBehavior, MovementBehavior, IdleBehavior, AttackBehavior...etc. I am also currently using javascript in some small places, but I decided from the beginning that I was going to eventually implement a much game logic as possible via script...to make it easier for others to work on without them needing a full IDE environment and compiler. I will admit that I do still get a bit confused with event driven development at times, so I'm sure that my trouble right now is probably because of that..but really what I am trying to figure out is WHERE things should be..[/font]
[font="Verdana"]
[/font]
[font="Verdana"]For example, let's say I want a zombie to roam around until he spots the player (or a living human). Now, I figured that this is a local event to the zombie itself which can be handled via a behavior class such as "LookForHumans" so once the behavior spots a human, it sets the zombies target and notifies it that a human is in site, which then, the zombie (which is the controller?) will then react to the event by say, starting it's MovementBehavior with the targets position as a parameter for location (of course this would get updated every ai think tick). Once the movement is satisfied (ie. the distance between the zombie and the destination is within range) the movement behavior then sends event to the zombie... Now...here's one place I'm confused.. [/font]
[font="Verdana"]
[/font]
[font="Verdana"][/font]
[font=Verdana]if(event == movement) attack_behavior.enable()[/font]
[font=Verdana]
[/font]
[font=Verdana]
[/font]
[font=Verdana]but what if there is no target? Attack makes no sense. My though is this, though this is probably obvious... Simply create an instance of MovementBehavior called "move_to_target" which when it's received in the zombies event handler, it knows it's finally caught the target. The attack behavior can then simply notify the zombie again if the target is null. [/font][font=Verdana]Am I right in my think here as far as the basic AI goes? This in theory, will all be handled via script.[/font]
[font=Verdana]
[/font]
[font=Verdana]Now, triggers and events... A TriggerEvent may be fired by another game object, such as a tile on the ground, when touched, would send a TriggerEvent like so:[/font]
[font=Verdana][/font]
[font="Verdana"]broadcast(new TriggerEvent("activate_poison_darts", this) // where as this refers to the (activator?)[/font]
[font=Verdana]

[/font]
[font=Verdana]
[/font]
[font="Verdana"]Several questions here...[/font]
[font=Verdana]
[/font]
[font=Verdana]My thinking is that, this would register the event with the AIManager which would then pass it along to ALL other actors. When the poison darts (or the object that shoots them) sees the event "activate_poison_darts" it then runs the method that fires the poison darts. This of course would definitely be a scripted event. However, where would this event be defined?? Should their be just a single script file for each level in which all logic for that level is defined? Also, in a case like the above, HOW will the script know when the object was touched?? It would need to listen for an event to be received it's self.. So using my behavior system...maybe have a ScriptedBehavior class and then exposed them to the script at run time? Hmmm.....[/font]
[font=Verdana]
[/font]
[font="Verdana"]Also, what about game events such as "player_entered_end_zone"? Should that possible be done like: broadcast_game_event(...) in which a WorldListener listens for?[/font]
[font="Verdana"]
[/font]
[font="Verdana"]I know this is probably quite a lot to tackle. I've been researching all of this for the past few days, but these are the things I was just really confused on how I might go about implementing...so thanks a ton if you help!![/font]
[font=Verdana]
[/font]
[font=Verdana]
[/font]
[font="Verdana"]Just realized I should probably also create several types of trigger events within Java such as touch, time, sight, location, noise, etc...these can then be exposed to the script.[/font]
Advertisement
If you want to model your AI after event-driven techniques, your best bet is either a Finite State Machine (FSM) or Hierarchical Finite State Machine (HFSM) as these will most closely approximate what you're used to doing (HFSMs slightly less so, as they're a bit more complex than just event-driven state machines).

However, I would submit that this is actually a rather awkward and inefficient way to structure your AI. For the kinds of examples you give, you might want to look into Behavior Trees instead, which should give you exactly the blend of flexibility, composition, and reuse that you're looking for. Best of all, BTs are easy to integrate with scripting solutions if you're a little careful up front.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

That's awesome! Thank you! So I just read a small introduction here. It seems to me that behaviors trees are perfect for local decision making. However, I still believe that a trigger/event system would still be needed for actor/actor communication..Indiana Jones removes the golden statue which triggers the release of the ball. I guess however, the "ball" isn't an actor, however as it doesn't behave any certain way (physics could control the ball entirely). However, suppose a soldier actor spots a target and should tell the rest of his squad...he'd generate a "suspicious" event which may tell the other soldiers to follow the activator and be on guard...so basically, use the event to control which behavior node is used?? Hmm.
Event interactions and AI behaviours are orthogonal problems.

Sure, if the golden idol is moved, it needs to trigger an event which wakes up the evil zombie guards; but the BT (or whatever) that controls the evil zombie guards doesn't have to be event-driven internally.

You have a couple of choices, which can actually be blended fairly easily if you really need to (depends on your game design): either have the BT change nodes based on triggered external events, or use a knowledge representation whereby the AI agent periodically examines the world for interesting stuff. In the former case, when the idol is stolen, you tell the BT to change modes (although a FSM might be cleaner here depending on what precisely you want to implement). In the latter case, the BT includes a mode where it occasionally looks to see if the idol was stolen, and then decides internally to change modes. Either approach is valid, and again you could easily combine them if you needed to.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Some words about BTs and external triggers. A pretty neat approach to handle external events is to use stimuli (see Chris Hecker notes about BT in spore).
A stimulus is in fact a temporary "flag" which indicates that something has happened. This stimulus could be set by external triggers and the next time the BT is processed it will react depending on available stimuli and other internal states.

I would solve the soldier/suspicion problem by letting each squad member, who see something suspiciously, broadcast a "be_suspicous" stimulus with a 1 minute timelimit to the squad(event). Each soldier will scan the surrounding carefully as long as he got the "be_suspicous" stimulus (done in the BT).

This way you can easily integrate asynchronously event handling in BTs.

This topic is closed to new replies.

Advertisement