Howdy, Stranger!

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

Objects in an array?

So I've been doodling at this story project for a bit and I've hit a snag.

The game is sf and it contains a planet discovery mechanic: basically you discover a (random) number of planets and can slowly discover more stuff about them. Because each planet will likely end up with a list of different characteristics, I thought it would be best to treat each planet as an object and create them into an array using something like this:
<<set $planets.push([$planet = {size:"giant"}])>>
And this does appear to be working. However, I now want to loop though the array showing a list of planets, and I'm hitting a snag. I tried using code like this:
<<print $planets[1].$planet.size>>
... with the assumption I could change [1] to a loop-iteration number. But it's throwing errors. I clearly have something wrong in my syntax or my assumption of how objects and arrays interact, and I'm not sure where.

Please spot my obvious mistake?  ;D Or suggest an alternate method for the results I want. Believe me, I'm open to suggestion.

Comments

  • You can't use the "$" sigil anywhere other than the front of variable names - so, not object property names.
  • Ok, I am probably very stupid but I'm still not getting what exactly I should change.

    I tried:
    <<set $planets.push(["planet" = {size:"giant"}])>>
    And got:
    [quote]Error: <<set>>: bad expression: Invalid left-hand side in assignment

    Various iterations moving quotes about threw nothing but more errors. Then I thought maybe I'd got hold of the fix from the wrong end, so I changed the push statement back and tried:
    <<print $planets[1].planet.size>>
    And got:
    [quote]Error: <<print>>: bad expression: Cannot read property 'size' of undefined

    I'm sure I'm doing something dumb or flat-out misunderstanding you. Little more help?  ::)
  • @katastrophe
    NOTE: I am assuming you have pre defined $planets as an array somewhere earlier in your code using:
    <<set $planets = []>>
    To add a complex object to the $planets array you would do the following:
    <<set $planets.push({planet:{size:"giant"}})>>
    NOTE: I removed the double quotes around planet, replaced the equals sign with a colon and the square brackets with curly braces.

    To display the planet.size of the first item stored within the $planets array do the following:
    <<print $planets[0].planet.size>>
    NOTE: Because java-script arrays are zero based, you access the first element using Zero, not One.

    I hope this helps
  • @greyelf: WOOT! It worked! And my wee test loop is even working!

    Now to add complexity and shoot myself in the foot again. :D

    Many thanks to both of you for the hand-holding. Someday I will be able to approach a new programming language without instantly tripping over its anal grammar requirements. Someday....
  • Might be simpler to so:
    <<set $planet1 = { size: "giant", name: "Zorg" }>>
    <<set $planet = [ $planet1 ]>>
    --or--
    <<set $planet = []>>
    <<set $planet.push({size: "giant", name:"Zorg"});
    --then--
    <<set $planet = $planet[0]>>
    <<print $planet.name>>
    <<print $planet.size>>
    --or--
    <<print $planet[0].name>>
    <<print $planet[0].size>>
    You can use a var for the index into the planets array (effectively the index number of the planet).

    You can create a display macro and use the parameter function to get the planet index:

    showplanet:
    <<set $px = parameter(0)>>
    <<set $planet = $planet[$px]>>
    <<print $planet.name>>
    <<print $planet.size>>
    --then--
    <<display showplanet 0>>
Sign In or Register to comment.