Real numbers and To decide rules

I have a simple calculation that seems to work in an AFTER action but not in a rule. I get an error in one place but not another. This is driving me crazy! Any help? I am trying to calculate the money value of the player.

After looking:
if player is carrying a pouch:
say “Player has [number of coins] coins in his pouch ([number of gold coins] gp/ [number of silver coins] sp).[line break]”;
now money of player is number of gold coins + (number of silver coins * 0.1);
say “Player has [money of player] gp in his pouch.”;
otherwise:
say “The player has no pouch, and therefore, no money.”;

To decide what real number is money of player:
if player is carrying an open pouch:
let money of player be number of gold coins + (number of silver coins * 0.1);
otherwise:
now money of player is 0.2;
decide on money of player;

BTW, what is with the strange blank lines inserted (apparently randomly) since your server came back up? I can’t add a code block like I used to. I also don’t have a DRAFT window to help me see what I’m posting. I think this is going to be a problem.

I notice that in the decide rule, you bury the let statement in the if block.

1 Like

Yes, is that a problem? Money of player is a (global) variable outside the rule block. Should I make a temp variable and assign it outside the block?Or perhaps not have the line “decide on money of player;” (although that seems to do nothing like everything else). :frowning:

BTW you can use single backticks to demarcate inline code, and triple backticks to demarcate blocks of code.

By using “let” you’ve redeclared money of player as a local variable. I don’t have my computer in front of me so I can test it, but could that be your problem?

I forgot to include the (paradoxical) error message.

Problem. You wrote 'let money of player be number of gold coins + (number of silver coins * 0.1)' , but that seems to involve multiplying a silver coin ('silver coins') by a real number ('0.1'), which is not good arithmetic.

Notice that it works in the AFTER block but not in the To decide block?!

Makes sense. Let me try that.

Could it be that you’re using the rich text editor? There’s a button at the top left top switch between WYSIWYG and markup editors.

On to your issue: it sounds like an issue of precedence. Try putting parentheses around (number of silver coins).

Another thing that seems questionable (though it might, in fact, be fine) is naming your variable the same as the phrase.

Also, as a side note, I would personnally recommend using equations, even for relatively simple calculations like this:

To decide what real number is money of player:
	let g be number of gold coins;
	let s be number of silver coins;
	let X be given by X = g + 0.1s where X is a real number;
	decide on X.

That way you don’t need to sprinkle parentheses all over, because the equation parser understands proper precedence. The downside is that you can only work with variables, not with other expressions like number of silver coins.

Yes, thank, and thanks. I was using the (default) RTF editor.

I used the equation in the AFTER rule because I couldn’t get the DECIDE block to work. When the AFTER rule worked, I was totally mystified.

Also, the line

let X of player be given by X = g + 0.1s where X is a real number

blows up in the parser.

OK, I got the code block to work by removing “of player” in
let X of player be given by…

However, I still get the 0.0 value for the money of player.
Any suggestions?

Current code

After looking:
	if player is carrying a pouch:
		say "Player has [number of coins] coins in his pouch ([number of gold coins] gp/ [number of silver coins] sp).[line break]";	
		say "Player has [money of player] gp in his pouch.";
	otherwise:
		say "The player has no pouch, and therefore, no money.";


To decide what real number is money of player:
	let g be number of gold coins in pouch;
	let s be number of silver coins in pouch;
	let X be given by X = g + 0.1s where X is a real number;
	decide on X.

Whoops, that was a mistake on my part. I was editing in parallel in the forum and in the IDE. I fixed it.

The code in your previous post works for me, so I think there must be something wrong in the code you haven’t shown.

To be precise, this compiles and works:

A pouch is a container.
A coin is a kind of thing.
A coin can be silver or gold.
3 gold coins are in pouch.
12 silver coins are in pouch.

After looking:
	if player is carrying a pouch:
		say "Player has [number of coins] coins in his pouch ([number of gold coins] gp/ [number of silver coins] sp).[line break]";	
		say "Player has [money of player] gp in his pouch.";
	otherwise:
		say "The player has no pouch, and therefore, no money.";


To decide what real number is money of player:
	let g be number of gold coins in pouch;
	let s be number of silver coins in pouch;
	let X be given by X = g + 0.1s where X is a real number;
	decide on X.

