Event driven scripting?

At present, the only way I’m aware that a series of events can be scripted is with a deeply nested group of “if” statements. But in some instances, this can be pretty laborious and I’m wondering if there’s a better way to set this up. Take for example the following series of commands to an object:

  1. move forward
  2. stop when wall is reached
  3. turn left
  4. move forward
  5. start timer
  6. jump up when timer reaches 5 seconds
    … and so forth

Although the actions aren’t all that complex, it still requires a fair number of “if” statements so that if I wanted a user to be able to modify step 3 on the fly to “turn right”, the code would get pretty involved.

Is there a better way to accomplish this? (I’m using Javascript).

Thanks!

I would use a combination of triggers and yield statements… no Ifs needed.

Thanks Randy…

I’ve made a pretty modular AI script. There are four base classes that extend each other and successively add more complex functionality.

After those four classes there are multiple branches that configure the AI differently and override methods to more radically change functionality.

This worked pretty well and allowed for much flexibility. All actions are split into methods and I first define a general approach but can override it in any derived class to modify it.

I also introduced some pseudo-events. Basically just methods that get called at certain points in the script and that can be overridden in derived classes to hook in other functionality.

The downside is that overriding doesn’t work in javascript and the Unity editor doesn’t like it. You’ll have to make all your properties private or protected and modify them in the script.

Sure, Does that give you enough to go by?

It was enough to nudge me in the right direction! :wink:

Adrian - Sounds like you’re already sorted out something like what I’m thinking about, although it also sounds like your implementation is more sophisticated than what I’m hoping to achieve.

If it was my problem, i would Enum the actions and then Switch them pointing to fuctions directly to the objects envolved…

That way you centralize your actions but isolate their implementations in the right objects.

I don’t know… without the actual problem is hard to propose a good solution.

.org