Accessing Variables from Other Entities

Updated: 2025-11-21

Accessing Variables from Other Entities

In addition to working with local or global variables, GPSS-Plus allows reading and writing ASSIGN variables from any live entity in the system.

Reading via SNA:

The usual P$ SNA accepts a second parameter indicating the target entity number:

P$(variableName, entityNumber)

This makes it possible to query internal values from another entity without copying them into a SAVEVALUE.

MOVE {name:text5, text:"Entity 2 has: P$(aPrivateNumber,2)"}

This directly accesses the variable aPrivateNumber currently active in entity number 2.

Writing into another entity:

By using a third parameter in the ASSIGN instruction, data can be stored directly into the target entity:

ASSIGN aNumber, 123, 1

Checking entity existence:

The existence of an entity can be verified using the specific SNA:

IF (D$(EXIST,1000)==1)

Example

/* Cross-entity access using P$ and ASSIGN with target entity */

POSITION {NAME:POS_MOD,   X:286, Y:244}
POSITION {NAME:POS_END,   X:741, Y:233, type:terminate}

GRAPHIC {NAME:T1, type:TEXT, X:455, Y:300}
GRAPHIC {NAME:T2, type:TEXT, X:455, Y:188}

START 60   ; there will be 6 entities (3 of each type)

/* --- ENTITY TYPE A --- */
GENERATE 30,0,15 {NAME:GA, X:100, Y:70, ECOLOR:#ff3333, subtitle:"A"}
    ASSIGN miValor, 100
    MOD {subtitle:"My value P$miValor"}

    ADVANCE 20 {to:POS_MOD}

    ; If entity B exists (2, 4, 6...)
    IF (D$(EXIST, D$N+1)==1)
        ASSIGN nEntidadDestino, D$N+1
        ASSIGN miValor, P$(miValor)+10, P$nEntidadDestino   ; A increments B's value
        MOVE {name:T1, text:"I change the value of P$nEntidadDestino to: [P$(miValor,P$nEntidadDestino)]"}
    ENDIF

    ADVANCE 50 {to:POS_END}
ENDGENERATE 3

/* --- ENTITY TYPE B --- */
GENERATE 30,0 {NAME:GB, X:103, Y:407, ECOLOR:#3366ff, subtitle:"B"}
    ASSIGN miValor, 200
    MOD {subtitle:"My value P$miValor"}

    ADVANCE 20 {to:POS_MOD}

    ; If entity A exists (1, 3, 5...)
    IF (D$(EXIST, D$N-1)==1)
        ASSIGN nEntidadDestino, D$N-1
        ASSIGN miValor, P$(miValor)-5, P$nEntidadDestino   ; B reduces A's value
        MOVE {name:T2, text:"I change the value of P$nEntidadDestino to: [P$(miValor,P$nEntidadDestino)]"}
    ENDIF

    ADVANCE 50 {to:POS_END}
ENDGENERATE 3