Howdy, Stranger!

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

Cloning an Object?

This works (but not what I want to do):
<<print "[[" + "Look at the " + $LocalCustomers[$_r].Sex + "|WHO" + "][$LookAt ='" + $LocalCustomers[$_r].Mood + "']]">>
I have a Widget which compares all NPC/Objects to a specific location and then outputs those objects into an array called $LocalCustomers.  Then using an array in a loop I output that list of NPC who are in the same location- each having a passage link as the above, so the player can click on an individual one for further description.

The above code works as it is currently shown here- which places a property for Mood into $LookAt, this is passed to the passage WHO with no probs. But I want to access another property belonging to the npc/object also.

So I want to pass/clone the prototype object onto $LookAt so I can.

This doesn't work:
[code]<<print "[[" + "Look at the " + $LocalCustomers[$_r].Sex + "|WHO" + "][$LookAt ='" + $LocalCustomers[$_r] + "']]">>
[/code]

It seems $LookAt is taken as just a variable and a reference to property.

In StoryInit I have $LookAt defined as <<set $LookAt ={}>>

With that code above in the passage WHO if I have <<print $LookAt.Mood>> it's blank, no errors are thrown.  Granted I am still getting to grips with Objects and I am sure in the past I was able to reference or set an object to be the same as another and be able to access the properties with it, in sugarcane anyway.

I am using Sugarcube -3105.  I hope all this makes sense?





Comments

  • You cannot pass a object reference via a string.  And 99.99% of the time you don't want to be passing around its serialization either.

    If $LocalCustomers will exist for the duration of what you're doing, then simply pass the index into it instead.

    For example: (assuming that $_r is the (integer) index into the array; if it were a (string) key instead, then you'd have to quote it)

    <<print "[[Look at the " + $LocalCustomers[$_r].Sex + "|WHO][$LookAt = " + $_r + "]]">>
    Then in your WHO passage:
    <<print $LocalCustomers[$LookAt].Mood>>
  • TheMadExile wrote:

    You cannot pass a object reference via a string.  And 99.99% of the time you don't want to be passing around its serialization either.

    If $LocalCustomers will exist for the duration of what you're doing, then simply pass the index into it instead.

    For example: (assuming that $_r is the (integer) index into the array; if it were a (string) key instead, then you'd have to quote it)

    <<print "[[Look at the " + $LocalCustomers[$_r].Sex + "|WHO][$LookAt = " + $_r + "]]">>
    Then in your WHO passage:
    <<print $LocalCustomers[$LookAt].Mood>>


    Thanks MadExile, that worked perfectly :)  I was almost banging my head against the wall.

    Just for purposes of understanding better, why is most of the time its not a good idea? Do you mean like if you make changes to the master or parent object when a game is running, any clones based off it wont be updated?



  • Dazakiwi38 wrote:

    Just for purposes of understanding better, why is most of the time its not a good idea? Do you mean like if you make changes to the master or parent object when a game is running, any clones based off it wont be updated?


    That's one of the reasons (and the reverse is also true), yes.  Another would be that even if you're not planning on making modifications to the original object, and will simply be using it as a read-only resource, you don't want to be creating additional copies of it that you don't need.
  • TheMadExile wrote:

    Dazakiwi38 wrote:

    Just for purposes of understanding better, why is most of the time its not a good idea? Do you mean like if you make changes to the master or parent object when a game is running, any clones based off it wont be updated?


    That's one of the reasons (and the reverse is also true), yes.  Another would be that even if you're not planning on making modifications to the original object, and will simply be using it as a read-only resource, you don't want to be creating additional copies of it that you don't need.


    Okay thanks for that clarification. :)
Sign In or Register to comment.