Variables: Savevalues and Assigns

Updated: 2026-11-17

In GPSS-Plus, variables allow storing information that can be used by entities or to configure elements of the environment. These variables are divided into two types:

1. SAVEVALUE (global variables): Global variables are accessible by any entity and persist throughout the entire simulation. They are useful for storing shared data such as counters, accumulators, or global states.

Syntax:
SAVEVALUE variable, value

Example:

SAVEVALUE TiempoEspera, 8

This command sets the global variable "TiempoEspera" to a value of 8.

Once defined, the value can be updated at any time:

SAVEVALUE TiempoEspera, 10

The value of "TiempoEspera" is now 10.

To retrieve the value of a global variable, SNA are used, which follow a specific format:

X$savevalueName or X$(savevalueName)

; X$TiempoEspera or X$(TiempoEspera)

IF (X$TiempoEspera > 10)
...

For example, incrementing the value of a savevalue can be done as follows:

SAVEVALUE TiempoEspera, X$TiempoEspera + 1

Or through the method:

SAVEVALUE.inc TiempoEspera

In particular, since these variables are used by more than one entity, they have an associated COMMAND for creation and initialization called INITIAL.

Its syntax is simple:

INITIAL TiempoEspera,10

Remember that INITIAL is a command. It is used at the beginning of the program for initialization.

2. ASSIGN (entity local variables):
Local variables belong to a specific entity. Each entity can have its own value for the same variable, which makes them useful for managing entity-specific data.

Syntax:
ASSIGN variable, value

Example:

ASSIGN Identificador, 1

This block assigns the value 1 to the variable "Identificador" of the current entity invoking ASSIGN.

To retrieve the value of a local variable using its SNA format:

P$assignName or P$(assignName)

; P$Identificador or P$(Identificador)
if (P$Identificador > 5) ...

P$Identificador is the form that allows obtaining the value of a local variable of the entity.

Key differences between SAVEVALUE and ASSIGN:

  • Scope: SAVEVALUE variables are global, while ASSIGN variables are entity-local.
  • Persistence: SAVEVALUE variables retain their value at all times, even if no entity uses them. ASSIGN variables only exist while the entity is active.
  • Usage: Use SAVEVALUE for shared data, and ASSIGN for entity-specific data.

Combined example:

In this example, we combine SAVEVALUE and ASSIGN to calculate the average waiting time at a service counter. If the time is defined by an ADVANCE 15,10, it means it will range between 15 and 25 time units. This should gradually approximate the average to 20.

INITIAL TiempoTotal, 0
INITIAL NumEntidades, 0
Facility {NAME:Ventanilla,X:354,Y:204}
Graphic {NAME:Text1,Type:TEXT,X:358,Y:321,Text:"Average"}
Position {NAME:Salida,X:577,Y:201}
START 50
;---------------------------------

Generate 20,0 {NAME:GEN1,X:100,Y:207}
ADVANCE 10 {to:Ventanilla}
SEIZE Ventanilla
ASSIGN TiempoInicio, AC1$
ADVANCE 15,10
ASSIGN TiempoFinal, AC1$
RELEASE Ventanilla
ADVANCE 15 {to:Salida}
ASSIGN TiempoEntidad, (P$TiempoFinal - P$TiempoInicio)
SAVEVALUE NumEntidades, X$NumEntidades + 1
SAVEVALUE TiempoTotal, X$TiempoTotal + P$TiempoEntidad
SAVEVALUE promedio, round(X$TiempoTotal / X$NumEntidades,3)
MOVE {NAME:Text1,text:"Average: X$promedio"}
TERMINATE 1

In this code:
- Each entity calculates its waiting time at the service counter.
- Times are accumulated in the global variable "TiempoTotal".
- The number of served entities is counted in "NumEntidades".
- At the end of the simulation, you can compute the average waiting time:
AverageTime = X$TiempoTotal / X$NumEntidades

In this way, SAVEVALUE and ASSIGN work together to provide efficient data handling within the simulation.

Later on, we will see how both BLOCKS support much more than just numerical variables.