ID:906508
 
Keywords: loops, objects, variables
(See the best response by GreatFisher.)
Code:
mob/verb/grabLoot()
for (var/k=1,k<=8,k++)
//find free inventory slot or abort proc
if (freeInventory()!=0)
var/freeSlot = freeInventory()

//transfer loot item to player inventory & set loot list slot to null
src.loot[k].locale="inventory"
src.equipment[freeSlot] = src.loot[k]
src.loot[k]=null

//send message to player if inventory is full & abort proc
else if (freeInventory()==0)
src<<"SYSTEM: Inventory is full."
return


Problem description:
In the code above, I am looping through a loot list ("src.loot") and transferring the objects in the loot list to the player's inventory list ("src.equipment"). Upon transferring the object, I want to reset a custom object variable called "locale", but when I try to call the variable from inside the loop ("src.loot[k].locale") I get an expected end of statement error because of the second period. Is there a way to directly call this custom variable for the object from inside the loop without having to pass another variable to the verb? In this case, src is the player (a mob), and the custom variable I'm trying to change belongs to an obj in the player's loot list.
you said the same thing twice, if(freeInventory()==1) might fix it
also put i'm nto sure if i'm 100% correct but put src. before freeInventory()
In response to Joejoe13
Not to be rude but you probably shouldn't be giving out advise if you don't actually know what you're talking about.
Best response
I would say your trying to make it more complicated than it needs to be. I would make a variable of the type that src.loot[k] is, that way you can access it's variables without error.
In response to GreatFisher
GreatFisher wrote:
I would say your trying to make it more complicated than it needs to be. I would make a variable of the type that src.loot[k] is, that way you can access it's variables without error.

Yep... that fixed it. I changed the loop to:

for (var/obj/o in src.loot)


... and then used the "o" to change the variable. Worked just fine. Makes sense when I think about it.

As for making it more complicated than necessary? Well, that's my middle name! Thanks for the help.
Haha no prob.
BYOND doesn't support chaining return values like that, you would have had to do it one step at a time:
var/obj/o = src.loot[k]
o.locale="inventory"
src.equipment[freeSlot] = o
src.loot[k]=null