Create a new object when action is carried out.

So in my game, I have a mining action, and as a part of the puzzle I want to use a rock obtained from mining the wall.

My problem is that I want mining to be an action that will always mine a rock from the wall. I.e. It creates a new object called rock. I don’t really know how to dynamically create instances of objects.
I’ve had some experience with standard coding languages, and in those you could create a list of objects then append a new object each time it was run, would you do something similar?

Here’s a draft piece of code of what I essentially want, however it obviously doesn’t work.

[code]rock is a kind of thing. the description of rock is “A lumpy rock”.

After examining creator:
a rock is in place;
say “Poof, a rock appeared”.

place is a room.
creator is a thing in place.[/code]

I’m trying to go ahead with the list idea but am failing to implement it. In python it would look something like this:

total_rocks = [] def add_rock: r = rock(location, description etc) total_rocks.append(r)

And this is what I have in Inform;

let total rocks be a list of stones. After examining creator: now r is a stone; r is in location; add r to total rocks; say "Poof, a rock appeared".

if someone is able to point out the differences/similarities of the actual solution I’d love that.

In standard Inform, you can’t create objects during the course of play. If you want to really be able to dynamically create objects, you need to use the extension Dynamic Objects by Jesse McGrew.

However, that’s almost never something you need to do. A better idea is to create a limited store of rocks and move one of them to the room when you need to. (I haven’t tested this code, though.)

[code]rock is a kind of thing. the description of rock is “A lumpy rock”.

place is a room.
creator is a thing in place.

Rock Storage is a room. Ten rocks are in rock storage.

After examining creator:
if a rock (called stone) is in rock storage:
say “Poof, a rock appeared”;
now stone is in place;
otherwise:
say “You certainly don’t need any more rocks right now.”[/code]

And if you ever need to remove a rock from play, you might want to make sure it winds up in Rock Storage.

The way you describe the puzzle, it sounds like the player only needs one rock, though. You might let them mine one rock, and refuse to mine another rock when that rock is in play, with a message that lets them know that if they destroy that rock they can come back and get another.

I concur with matt w. Creating 50 rocks off-stage and just moving them into place when needed is simpler, doesn’t significantly impact the game’s memory requirements, and is indistinguishable from the player’s point of view. Here’s an example that’s been tested:

[code]Test Chamber is a room. The creator is in the Test Chamber.

A rock is a kind of thing.

There are 50 rocks.

After examining the creator:
if no rocks are off-stage:
say “The creator appears to be worn out from overuse.”;
otherwise:
say “Poof, a rock appears.”;
let R be a random off-stage rock;
move R to the Test Chamber.

Test me with “x creator / x creator / x creator / l”.[/code]

Along with what’s been said, sometimes having 50 rocks floating around the game world can be awkward, especially if you have different types of rock and the player wants this one as opposed to that one and has to disambiguate if the player wants to use rocks they are holding or rocks on the ground. You can usually justify not having more by putting limits on the player’s inventory (“You can’t carry more than 50 rocks at a time!”), making nodes “run out” (“You can’t seem to get any decent rocks out of this ore at the moment.”) and shuffling things around a lot.

I did this in Baker of Shireton by having 20 duplicates of each version of dough, bread, and fifty physical coins. I set limits by stopping the player by warning them they should bake some of their dough or sell some of their bread if it was all on-stage, and also forcing the player to automatically tire of carrying large numbers of coins and drop them in a locked strongbox (where they could be shuffled off-stage and recycled to appear again.)

If you have lots of mining and crafting in your game, you might want to not represent every item physically, but numerically. Give the player a “rock bag” container and use values that vary which are reported when the player examines the bag or takes inventory. Have a general “rock ore” object that is moved into the bag when your variable is greater than 0 and off-stage when it is 0. This object should override taking it out of the bag with Instead rules. Optimally you’d have the description for the rock container inventory object say “It’s a bag for collecting rocks. It currently contains 832 coal, 96 diamonds, and 3 feldspar.”

If you don’t want 50 arbitrary rocks floating around, you could force their display name and description through programming. I suppose. I know you can do that with like, rooms that you want their descriptions to arbitrarily change like if you have 50 identical rooms and don’t want the player to realize they are identical.

But I agree, I would create a limit to the number of rocks, especially if you were compiling z-code instead of glulxe.