Can I swap objects between two characters?

I’m trying to figure out best way to swap items between two characters. In other languages swapping objects would be basically:

Create temp T;
Copy Player1 item to t item;
Copy Player 2 item to Player1 item;
Copy T item to Player2 item;

Would this work in Inform 7? Can I create a “temporary” person or object in a “carry out/if” statement action and have it erased once the action is finished or do I need to permanently keep a “temporary” object offstage or in a holding room, just to for use of the action?

Yes, you can, and there are a number of ways to do it, depending on whether the objects to be swapped are specific and known, in which case you can say:now player 2 is carrying object1. now player1 is carrying object2.

If you are just swapping everything each character is carrying, then you could use a loop on things carried for each person. Your principle of having an offstage container is sound. You’d have 3 loops, one transferring from person1 to container, one from person2 to person1, and one from container to person2.

You can do it without writing loops (Inform will do those for you).

The temporary locker is a container.
To switch inventory between (A - a person) and (B - a person):
	now everything carried by B is in the temporary locker;
	now everything carried by A is carried by B;
	now everything in the temporary locker is carried by A.

Example “Pine 3” in the manual chapter 10:7 shows an example of how to completely re-equip a character with different clothing and inventory objects for a flashback scene.

http://inform7.com/learn/man/WI_10_7.html

Thanks folks, two be clear, I did say “objects” but I should have been more specific in saying that it was values, things, relations, etc… attached to the player and NPC, not inventory items.

Seems like it’d be easier just to swap the player to the NPC, by saying “Now the player is Alice.” Then Alice and the old PC should retain their old relations.

For the gameplay i’m working on, it’s on an “as required” type basis. If that makes sense.

Would an easier approach be to store all this info for Player and NPCs in a table, and just swap table values around, than override the examine and other things using these values so that they reference the table and nothing actually held by the player?

In I7, various optimizations are baked into the language at a low level. So the answer to “should I rebuild this fundamental and intrinsic language feature from scratch?” is usually “no”. The object tree is inherent to the virtual machine itself (at least in Z8 mode) and will probably work more reliably than a pure-Inform replacement.

Actually, I’m not sure exactly what you do mean by this. What’s an example of the kind of effect you want?

All I want to do is be able to swap two of the same type of value between two players as required. It’s sort of a scifi type game mechanic, parts of the player bodies are cybernetic and they’re being traded, each piece has certain buffs, debuffs and unique values but all characters have the same modifications, so it was easier to code them as a raw value in the player/npc people classes that I want to have as tradable, instead of creating items in inventory that are passed. you don’t “carry” your arm in inventory, it’s part of you" So i’m going for that effect.

Additionally, for an espionage part, one character was supposed to be able to “swap” names with another character as part of the subterfuge.

To switch arms between (A- a person) and (B - person):
     let A_arm be a random arm which is part of A;
     let B_arm be a random arm which is part of B;
     now A_arm is part of B;
     now B_arm is part of A.

(This is untested.) You’ll need one of these for each type of body part—I don’t think there’s an easy way of doing it generically.

For names:

Every person has a text called the name. Understand the name property as referring to a person.
Rule for printing the name of someone: say "[Name]". 

To switch names between (A- a person) and (B - person):
     let A_name be the name of A;     
     let B_name be the name of B;
     now the name of A is B_name;
     now the name of B is A_name.

(Also untested. I know this doesn’t work in 6G60, but think it should in later builds. I would prefer to have given each person a variable person called the alias (initialized to themself), but I don’t think “Understand the alias property as referring to a person” would work. I haven’t tested it though.)

OK, I think the easiest thing to do would probably be to code the modifications as part of the player (rather than inventory items) and set the buffs/debuffs/values as properties of the parts rather than of the people. Then you can just swap the parts, using code like jrb’s, and everything else will come along automagically.

It’s probably a good idea to have the parts implemented as objects anyway–if which arm (or whatever) you have is an important part of the game, then the player will want to be able to type “x arm” and “x jim’s arm” or something. It’ll be much easier to handle those commands if the arm is implemented as an object in the world. And if it’s a part rather than an inventory item, it won’t show up in inventory listings and the like.

So is the “let” keyword basically creating a temporary variable on the fly? Much like my pseudocode from the start of the thread?

Yes, that’s entirely correct. You were getting more complicated answers to begin with because Inform doesn’t allow you to create temporary objects or things, but temporary variables are OK.