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
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. 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.
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).
<<set $foo = {name: "foo" }>>
I.e., it has a property, name, that is "foo", rather than a variable called "foo"?
If there can only ever be one axe, you can use a simple reverse lookup to find it: 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.
Super quick reply as I have to leave.
Yep.
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()>>.
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 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!