Howdy, Stranger!

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

Another Array Question

It's about 3 a.m. and I think my mind has went out on me.

It's easy to remove an object from a list/array when you know what that object is:
<<set $foo = "foo">>
<<set $bar = "bar">>
<<set $baz = "baz">>
<<set $inv = [$foo, $bar, $baz]>>
<<set $inv.splice($inv.indexOf($bar),1)>>
So, $bar is removed. No problem.

However, what if all objects have the same name (e.g., $item)?

I'm sooo sleepy right now that's about as well as I can pose my question. I'll probably wake up tomorrow and figure it out, but I thought I would ask just in case.

Thanks!

Comments

  • Like this:
    <<set $foo = "foo">>
    <<set $bar = "bar">>
    <<set $baz = "baz">>
    <<set $inv = [$foo, $bar, $baz, $bar]>>
    <<print $inv>>
    <<print $inv.length>>
    <<set for(var i = $inv.length; i--;){
    alert(i);
    if ($inv[i] === $bar) $inv.splice(i, 1);
    }>>
    <<print $inv>>
  • No . . .  I'm sorry. I just can't figure out at all how I can even start to word my question correctly (but I'm not trying to get rid of all my $items). I'm not even going to try. Going to bed. Thanks anyway.
  • This line from your example

    <<set $inv = [$foo, $bar, $baz]>>
    Is the same as writing

    <<set $inv = ["foo", "bar", "baz"]>>
    Because the array contains the values that were assigned to the $foo, $bar, $baz variables, not pointers to the original variables.

    This can be seen by doing the following, in which the array will contain the same values even though one of the variable is changed.

    <<set $foo = "foo">>
    <<set $bar = "bar">>
    <<set $baz = "baz">>
    <<set $inv = [$foo, $bar, $baz]>>
    <<print $inv>>
    <<set $foo = "bob">>
    <<print $inv>>
    <<print $foo>>
    The slice line of your example is looking for value currently stored in $foo not a name, so if you changed $foo to equal "bob" it would NOT remove the "foo" value from the $inv array because it would be looking for "bob" which is not in the array.

    So it does not matter if you use the same variable ($item) each time you add something the $inv array because it is just adding $item's current value to the array.
  • Ninja'ed by greyelf. That was just an example. I'm not trying to do that. The objects in my list have several properties.

    Probably crazy talk at this point. Gah, 5 till 4 a.m.

    Attached is screenshot. I printed the "$i" from the loop (in white) beside the item's equipping link. If the player clicks an item, it will equip (of course, I won't know what one they want to equip). I want to then remove it from the list. Can't figure out how. Brain now dead.

    So, if the player clicks "axe" I want to remove $list[1] because that's where it is in the list (see attached pic).
  • So is it a variable like this that you want to delete?
    <<set $foo = {name: "foo" }>>

    I.e., it has a property, name, that is "foo", rather than a variable called "foo"?
  • Sounds like you need to search the array for an object with the name "axe" to get it's index and then use splice on the index.

    If there can only ever be one axe, you can use a simple reverse lookup to find it:
    ::acquireitem
    <<set $item to parameter(0)>>

    <<if $lookup[$item.name] is 0>>
    <<set $inv.push($item)>>
    <<set $lookup($item.name) to $index.length - 1>>
    <<endif>>

    ::purgebyname
    <<set $item_name to parameter(0)>>

    <<set $ix to $lookup[$item_name]>>

    <<if $ix gt 0>>
    <<set $inv.splice($ix)>>
    <<set $lookup[$item_name] to 0>>
    <<endif>>
    Only tricky bit is that you need to put a fake item 0 in their inventory, as $lookup will return 0 for an unknown item.  Code is untested, but should give you the general idea.
  • Wow. Didn't even remember I made this post.

    Super quick reply as I have to leave.

    The wrote:

    So is it a variable like this that you want to delete?
    <<set $foo = {name: "foo" }>>


    Yep.

    mykael wrote:

    Sounds like you need to search the array for an object with the name "axe" to get it's index and then use splice on the index.


    I have its index number (it's in white in the image). However, from experience, I don't think <<set $inv.splice($ix)>> will do anything different than <<set $inv.splice()>>.
  • Try using $inv.splice($ix, 1) and tell it how many to extract, it should work if your $inv is an array and not a "associative array"

    One note about using splice() is it is not support by IE < 9, so it will fail for people using IE on WinXP which may not be an issue for you.
  • I'm on my phone real quick but are you sure that is valid syntax?  That just doesn't look like that will work to me.
  • Try this (but as noted, wil not work on early IE):
    <<set for(var i = $inv.length; i--;){
    if ($inv[i].name === $bar) $inv.splice(i, 1);
    }>>
  • I believe you two got me where I needed to go. I'll report back after some testing. Thanks a lot for helping me.
  • After some testing, like, maybe a solid hour of doing nothing but buying, selling, equipping and unequipping, repeat, repeat, repeat, I can say that I have strong confidence in my inventory system now.

    I may report back yet again to this thread in the future giving a more information. I know inventory systems are of wide interest, but I'm not ready to release how I'm handling it just quite yet. A lot of tweaking and testing left to do. :)

    Thanks again, guys! I really appreciate the help! :)
Sign In or Register to comment.