Sample output:

>l
room
The player has no pouch, and therefore, no money

>purloin pouch
[Purloined.]

>l
room
Player has 15 coins in his pouch (3 gp/ 12 sp).
Player has 4.2 gp in his pouch.

OK, I figured out the bogus line in my code. It is

The player has a real number called money. Money is 0.0.

With that commented out, all works fine. Now the question is: WHY?

Does money of player DECIDED on not replace the value of the property Money for the player?

1 Like

That’s a good question! Here’s the relevant snippet from the Preform (on a very old build, but I bet this hasn’t changed that much):

<s-value> ::=
a ( <s-value> ) |
b <s-variable> |
c <s-constant-value> |
d <s-value> where <s-plain-text> |
e <if-let-equation-mode> <s-plain-text-with-equals> |
f <s-property-name> |
g <if-table-column-expected> <s-table-column-name> |
h <s-action-pattern-as-value> |
i <s-value-phrase-non-of> |
j <s-adjective-list-as-desc> |
k <s-purely-physical-description> |
l <s-table-reference> |
m member/members of <s-description> |
n member/members of <s-local-variable> |
o <s-property-name> of <s-value> |
p <if-pronoun-present> <possessive-third-person> <s-property-name> |
q entry of/in/from <s-value> |
r <s-description> |
s <s-table-column-name> |
t <s-value-phrase>

That might be mostly Greek to you, but you can see that variables take top priority while phrases are rock-bottom, unless they happen to not contain the word “of”, in which case they rate just above a list of adjectives.

In other words: no, reading text like “A of B” as a property takes precedence over reading it as a “to decide what” phrase.

Whew! Yes, that was Greek to me, but I’m lucky in that I don’t need the player property MONEY. I only need (and want) to calculate the money amount on the fly.
I removed that line and all is fine.

Only minor caveat. Instead of a coin being silver or gold,
I made each a kind so that when taking INVENTORY, I get the list of silver and gold coins, instead of a total of all coins.

> i
You are carrying:
a pouch (open)
3 gold coins
12 silver coins

Decide phrases are only evaluated when they are explicitly used. So,

To decide the money of player does not imply a universal mathematical relationship between money of player and the result of the decide phrase. Rather, you have to be explicit about it:

To decide new money:
   decide on 3.1415;

Instead of jumping:
    now money of player is new money;

I would not use money of player as a decide phrase name, as it shadows the original meaning of money as a property of player.

Thank you. I think I killed the problem by removing MONEY as a property of player. As I said in my reply to Celtic Minstrel above, I wanted the money amount to be calculated on the fly.

1 Like

I found a new wrinkle. If any coins are “on the floor” of the room, they are automatically added into the player’s pouch. It seems that IF7 does not allow duplicates to be separated in the same location.

Starter is a room. 

A coin is a kind of thing. 
A gold coin is a kind of coin.
A silver coin is a kind of coin.

A pouch is a container carried by player. 
The pouch is openable and open.
There are 3 gold coins and 12 silver coins in the pouch.

There are 2 gold coins and 9 silver coins in Starter.

After looking:
	if player is carrying a pouch:
		if pouch is open:
			say "Player has [number of coins] coins in his pouch ([number of gold coins] gp/ [number of silver coins] sp).[line break]";	
			say "Player has [money of player] gp in his pouch.";
		otherwise:
			say "You don't know. Your money is closed away in your pouch.";
	otherwise:
		say "The player has no pouch, and therefore, no money.";

To decide what real number is money of player:
	let g be number of gold coins in pouch carried by player;
	let s be number of silver coins in pouch carried by player;
	let X be given by X = g + 0.1s where X is a real number;
	decide on X.

Note that the code focuses on what’s in the pouch, and not in the room. Note also that the floor coins are not added into the total, which means IF7 makes some kind of distinction. When play starts, that is, the room is LOOKED at, I get the following response:

Starter
You can see 2 gold coins and 9 silver coins here.
Player has 26 coins in his pouch (5 gp/ 21 sp).
Player has 4.2 gp in his pouch

EDIT: Sorry. My stupid mistake. I forgot to add “in pouch” to the say statements. Everything is working fine. That means that duplicates can be processed separately. :slight_smile:

1 Like