Updating the event queue: UPDATE

Updated: 2026-01-07

We previously saw the ADVANCE block as something that seemed immutable. For example:

ADVANCE 20,10

The role of UPDATE

An entity that is inside an ADVANCE cannot modify its own wake-up time, because it is inactive. Therefore, another entity (usually a virtual entity, triggered by a TIMER or a HOOK) must execute UPDATE to adjust its time.

Technically, UPDATE does not modify an ADVANCE: it modifies the event queue.
It removes the entity from its future activation and reschedules it at a new absolute time.
The engine does not interpret the change: it only keeps the queue ordered.

 

Scenario: Changing conditions on a road segment

We simulate a road segment under conditions stored in the SAVEVALUE conditions.

  • Under normal conditions (conditions = 1.0), it takes 100 time units to cross.

  • If conditions = 1.5, the trip takes 150 time units.

  • An entity computes its travel time when it enters the segment.

  • If conditions change while an entity is on the way, we want to adjust its arrival time to reflect that change.

How is it computed?

Imagine an entity entered when conditions = 1.2 (travel time = 120), but now conditions improve to 1.0. If it has already completed 50%, it would have 50 units left under the new conditions. In total, it would take 110 instead of 120.

The UPDATE command

UPDATE entityID, newTime

This command reschedules an entity with a new activation time, as long as that time is ≥ AC1$ (the current simulation time).

What this example demonstrates:

  • That an entity’s behavior can be modified even while it is in progress.

  • That TIMER + FOREACH + UPDATE is a powerful combination to react dynamically.

  • That it enables representing real-world scenarios where conditions change and the system must adapt.

 

Example

/*

 Updating the event queue: UPDATE

*/
SYSTEM {TYPE:ON_TIMER, TRIGGER:TIMER1, INTERVAL: 100}

Queuer {NAME:Queuer1,X:350,Y:324}
Graphic {NAME:Text1,Type:TEXT,X:352,Y:381}

POSITION {NAME:POS1,X:90,Y:513}
POSITION {NAME:POS2,X:638,Y:512}
POSITION {NAME:POS3,X:635,Y:322}

START 100

;*********************************************************
INITIAL defaultTime,100
INITIAL conditions,1.0
;*********************************************************
PROCEDURE TIMER1
	savevalue oldConditions,X$conditions
	savevalue conditions,round(random + 0.5,2)
    MOVE {NAME:Text1,TEXT:"Old Conditions: X$oldConditions \nNew conditions: X$conditions"}
    FOREACH number,IN_RESOURCE,Queuer1
    
    assign myConditions,X$conditions,P$number

    ASSIGN from, D$(ADVANCESTART, P$number)
    ASSIGN lapse, D$(ADVANCELAPSE, P$number)
    
    ASSIGN completedRatio , (AC1$ - P$from) / P$lapse
	ASSIGN remainingRatio , 1 - P$completedRatio 

	ASSIGN adjustedRemainingTime , P$remainingRatio  * X$defaultTime * X$conditions
	ASSIGN newTime, AC1$ + P$adjustedRemainingTime 

      if (X$conditions<1)
     	 MOD {number:P$number,color:#00DD00}
      else
      	MOD {number:P$number,color:#000000}
      endif
      if (P$newTime < AC1$)
     	 assign newTime,AC1$
      endif
      Update P$number,P$newTime
    ENDFOREACH

    

    TERMINATE_VE
ENDPROCEDURE
;*********************************************************


GENERATE 20,0 {NAME:GEN1,X:102,Y:203, ECOLOR:#FF3333, ERADIO:8,esubtitle:P$myConditions}
    ADVANCE 16,5 {TO:POS1}
    assign myTime,X$defaultTime * X$conditions
    assign myConditions,X$conditions
    Queue Queuer1
    ADVANCE P$myTime,0 {TO:POS2}
    Depart Queuer1
    MOD {color:#FF3333}
    ADVANCE 16,5 {TO:POS3}
ENDGENERATE 1