Agent as a controller

Updated: 2025-09-15

In this chapter we explore how an agent can act as an autonomous visual controller that reacts to changes in its environment and produces animated effects. We will see a typical example: an automatic door that opens when it detects entities nearby and closes when nobody is around.

Core idea

The agent is a VE that never ends and repeatedly executes one task: check whether there are entities in a given area (simulated with a QUEUER) and move the door accordingly.

This kind of logic is ideal for animated behaviors, sensors, traffic lights, or any visual object that acts based on its surroundings.

Example

/*

  	Agent as a controller
 
*/
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}

Queuer {NAME:sensor,X:403,Y:575}

POSITION {NAME:POS1,X:265,Y:454}
POSITION {NAME:POS2,X:523,Y:449}
POSITION {NAME:POS3,X:720,Y:450}

Graphic {NAME:Line1,Type:LINE,color:#FF0000, X1:418,Y1:370,X2:413,Y2:383,X3:387,Y3:383,X4:381,Y4:370}
Graphic {NAME:Line2,Type:LINE,color:#FF0000, X1:422,Y1:509,X2:414,Y2:486,X3:387,Y3:486,X4:382,Y4:508}

Graphic {NAME:Text1,Type:TEXT,X:431,Y:262,Text:" "}

initial nAgent,0
START 200

;*****************************************************

PROCEDURE PRE_RUN
	timeout agent.main,0
	TERMINATE_VE
ENDPROCEDURE 1

;*****************************************************
GENERATE 30,50 {NAME:GEN1,X:86,Y:450}

	ADVANCE 20,0 {TO:POS1}
	queue sensor
	ADVANCE 20,0 {TO:POS2}
	depart sensor
	ADVANCE 20,0 {TO:POS3}

ENDGENERATE 1


;*******************************
procedure agent.main
	savevalue nAgent,D$N
    assign users,0
    assign state,100
    assign posY2,GD$(Line1,Y2)
    while (1==1)
    	advance 1
        call agent.moveDoor
    endwhile
    terminate_ve
endprocedure


procedure agent.moveDoor
    IF (R$(sensor,IN)<=0)
    assign state,P$state + 5
    assign state,MIN(100,P$state)
    else 
    assign state,P$state -5
    assign state,MAX(10,P$state)
    ENDIF

move {name:Line1,Y2:P$state + P$posY2,Y3:P$state + P$posY2}
;move {name:Text1,TEXT:"p$users P$state | P$posY1"}

endprocedure