Overloaded variable. Why does this work?

Pretty much exactly what it says on the tin. I am trying to understand why overloaded_variable, which sometimes is a number and sometimes a text substitution…

"Puzzling Behavior" by Mim

The Test Room is a room.

The overloaded_variable is initially 0.

Instead of examining the player: 
	now the overloaded_variable is 2;
	say "[overloaded_variable][paragraph break]";
		
To say overloaded_variable:
	choose row overloaded_variable in the Table of Messages;
	say "[text entry]";  
	

Table of Messages
index	text
1	"Kek."
2	"It works!"

…behaves as I intended, and does not bug the hell out of me.

So, why doesn’t it print just its value, 2?

>x me
It works!

This also works as intended. (Now the name of the table is overloaded_variable too, kek).

"Puzzling Behavior" by Mim

The Test Room is a room.

The overloaded_variable is initially 0.

Instead of examining the player: 
	now the overloaded_variable is 2;
	say "[overloaded_variable][paragraph break]";
		
To say overloaded_variable:
	choose row overloaded_variable in the Table overloaded_variable;
	say "[text entry]";  
	

Table overloaded_variable
index	text
1	"Kek."
2	"It works!"

It acts this way because you redefined ‘overloaded_variable’–the ‘To say’ phrase essentially overwrote the numerical value.

1 Like

Let us comment the [now the overloaded_variable is 2;] line.

"Puzzling Behavior" by Mim

The Test Room is a room.

The overloaded_variable is initially 2.

Instead of examining the player: 
	[now the overloaded_variable is 2;]
	say "[overloaded_variable][paragraph break]";
		
To say overloaded_variable:
	choose row overloaded_variable in the Table overloaded_variable;
	say "[text entry]";  
	

Table overloaded_variable
index	text
1	"Kek."
2	"It works!"

Why does this code behave as if the To say phrase did not overwrite the numerical value? In particular, why is the line

choose row overloaded_variable in the Table overloaded_variable;

acting as if the To say phrase did not overwrite the numerical value? :thinking:

>x me
It works!

>x me
It works!

:wink:

It is as if this phrase is being enforced/invoked at the beginning of each turn :thinking:

I guess that could be your answer. The ‘To say’ phrase did print the correct thing, however, as it redefined the variable. Maybe the magic word was ‘intially’, perhaps meaning at the beginning of the turn, as you say. I’m not sure I recall this concept from the manual…?? (real experts, please chime in!)

1 Like

There are three different things here going by the same name: a variable, a to-say-phrase and a table. The compiler tries to glean from context which is meant where. The only point where this needs an order of precedence is this one: The to-say-phrase takes precedence over the variable in the context of the say function.

The reassignment with now to a numeric value is obviously an operation on the variable. Choosing a row can only be done with a table, so that’s clear.

The “initially” initializes the variable before the beginning of play. If you initialize it to 1 and leave the reassignment commented out, It’ll say “Kek.” if you initialize it to 3, it will throw a run time error.

5 Likes

I was instinctively expecting this. Thank you from the bottom of my heart. :wink: