Tutorial: How to get a reference to a Variable

The place for any Hollywood Designer tutorials
Post Reply
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Tutorial: How to get a reference to a Variable

Post by Bugala »

Came to my mind this could be useful to others too.

As Hollywood doesnt normally support References to variables, here is how to get something equivalent to a reference to a variable In Hollywood.

Code: Select all

Function NewValueToVarReference(Table, Var, NewValue)
	Table[Var] = NewValue
EndFunction

TestTable = {a = 1, b = 2}

NewValueToVarReference(TestTable, "a", 3)
VarToUse = "b"
NewValueToVarReference(TestTable, VarToUse, 4)

DebugPrint(TestTable.a)
DebugPrint(TestTable.b)
Result:
3
4


Do notice it is recommended to use small letters in variable names, as example, using:

Code: Select all

VarToUse = "B"
would not work correctly since it would be trying to access TestTable["B"] instead of TestTable["b"]

Point for using noncapitalized is that there is no way to access Capital Letter named variables through regular table.var suffix.

You can assign

Code: Select all

TestTable["B"] = 2
, and then access it using

Code: Select all

Debugprint(TestTable["B"])
But if you would try to access it using either:

Code: Select all

TestTable.b
TestTable.B
they would both be trying to access TestTable.["b"]

While ["abc"], ["Abc"], and ["ABC"] all create their own unique variable, only way to access Abc or ABC, is by using table["Abc"], table["ABC"], everything else would try to access Table["abc"]
as example:

Code: Select all

Table.abc
Table.Abc
Table.ABC
are all referring to Table.abc / Table["abc"]
Post Reply