ProtoLang.net

Reference: Set

☰ Hide Sidebar
TOKEN SET
ALIAS None

The Set Command is used to change the value of an existing variable in the current scope's memory (Read about scope in Functions). The Set command can only modify variables that have already been created with the New command. The new value must match the variable's original data type, which cannot be changed after creation.

Syntax

The short syntax structure uses the word SET:

SET VARIABLE = VALUE

The long syntax structure uses the word SET:

SET VARIABLE EQUAL TO VALUE

Basic Value Updates

The Set command replaces the current value of a variable with a new value.

Example: Updating Integer Variable

NEW INT counter = 0
PRINTLN counter // Displays "0"
SET counter = 5
PRINTLN counter // Displays "5"
SET counter = 10
PRINTLN counter // Displays "10"

Example: Updating Float Variable

NEW FLOAT temperature = 72.5
PRINTLN temperature // Displays "72.5"
SET temperature = 75.0
PRINTLN temperature // Displays "75.0"

Example: Updating String Variable

NEW STR status = "pending"
PRINTLN status // Displays "pending"
SET status = "active"
PRINTLN status // Displays "active"
SET status = "complete"
PRINTLN status // Displays "complete"

Example: Updating Boolean Variable

NEW BOOL isActive = FALSE
PRINTLN isActive // Displays "FALSE"
SET isActive = TRUE
PRINTLN isActive // Displays "TRUE"

Using Different Value Types

Like the New command, Set can assign Literals, Variables, or Expressions.

Example: Setting with Literals

NEW INT x = 0
SET x = 42 // Integer literal
PRINTLN x // Displays "42"

NEW STR name = ""
SET name = "Alice" // String literal
PRINTLN name // Displays "Alice"

Example: Setting with Variables

NEW INT source = 100
NEW INT destination = 0
SET destination = source
PRINTLN destination // Displays "100"

NEW STR original = "Hello"
NEW STR copy = ""
SET copy = original
PRINTLN copy // Displays "Hello"

Example: Setting with Expressions

NEW INT x = 10
SET x = x + 5 // Arithmetic expression
PRINTLN x // Displays "15"

NEW BOOL result = TRUE
SET result = TRUE && FALSE // Logical expression
PRINTLN result // Displays "FALSE"

NEW STR message = "Score: "
SET message = message .. "100" // Concatenate expression
PRINTLN message // Displays "Score: 100"

Using Variable in Its Own Update

A powerful feature of Set is the ability to use a variable's current value to calculate its new value.

Example: Incrementing

NEW INT counter = 0
SET counter = counter + 1
PRINTLN counter // Displays "1"
SET counter = counter + 1
PRINTLN counter // Displays "2"
SET counter = counter + 1
PRINTLN counter // Displays "3"

Example: Decrementing

NEW INT lives = 3
SET lives = lives - 1
PRINTLN lives // Displays "2"
SET lives = lives - 1
PRINTLN lives // Displays "1"

Example: Multiplying

NEW INT value = 5
SET value = value * 2
PRINTLN value // Displays "10"
SET value = value * 3
PRINTLN value // Displays "30"

Example: Accumulating

NEW FLOAT total = 0.0
SET total = total + 10.5
SET total = total + 20.3
SET total = total + 15.7
PRINTLN total // Displays "46.5"

Example: Building Strings

NEW STR output = "Result: "
SET output = output .. "Step 1, "
SET output = output .. "Step 2, "
SET output = output .. "Complete"
PRINTLN output // Displays "Result: Step 1, Step 2, Complete"

Using Set in Conditionals

The Set command is commonly used inside If statements to update variables based on conditions.

Example: Conditional Update

NEW INT score = 75
NEW STR grade = ""
IF score >= 90
    SET grade = "A"
ELIF score >= 80
    SET grade = "B"
ELIF score >= 70
    SET grade = "C"
ELSE
    SET grade = "F"
END IF
PRINTLN grade // Displays "C"

Example: Toggling Boolean

NEW BOOL isOn = FALSE
PRINTLN "Status: " .. isOn // Displays "Status: FALSE"
IF isOn == FALSE
    SET isOn = TRUE
ELSE
    SET isOn = FALSE
END IF
PRINTLN "Status: " .. isOn // Displays "Status: TRUE"

Example: Applying Bonus

NEW INT points = 100
NEW BOOL hasBonus = TRUE
IF hasBonus == TRUE
    SET points = points + 50
END IF
PRINTLN "Total points: " .. points // Displays "Total points: 150"

Example: Status Update

NEW FLOAT temperature = 105.0
NEW STR alert = "Normal"
IF temperature > 100.0
    SET alert = "High Temperature Warning"
ELIF temperature < 32.0
    SET alert = "Low Temperature Warning"
END IF
PRINTLN alert // Displays "High Temperature Warning"

Using Set in Loops

The Set command is essential for updating loop counters and accumulating values in Loops.

Example: Basic Loop Counter

NEW INT i = 1
WHILE i <= 5
    PRINTLN "Iteration: " .. i
    SET i = i + 1
END WHILE
// Displays: Iteration: 1, Iteration: 2, Iteration: 3, Iteration: 4, Iteration: 5

Example: Countdown Loop

NEW INT countdown = 5
WHILE countdown > 0
    PRINTLN "T-minus " .. countdown
    SET countdown = countdown - 1
END WHILE
PRINTLN "Liftoff!"

Example: Accumulator Pattern

NEW INT sum = 0
NEW INT i = 1
WHILE i <= 10
    SET sum = sum + i
    SET i = i + 1
END WHILE
PRINTLN "Sum: " .. sum // Displays "Sum: 55"

Example: Building Output in Loop

NEW STR result = ""
NEW INT counter = 1
WHILE counter <= 3
    SET result = result .. counter .. " "
    SET counter = counter + 1
END WHILE
PRINTLN result // Displays "1 2 3 "

Example: Multiple Variables in Loop

NEW INT current = 1
NEW INT previous = 0
NEW INT temp = 0
NEW INT i = 1
WHILE i <= 5
    PRINTLN current
    SET temp = current
    SET current = current + previous
    SET previous = temp
    SET i = i + 1
END WHILE
// Displays Fibonacci sequence: 1, 1, 2, 3, 5

Practical Applications

Example: Score Tracking

NEW INT playerScore = 0
PRINTLN "Score: " .. playerScore
// Player collects items
SET playerScore = playerScore + 10
PRINTLN "Score: " .. playerScore // Displays "Score: 10"
SET playerScore = playerScore + 25
PRINTLN "Score: " .. playerScore // Displays "Score: 35"
SET playerScore = playerScore + 50
PRINTLN "Score: " .. playerScore // Displays "Score: 85"

Example: Temperature Conversion

NEW FLOAT celsius = 25.0
NEW FLOAT fahrenheit = 0.0
SET fahrenheit = ( celsius * 9.0 / 5.0 ) + 32.0
PRINTLN celsius .. "C = " .. fahrenheit .. "F"
// Displays "25.0C = 77.0F"

Example: Inventory Management

NEW INT stock = 100
PRINTLN "Initial stock: " .. stock
// Customer purchases
SET stock = stock - 15
PRINTLN "After sale: " .. stock // Displays "After sale: 85"
// Restock
SET stock = stock + 50
PRINTLN "After restock: " .. stock // Displays "After restock: 135"

Example: State Machine

NEW STR state = "idle"
NEW STR action = "start"
PRINTLN "Current state: " .. state
IF action == "start"
    SET state = "running"
ELIF action == "stop"
    SET state = "stopped"
END IF
PRINTLN "New state: " .. state // Displays "New state: running"

Example: Health System

NEW INT health = 100
NEW INT maxHealth = 100
PRINTLN "Health: " .. health
// Take damage
SET health = health - 30
PRINTLN "Health: " .. health // Displays "Health: 70"
// Heal
SET health = health + 20
IF health > maxHealth
    SET health = maxHealth
END IF
PRINTLN "Health: " .. health // Displays "Health: 90"

Data Type Consistency

The Set command enforces type consistency - you can only assign values that match the variable's declared type.

Example: Integer Type Consistency

NEW INT count = 10
SET count = 20 // Valid: Integer literal
SET count = 5 + 5 // Valid: Expression result is Integer
SET count = count * 2 // Valid: Expression result is Integer
PRINTLN count // Displays "20"

Example: Float Type Consistency

NEW FLOAT price = 10.0
SET price = 15.99 // Valid: Float literal
SET price = price * 1.5 // Valid: Expression result is Float
PRINTLN price // Displays "23.985"

Example: String Type Consistency

NEW STR message = "Hello"
SET message = "Goodbye" // Valid: String literal
SET message = message .. "!" // Valid: Expression result is String
PRINTLN message // Displays "Goodbye!"

Example: Boolean Type Consistency

NEW BOOL flag = TRUE
SET flag = FALSE // Valid: Boolean literal
SET flag = 10 > 5 // Valid: Expression result is Boolean
PRINTLN flag // Displays "TRUE"

Complex Updates

Example: Multi-Step Calculation

NEW FLOAT total = 100.0
NEW FLOAT taxRate = 0.08
NEW FLOAT discount = 0.10
// Apply discount
SET total = total - ( total * discount )
PRINTLN "After discount: " .. total // Displays "After discount: 90.0"
// Add tax
SET total = total + ( total * taxRate )
PRINTLN "After tax: " .. total // Displays "After tax: 97.2"

Example: Conditional Accumulation

NEW INT score = 0
NEW INT i = 1
WHILE i <= 10
    IF i % 2 == 0
        SET score = score + i
    END IF
    SET i = i + 1
END WHILE
PRINTLN "Even sum: " .. score // Displays "Even sum: 30"

Example: Swapping Values

NEW INT a = 5
NEW INT b = 10
NEW INT temp = 0
PRINTLN "Before: a=" .. a .. ", b=" .. b
SET temp = a
SET a = b
SET b = temp
PRINTLN "After: a=" .. a .. ", b=" .. b
// Displays "Before: a=5, b=10"
// Displays "After: a=10, b=5"

Example: Progressive Calculation

NEW INT value = 10
PRINTLN "Start: " .. value
SET value = value + 5
PRINTLN "After add: " .. value // Displays "After add: 15"
SET value = value * 2
PRINTLN "After multiply: " .. value // Displays "After multiply: 30"
SET value = value - 10
PRINTLN "After subtract: " .. value // Displays "After subtract: 20"
SET value = value / 4
PRINTLN "Final: " .. value // Displays "Final: 5"

Set vs New

It's important to understand the difference between Set and New.

Example: New Creates, Set Modifies

NEW INT x = 10 // New creates the variable
PRINTLN x // Displays "10"
SET x = 20 // Set modifies the existing variable
PRINTLN x // Displays "20"
SET x = 30 // Set can be used multiple times
PRINTLN x // Displays "30"

Example: New Requires Type, Set Does Not

NEW INT age = 25 // New and requires data type
SET age = 26 // Set only needs the variable name and new value
SET age = 27 // Type is already known from creation
PRINTLN age // Displays "27"

Important Notes

  • Set can only modify variables that already exist.
  • The variable must be created with New before using Set.
  • The new value must match the variable's original data type.
  • A variable can use its own current value in the assignment expression.
  • Set can be used multiple times on the same variable.
  • Variable names in Set are case-sensitive.

Common Errors

Error: Setting Non-Existent Variable

SET x = 10 // Runtime error! Variable "x" does not exist!
// Correct: Create variable first with New
NEW INT x = 10
SET x = 20 // Now valid

Error: Type Mismatch

NEW INT x = 5
SET x = 5.0 // Runtime error! Cannot assign Float to Integer variable
SET x = 6 // Correct: Integer value matches Integer type

Error: with Using Set

NEW INT x = 5
SET INT x = 10 // Syntax error! Set does not SET use x = 10 // Correct: Set only needs variable name and value

Error: Assigning Wrong Type

NEW INT count = 10
SET count = "twenty" // Runtime error! Cannot assign String to Integer
SET count = 20 // Correct: Integer matches Integer type

Error: Boolean Type Mismatch

NEW BOOL flag = TRUE
SET flag = 1 // Runtime error! Cannot assign Integer to Boolean
SET flag = FALSE // Correct: Boolean literal matches Boolean type

Error: Mixing Integer and Float

NEW INT x = 10
NEW FLOAT y = 2.5
SET x = x + y // Type error! Cannot mix Integer and Float
// Correct: Keep types consistent
NEW INT a = 10
NEW INT b = 2
SET a = a + b // Valid: Both are Integers

Error: Case Sensitivity

NEW INT myVar = 5
SET MyVar = 10 // Runtime error! Variable "MyVar" does not exist
SET myVar = 10 // Correct: Case must match exactly

Error: String Without Quotes

NEW STR message = "Hello"
SET message = Goodbye // Syntax error! Strings must be quoted
SET message = "Goodbye" // Correct: String is properly quoted

Error: Integer Assigned to Float Variable

NEW FLOAT price = 10.0
SET price = 15 // Runtime error! 15 is Integer, not Float
SET price = 15.0 // Correct: Float literal with decimal point

Error: Trying to Change Type

NEW INT value = 10
SET value = 10.5 // Runtime error! Cannot change type from Integer to Float
// Correct: Create new variable if different type needed
NEW INT value = 10
NEW FLOAT floatValue = 10.5