Maximum value question

Say I wanted each npc to have a friendship percentage. I want the value to max out at 100 and drop over time. How would I put an upper bound on friendship such that it doesn’t overflow to something above 100. I’m pretty new to this, sorry if this is a foolish question.


That depends what language you are using. Is it TADS, or Inform 7, or Gruescript…? Or one of the other ones.


The general pattern is pretty straightforward, it’s just something like this:

friendship = friendship + newFreiendship
if friendship > 100:
    friendship = 100

inform7 whoops. Forgot to include that.

1 Like

If you just want to track one friendship, something like this will work. If you want more than one you should make it a property of the thing being befriended (and then the every turn rule should become a loop over the things).

"Leaping into Friendship"

Laboratory is a room.

Friendship is a number that varies. Friendship is initially 0. [ Starting value. ]

Every turn:
	If friendship > 0:
		Now friendship is friendship - 1;
	say "The current friendship is [friendship].".

To increase friendship by (N - a number):
	Now friendship is friendship + N;
	If friendship > 100:
		Now friendship is 100;

Instead of jumping:
	say "You jump into friendship!";
	Increase friendship by 30;

And if you want to track multiple friendships?
I’m trying to use this code but i keep getting this:
Problem. Before reading ‘Loyalty is a number that varies’, I already knew that ‘Loyalty’ is a nothing valued property, and it is too late to change now.

i want loyalty, or friendship, to be a property of every person in the game, except the player.

or even of the player as well, maybe. i could work that out, actually.

Please let me know what your existing code is that refers to Loyalty, 'cause I don’t know how one ends up with a nothing-valued property.

The thing you want is:

a person has a number called loyalty.

But like I said, before replacing it, what is it you have now?

The player would end up with a loyalty property with the above. If that’s an actual problem, you could do something like:

an NPC is a kind of person.
an NPC has a number called loyalty.

and then make your npcs as, well, npcs instead of persons. You could even re-parent the existing subkinds of person to NPC if you’re using them…

a man is a kind of NPC.
a woman is a kind of NPC.
an animal is a kind of NPC.

so the line of code referring to loyalty is the fourth line in my program after “when play begins”

A person has a number called loyalty. Loyalty is a number that varies. Loyalty is initially 0.

that’s the line. Loyalty isn’t referred to anywhere else in the code until i can get it to work here.

ah, assuming you’re using 9.3/6M62 or 10.1, I think you got a word wrong when transcribing the error message. It does, sensibly, say numbers valued property.

anyway, to make a property, one wants

A person has a number called loyalty.

To make a global variable, one wants

Loyalty is a number that varies. Loyalty is initially 0.

You never want both of those: as you’ve seen, if you try it with the same identifier, it doesn’t work.

1 Like

But now when i want to increase a character’s loyalty it gives me this:

**Problem.** In the sentence 'increment jack's loyalty', I was expecting to read a value, but instead found some text that I couldn't understand - 'mom's loyalty'.


**Problem.** You wrote 'increment jack's loyalty', but 'jack's loyalty' is a value, not a place where a value is stored.

For example, if 'The tally is a number that varies.', then I can 'increment the tally', but I can't 'increment 37' - the number 37 is always what it is. Similarly, I can't 'increment the number of people'. Phrases like 'increment' work only on stored values, like values that vary, or table entries.

I was trying to match this phrase:

 increment (jack's loyalty - a stored value) !

But I didn't recognise 'jack's loyalty'.

try loyalty of jack instead.

same response

sorry, I didn’t read your post closely enough. You don’t need a phrase to manipulate it. You can just say, e.g.,

now the loyalty of jack is 0;
increment the loyalty of jack;
decrease the loyalty of jack by 10;

Or if it’s a complicated calculation you could do something like:

To make (p - a person) more loyal:
 [something, something] loyalty of p [something]

When you declare a phrase, the thing to the left of the hyphen is just an arbitrary name for a variable local to the phrase. jack's loyalty there means exactly as much as x would: it doesn’t inherently have anything to do with jack or with a loyalty property.