Discussion:
Dynamically creating objects in Inform 7
(too old to reply)
Kristopher Neidecker
2007-04-22 02:35:06 UTC
Permalink
I am new to this group and figured I would say hi to all the various IF
writers here before throwing any questions around :)

So...Hello! I have been diving into many of the great IF creations listed
in Bafs Guide and the IF Comp lists and I give my respect to each and every
one of you.

Now my question is probably elementary but I cannot find any info in the
docs for it. I used to play around with Inform 6 about 5 years back or
more, and loved it, but never really sat down to work on anything but little
tests and putter around a bit. I found Inform 7 recently and was blown
away. I am working on a project now (::insert collective sigh over another
first Inform project Oo ::) and have run into one thing I cant seem to
figure out compared to Inform 6. In 6 I was able to dynamically create
items, and dynamically destroy them, so 1 prototype grain of sand turned
into an infinite pile of sand, etc. Like:
****
Class sand(200)
with name "sand" "grain",
short_name "Grain of sand",
initial "You see a grain of sand",
plural "Grains of sand",
create
[ x; move self to beach;
],
destroy
[ x;

],
after
[x; Take: sand.create();
Drop: sand.destroy(self); print "You loose the sand.^";rtrue;
];
****
That is not a real good use for it but there it is anyway. So you could use
one prototype monster and spawn him where needed.

So is there a way to do this through native 7 code or will I need to dip
into 6 code to replicate this? I could make a stack of 50 or 100 objects
and move them into play when I needed to or some sort of work-around, but
that is inefficient to the max for a larger game, with a bunch of
randomization or something similar. Thanks for the help.
Eric Eve
2007-04-22 07:40:46 UTC
Permalink
Post by Kristopher Neidecker
I am working on a project now (::insert collective sigh over
another first Inform project Oo ::) and have run into one thing I
cant seem to figure out compared to Inform 6. In 6 I was able to
dynamically create items, and dynamically destroy them, so 1
prototype grain of sand turned into an infinite pile of sand, etc.
So is there a way to do this through native 7 code or will I need
to dip into 6 code to replicate this? I could make a stack of 50
or 100 objects and move them into play when I needed to or some
sort of work-around, but that is inefficient to the max for a
larger game, with a bunch of randomization or something similar.
Thanks for the help.
I fear the quick answer is that there isn't a way to dynamically
create objects in I7. I'm not sure whether dipping into I6 would
help with this either; you could always experiment, but I seem to
recall that other recent posts here have mentioned problems with
using pure I6 objects in I7 code. You may be forced to use some kind
of workaround: either creating a stack of objects as you suggest
(the 'Nickel and Dimed' example in the documentation does this), or
finding an alternative approach that doesn't involve multiple copies
of the same object.

You might want to take (another?) look at section 4.4 (Kinds:
Duplicates) in the I7 documentation. In particular it states:

"The second caveat is that excessive duplication is expensive in
memory and running time. It is perfectly legal to say

In the Lab are 75 triangles.


but the resulting game may be a little sluggish: and Inform draws
the line at 100, refusing to create more duplicates than that in any
single place. If we really need more than about fifty duplicated
objects - say, a tombola containing raffle tickets numbered 1 to
1000 - it is usually better to find some less literal way to
simulate this: for instance, only having a single raffle ticket, but
with a randomly chosen number on it."

-- Eric
Emily Short
2007-04-22 09:00:19 UTC
Permalink
Post by Eric Eve
I fear the quick answer is that there isn't a way to dynamically
create objects in I7. I'm not sure whether dipping into I6 would
help with this either; you could always experiment, but I seem to
recall that other recent posts here have mentioned problems with
using pure I6 objects in I7 code.
Yes, I'm not sure this would work. But there's also no reason to do
it. I6's "dynamic creation" is fake; all 200 instances of "sand" in
Kristopher's example would be created, but just kept out of play until
needed. Inform 7 is just more straightforward about what's going on.
If you want 200 thingies, make them and put them offstage somewhere
until you're ready to use them. This will cost some memory, but so
would the I6 equivalent.
Kristopher Neidecker
2007-04-23 00:28:14 UTC
Permalink
Post by Emily Short
Post by Eric Eve
I fear the quick answer is that there isn't a way to dynamically
create objects in I7. I'm not sure whether dipping into I6 would
help with this either; you could always experiment, but I seem to
recall that other recent posts here have mentioned problems with
using pure I6 objects in I7 code.
Yes, I'm not sure this would work. But there's also no reason to do
it. I6's "dynamic creation" is fake; all 200 instances of "sand" in
Kristopher's example would be created, but just kept out of play until
needed. Inform 7 is just more straightforward about what's going on.
If you want 200 thingies, make them and put them offstage somewhere
until you're ready to use them. This will cost some memory, but so
would the I6 equivalent.
Well that would explain it a bit wouldn't it. It has been 5+ years since I
made that cheesy test bit back there and I saw the (200) and thought to
myself "Nahhhhh, couldn't be" but there is is.

Ok so if that's the case then let me ask 1 more bit based off of that. In
the guts of compiled I7, when a game is played on an interpreter, do those
200 or so objects take up no, or limited at least, computation cycles until
they are 'in play'? Or will a PalmOS or similar limited device choke up on
something like that even when the items are not in play at the moment.
Basically when something is offstage is it still making a significant
performance hit. AI routines (that's a loose term in this case of course)
could be told simply to ignore anything not in play, so that's not a
problem, but I am concerned about the general overhead.

As an addendum, I have NO desire to swamp a room with a literal stack of 100
things or anything like that. But lets say for example (and this has
nothing to do with my current project, but may be used in the future) you
wanted a randomized dungeon with some randomized monsters or whatever. The
easiest way to something like that is by spawning a thing in a room from a
stack or dynamically so you may have more than one of a duplicate creature
running around.
Emily Short
2007-04-23 02:31:11 UTC
Permalink
Post by Kristopher Neidecker
Ok so if that's the case then let me ask 1 more bit based off of that. In
the guts of compiled I7, when a game is played on an interpreter, do those
200 or so objects take up no, or limited at least, computation cycles until
they are 'in play'?
Well-- that depends quite a lot on the code you write yourself. If
you're writing loops that apply to all things, then yeah, your stack
of 200 off-stage granola bars (or whatever) will affect the processing
speed, whether or not they're in play. The added items may also cause
some mild slowing of the standard library, but I suspect that will be
a pretty minor effect, and that mostly the speed burden (if any) will
come from what you write yourself. So it's up to you to optimize.
Kristopher Neidecker
2007-04-23 05:43:34 UTC
Permalink
Post by Emily Short
Post by Kristopher Neidecker
Ok so if that's the case then let me ask 1 more bit based off of that.
In
the guts of compiled I7, when a game is played on an interpreter, do those
200 or so objects take up no, or limited at least, computation cycles until
they are 'in play'?
Well-- that depends quite a lot on the code you write yourself. If
you're writing loops that apply to all things, then yeah, your stack
of 200 off-stage granola bars (or whatever) will affect the processing
speed, whether or not they're in play. The added items may also cause
some mild slowing of the standard library, but I suspect that will be
a pretty minor effect, and that mostly the speed burden (if any) will
come from what you write yourself. So it's up to you to optimize.
Figured as much then, thanks. Mmmmm, 200 granola bars. :)

Loading...