Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Temporary Variables, And Transferring Their Data To Other Variables

Okay, so I'm having a really hard time trying to even explain this one. So if it doesn't make sense I apologize.

So, say I have many different versions (instances) of one base object. In this case, a "Person" object. In JavaScript I've created a random person generator, with differing names, genders etc. I don't know if giving the code will help, but here it is (I've removed some unneeded information) -
window.Person = function() {
this.Name = either("Tom", "Jack", "Jim", "Sarah", "Beth", "Lisa");
this.Met = false;
this.Relation = 50;
this.Greet = function(){
if (this.Met === true){
return either("Hello.", "Hi.", "What's up?", "Hey there, what can I do for you?", "Did you need somthing?", "What's the word?", "Need somthing?");
}
else {
return either("I don't think we've been introduced yet.", "I don't think we've met before.") + " My name is " + this.Name + ", " + "it's good to finally meet you.";
}
};
};
Yes, I know it looks a little sloppy, but I'm learning.

What I've done to make a persistent list of these "People" objects is make a People Array, and pushing new People into it like so
<<set $People = []>>
<<set $People.push( new Person() )>>
And that's all find and dandy. It works just the way I'd expect it to. When I'd want to access a Person's greet or name I'd just go <<print $People[0].Name>> or whatever I'd need.

So now that I've got the background out of the way, here's what I'm struggling with.

When I have more than say, five or six People in the People-Array, things can start to get a bit muddled.

Without thinking, I made a passage to access each individual Person that looks like this -
<<if $People.length gte 1>><<click "First Person" "InspectPerson">><<set $Inspect = $People[0]<</click>><</if>>
<<if $People.length gte 2>><<click "First Person" "InspectPerson">><<set $Inspect = $People[1]>><</click>><</if>>
and so on and so fourth. (I'm writing this write now, so the syntax may have some mistakes but you get the point.)

Then, in the inspect person passage, so then I <<print>> the Persons name and greeting and so on in the Inspect passage. And wow, it actually works.

However, if you've noticed the "Met" property of the Person function, you'll notice that if a person is met for the first time, they will have a different greeting that includes and introduction, and their name. The issue is that in the inspect passage, I <<set $Inspect.Met = true>>.

I quickly realized how dumb that was to do, as it was only setting a value to a temporary variable, and wont be consistent with the actual Person.

I'm fingers are exhausted, so I think I'll just leave it at that. I need a way to transfer changes made to a temporary variable, back to it's original value.
Now thinking about it, it's probably my whole approach to this that's incorrect, so if there's a workaround for what I already have, I'd like to hear it, if not I'd also like to hear what different approach I should make to accomplish the same basic concept.

Thanks for reading, any advice / input would be greatly appreciated.

Comments

  • EcnelOvelam wrote:

    However, if you've noticed the "Met" property of the Person function, you'll notice that if a person is met for the first time, they will have a different greeting that includes and introduction, and their name. The issue is that in the inspect passage, I <<set $Inspect.Met = true>>.

    I quickly realized how dumb that was to do, as it was only setting a value to a temporary variable, and wont be consistent with the actual Person.


    While it's true that $Inspect is a temporary variable, it's a temporary reference variable, since you assigned it a reference type.  Variables come in two types in JavaScript, value types and reference types.  Value types hold simple non-object values (undefined, null, string, number, boolean).  Reference types hold references to objects (which include arrays) or functions.  When you assign a value type from one variable to another you make a copy of the value, however, when you assign a reference type from one variable to another you create another reference to the underlying object.

    You still can't do exactly what you're trying, but it's not because $Inspect is a temporary variable (since it is a reference type).  The problem is that the story formats clone the $variable store between passages, since each history state needs its own copy.  So, when you go from the "access passage", which is where you set $Inspect, to the "inspect passage", which is where you modify $Inspect.Met, both it and the $People[] element it was assigned from have become equivalent but separate clones of the object that both referred to only one passage hence.

    The easy solution is to pass the key/index of the Person object you want to interact with and not a reference to it.  For example, in your "access passage" do something like:

    /% Using <<click>>. %/
    <<if $People.length gte 1>><<click "First Person" "InspectPerson">><<set $Inspect = 0>><</click>><</if>>
    <<if $People.length gte 2>><<click "First Person" "InspectPerson">><<set $Inspect = 1>><</click>><</if>>

    /% Or, using the setter link syntax of the wiki link markup. %/
    <<if $People.length gte 1>>[[First Person|InspectPerson][$Inspect = 0]]<</if>>
    <<if $People.length gte 2>>[[First Person|InspectPerson][$Inspect = 1]]<</if>>
    And then, in your "access passage":

    <<print $People[$Inspect].Greet()>>\
    <<set $People[$Inspect].Met = true>>
Sign In or Register to comment.