| Home Page | Recent Changes | Preferences

Extending States

Some discussion shunted over from State...

Question 1: Does the extends modifier allow states to use functions defined in the superclass state that would not otherwise be acessible? If not what does it do?

Just like extending a class: Declares a new state that's just like the extended one save the functions you added or overwrote. —Mychaeel

Let me ask another question then. If you do not use the extends modifier in the declaration of the state in a subclass, does the state in your subclass completely replace the state in the superclass, or, does it behave in extactly the same way as if you had used the extends modifier. – EntropicLqd

Maybe you're mixing two things here. With "extends" you declare an entirely new state that just inherits the content of the state it extends from (in the same way subclasses do from their base classes); the state you extend from still exists separately, even in your subclass. If you just wish to override some functions of a state that already exists, you don't need "extends." —Mychaeel

Gotcha. Sorry, was just completely missing the point there. Tarquin's comment below is even more strange in that it means that you can subclass a state defined at any point in the class hierarchy (same class or above) to create a new state. – EntropicLqd

AFAIK, "extends" is to extend a state in the same class, eg Monster.Attacking and Monster.AttackingWithExtremePrejudice. If you have GreenMonster.Attacking, I think that automatically is an extension of Monster.Attacking. In a way (and this is very zen, beware), all states extend the null state, inasmuch as a function is present in all states as declared in the in null state, unless ignored or over-ridden. – Tarquin

Refactor Me

Example

Suppose we have a class Monster and a subclass BugEyedMonster.

Monter has the following states:

  • Idle
  • Attacking

BugEyedMonster inherits these states. We can do the following:

add to an inherited state

eg add a function RollEyes to the Attacking state. (though I'm not sure what happens to label code outside of functions. help!)

AFAIK label code outside of functions are simply executed when control is passed there via the Goto() command. Epic pretty much always have a set of function calls after the label which invariably change the state of the object to make it do more stuff. When defining a state they place all of the functions within the state first and then have a series of labels after the functions that do appropriate "stuff". In subclasses it looks like you can redefine labels to "override them" but you can never call up to the label defined in the superclass once it has been overidden in the subclass's state. – EntropicLqd

add an entirely new state

eg RunningAway (because BugEyedMonsters are very cowardly)

This state will inherit functions from BugEyedMonster's null state

extend a state

eg ScratchingButt extends Idle. We'd want most of the functionality of Idle, but with an extra occasional animation. We'd also have to add to our inherited Idle state to sometimes switch to ScratchingButt depending on circumstances.

I've probably got it all wrong, but if it makes Mych laugh enough he may fix it. :-)Tarquin

So if we consider a function present in Monster, eg Trigger, we now have this in several flavours:

In Monster we have:

  • Trigger() in null state
    • Idle.Trigger() inherits null.Trigger unless we override
    • Atacking.Trigger() inherits null.Trigger unless we override

In BugEyedMonster we have

  • Trigger() in null state, inherits Monster.Trigger unless we override
    • Idle.Trigger() – this is the confusing one. see below
    • Atacking.Trigger()
    • ScratchingButt.Trigger() inherits Idle.Trigger() unless we override

from UnrealScript Language Reference/States

The scoping rules, which resolves these complex situations, are:

  • If the object is in a state, and an implementation of the function exists somewhere in that state (either in the actor’s class or in some parent class), the most-derived state version of the function is called.
  • Otherwise, the most-derived non-state version of the function is called.

This seems to say that if we have the following functions defined:

  • Monster.Trigger
  • Monster.Idle.Trigger
  • BugEyedMonster.Trigger
  • but no Trigger function in BugEyedMonster.Idle, then a call of Trigger when BugEyedMonster is in Idle state will call Monster.Idle.Trigger

Even if there's a BugEyedMonster.Trigger

scoping example

Suppose we have:

Actor >> Pawn >> Monster >> BugEyedMonster

and our BugEyedMonster object is in the Idle state when its Trigger function is called. Which Trigger() piece of script is executed? UnrealScript asks the following questions:

  1. is there a BugEyedMonster.Idle.Trigger() ?
  2. does the parent class, Monster have a state 'Idle' defined? If so, is there a Monster.Idle.Trigger() ?
  3. carry on up the hierarchy looking for Idle.Trigger
  4. if we get to the first state that defines 'Idle' and there's still no Trigger function...
  5. is there a BugEyedMonster.Trigger() ?
  6. does the parent have a non-state Trigger() ?
  7. and so on...

Category To Do

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In