Drag[en]gine Script Module DragonScript  1.23
Dragengine.Utils.ElementResolver Class Reference

Resolve element links. More...

Inheritance diagram for Dragengine.Utils.ElementResolver:

Classes

class  Request
 Resolve request. More...
 
class  ResolveException
 Resolve failure. More...
 

Public Member Functions

void add (UniqueID id, Block ablock)
 Add resolve request. More...
 
Request getAt (int index)
 Pending request at index. More...
 
int getCount ()
 Number of pending requests. More...
 
ElementResolver new ()
 Create resolver. More...
 

Detailed Description

Resolve element links.

Helper class to resolve element identifiers read from file readers. While loading elements from files the elements referenced by identifiers very well can not be present yet in the game world since they show up in the file at a later time. To support such links the UniqueID of an element is saved. Once read it has to be resolved once all elements are present in the game world. The best time for this is during a call to Element.enterFrame(). An instance of this class has to be placed in elements requiring resolving behavior. To resolve an encountered element add a resolve request to the instance of this class with a block to use. The following code example shows how to use this class.

class MyElement extends Element
private var ElementResolver pElementResolver
private var Element pElementInHands
public func new()
this.pElementResolver = ElementResolver.new()
end
public func void readFromFile( FileReader reader )
...
if hasElementInHands
this.pElementResolver.add( UniqueID.readFromFile( reader ), block Element resolved
this.pElementInHands = resolved
end )
end
end
public func addToGameWorld()
// request enterFrame() to be called once. works fine for both creating or loading
// elemente. in case of creating new elements there are simply no request to resolve
this.getGameWorld().addEnterFrameOnce( this )
end
public func void enterFrame()
// resolve all elements. once finished all request are removed. if a resolve request
// fails the block is called with a null element. it is the responsibility of the
// block to handle this situation.
this.pElementResolver.resolve( this.getGameWorld() )
end
end
void add(UniqueID id, Block ablock)
Add resolve request.
ElementResolver new()
Create resolver.
BehaviorsElement Behaviors

This class is also able to resolve BehaviorElement behaviors. During saving you have to store both the BehaviorElement UniqueID as well as the behavior instance index. The DefaultECBehavior class provides this information. While resolving the block is called with BehaviorInstance as parameter. In general you will change this to parameter type to match the behavior stored.

class MyElement extends Element
private var ElementResolver pElementResolver
private var MyBehavior.Instance pElementInHands
public func new()
this.pElementResolver = ElementResolver.new()
end
public func void readFromFile( FileReader reader )
...
if hasElementInHands
var UniqueID id = UniqueID.readFromFile( reader )
var int index = reader.readUShort()
this.pElementResolver.addBehavior( id, index, block MyBehavior.Instance behavior
this.pElementInHands = behavior
end )
end
end
public func addToGameWorld()
// request enterFrame() to be called once. works fine for both creating or loading
// elemente. in case of creating new elements there are simply no request to resolve
this.getGameWorld().addEnterFrameOnce( this )
end
public func void enterFrame()
// resolve all elements. once finished all request are removed. if a resolve request
// fails the block is called with a null element. it is the responsibility of the
// block to handle this situation.
this.pElementResolver.resolve( this.getGameWorld() )
end
end

Member Function Documentation

◆ add()

void Dragengine.Utils.ElementResolver.add ( UniqueID  id,
Block  ablock 
)

Add resolve request.

Add request to resolve element by identifier. Once resolved ablock is called with the resolved element as parameter. If resolving fails ablock is called with null as parameter.

◆ getAt()

Request Dragengine.Utils.ElementResolver.getAt ( int  index)

Pending request at index.

◆ getCount()

int Dragengine.Utils.ElementResolver.getCount ( )

Number of pending requests.

◆ new()

ElementResolver Dragengine.Utils.ElementResolver.new ( )

Create resolver.


The documentation for this class was generated from the following file: