Skip to content

Variables and Containers

Matt DeFano edited this page Mar 9, 2019 · 2 revisions

Variables | Parts | Menus | Message | It | Selection | Target

A container is anything in WyldCard that you can put a value into (an l-value, in C parlance): parts, variables, properties, menus, the message box, the selection and the target are all containers.

Use the HyperTalk command put to place a value into a container (put 10 into x). HyperTalk does not use the = operator for value assignment as is traditional in most programming languages.

Variables as containers

In HyperTalk, variables are implicitly declared when a value is put into them. Referring to a variable that has had nothing put into it results in the name of the variable itself.

For example:

on mouseDown
  put "Something" into someVariable  -- someVariable contains 'Something'
  answer someVariable                -- Displays 'Something'
  answer nothingHere                 -- Displays 'nothingHere'
end mouseDown

Local variables in HyperTalk are lexically scoped and retain their value only within the handler in which they were created.

A variable may be made global by declaring it so with the global keyword. Global variables are accessible from any script of any part in any open stack. Once created, a global variable retains its value until the WyldCard application is closed. Be aware that variables not explicitly declared global are considered local, even when a global variable of the same name exists.

--
-- Global and local variable example script
--

on mouseUp
  global fivr
  put 5 into fivr  -- puts 5 into global variable

  answer local()   -- Displays "fivr"
  answer global()  -- Displays "5"
end mouseUp

function local
  return fivr	   -- returns "fivr"
end f

function global
  global fivr
  return fivr	   -- returns "5"
end y

Parts as containers

Like variables, a part can also store value. When placing a value into a field, the text of the field is changed. However, when placing a value into a button, card, background or stack, the part's contents property is changed (the contents of a part can be seen and edited from the "Info..." dialog in the "Objects" menu). Except when dealing with text fields and popup styled buttons (combo boxes), the contents property does not affect how the part appears on screen.

In HyperCard, a value could only be placed into button or field parts. WyldCard allows values to be placed into cards, backgrounds and stacks, too. Similarly, HyperCard did not provide a contents property, but in WyldCard, put pi into the contents of card field "Math" is equivalent to put pi into card field "Math"

For example:

put 35 + 27 into field id 12          -- Changes the text of this field to "62"
put 35 + 27 into button "My Button"   -- Changes the contents property of this button to "62"
put "This is my card" into this card  -- Changes the contents property of this card
put "Yes,No" into button myMenuButton -- Menu-styled button gets two menu items "Yes" and "No"

Menus as containers

Every menu in the menu bar, as well as buttons of the style popup, are special containers whose contents defines the list of menu items that appear in them. The value placed into a menu container is interpreted as a list of items or lines, each of which represents an item in the menu. A single hyphen (-) value in the list is interpreted as menu separator (this was not true in HyperCard).

For example:

create menu "My Menu"                               -- adds a new menu to the menu bar
put "Item 1,Item 2,-,Other..." into menu "My Menu"  -- adds three items and a separator

The result of getting menu "My Menu" would produce:

Item 1
Item 2
-
Other...

The message as a container

The message box is a floating window containing a single-line editable text field (you can show or hide this window from the "Go" menu). Text entered into the message is evaluated as a HyperTalk statement when you press enter, and the contents of this field can be read or written as a container. The contents of the message window are addressable in HyperTalk as [the] message, [the] message box or [the] message window.

For example:

put " -- Add a comment" after the message box
multiply the message by 3

When a container is not explicitly specified in the put command, the message box container is used. For example, put "Hello" causes the message box to be displayed and for the text "Hello" to appear inside of it.

The it container

HyperTalk provides an implicit variable named it. Some commands place a value into this variable so that it refers to the last computed value. Scripts can change the value of it using the put command, but this is not advised as values placed into it may be overwritten by subsequent commands.

For example:

on mouseUp
  get 2 * 3
  answer it   -- Responds with 6
end mouseUp

The selection as a container

The active selection of text (created by dragging and highlighting editable text in a field or the message box) can be used as a container. When a value is put into the selection, the selected text itself is changed.

Modern windowing systems typically allow multiple selections of text to exist concurrently (in different fields, for example). HyperCard did not allow concurrent selections of text, but in WyldCard multiple selections of text can exist simultaneously. In this situation, the selection refers to the last selection made.

For example,

if the selection is a number then multiply the selection by 10
put "[redacted]" into the selection

The target as a container

The target is both a HyperTalk function (when used with the, as in the target) and also a part. The built-in function the target returns a string expression referring to the part that the current message was originally sent to, but target refers to the part itself.

For example, consider the behavior of this script when added to a card field:

on mouseUp
  answer the target   -- displays 'card field id x'
  answer target       -- displays the text of this field
end mouseUp