Using the same code over and over

I am using the same piece of code over and over is there an easier way other than copy paste change the small thing that needs changed.

Before going to More Parking Lot:
clear the screen;
display the figure lot2 centered;
continue the action.

The things that change are bolded and italicized.

Is there a table or array that could be made? Im looking to tighten up code,.

1 Like

This is a basic mockup that works on my system. There are probably plenty of rough edges. (Also, I’m not sure which extension you’re using to get the centered qualifier to work, but it should be fine, I’d guess, if you add it back into this example.)

Include Basic Screen Effects by Emily Short.

A room can be pictorial. A room is usually not pictorial.

Figure 1 is the file "parking lot.jpg". 

Figure 2 is the file "grocery.jpg".

Moore Parking Lot is a pictorial room. Les Groceries is a pictorial room. Les Groceries is west of Moore Parking Lot.

Before going to a pictorial room (called the destination):
	clear the screen;
	let the wonderful drawing be the illustration corresponding to a location name of the destination in the Table of Room Illustrations;
	display the wonderful drawing.
	

Table of Room Illustrations
Location Name	Illustration
Moore Parking Lot	Figure 1
Les Groceries	Figure 2

Making a room not pictorial by default lets you only add illustrations as necessary, but if your plan is to have an illustration for absolutely every room, you can drop all references.

Does that help?

1 Like

That’s a good way to do it. Another way is to make the picture a property of the room:

Figure of nothing is the file "empty_image.png".
A room has a figure name called the illustration. The illustration of a room is usually the figure of nothing.
Definition: a room is illustrated if its illustration is not the figure of nothing.

Before going to an illustrated room (called the place):
    clear the screen;
    display the illustration of the place centered;
    continue the action.
2 Likes

Yes this helps Ill give it a try. Thanks!

1 Like

These are the best solutions in this case, but in the future, if you have a specific piece of code you use a lot, Inform 7 has a tool called Phrases that acts like functions.

You define phrases with inputs like this:

To destroy (destructee - a thing):
    say "[The destructee] has been demolished!"; 
    now destructee is nowhere;

Then you can call it later like this:

Report pushing the red button:
    destroy the stone wall;

So in this case, ‘destroy’ is the name of the function/phrase.

Again, I don’t recommend using it for your specific need, as I think the other two answers are better. But I wanted to put it out there because I had a lot of useless copy/pasted code before.

3 Likes

There’s an extension Location Images by Emily Short that does this automatically – you just need to declare that The room-illustration of Some Room is Figure of SomeRoom.

It attaches to carry out looking rather than before going so that the look command will restore the location image, for example if you have illustrations for examining objects that could sometimes replace it with something else. (And also because before going will still trigger even if the move is prevented by another rule, which can cause unexpected behaviour.)


Incidentally, if you have a look at the Kinds tab you can see that the default value for a figure name is Figure of cover (which is the cover image for your story, which can’t actually be displayed unless the game is released anyway). Assuming that you don’t want to use the cover image as a room illustration at some point (which is usually a reasonably safe assumption), you can exploit this to remove the need for a dedicated empty image:

A room has a figure name called the illustration.
Definition: a room is illustrated if its illustration is not the figure of cover.
2 Likes