ID:1222588
 
Keywords: loop, obj, while
(See the best response by Stephen001.)
Code:
verb/loop(obj/o as obj in usr.loc)
while(expression)
function(o)

verb/function(obj/o as obj in usr.loc)


Problem description:

Guys I'm in a loop problem. I have a main loop that runs the entire program, but certain verbs called from the loop need an obj argument. But since it is inside a loop the object has to change in each iteration. How can I supply the function with the current obj in usr.loc in each iteration?
verb/loop()
var/list/L = new()
for(var/obj/O as obj in usr.loc)
L.Add(O)

var/index = 1
var/max_index = L.len

while(expression)
function(L[index++])
if(index > max_index) index = 1

verb/function(obj/o as obj in usr.loc)
verb
loop()
while(expression)
for(var/obj/O in usr.loc)
function(O)
Where the hell is the sleep on these while loops? jk lol
I've thought of that but it stores the obj in the users first location and I have a moving user. Sorry if I didn't explain my issue well on my first post.
Do you have a more real-world code example we could look at? It's a bit difficult to understand what you are doing at the moment, and why.
Ohh man it's really complicated. In a few words I have a bot I designed that is controlled by certain variables which are altered when data is inputted to the bot. This data is contained within objects and in the loop I call the functions which gather data from the obj in usr.loc. So since the bot moves around the map I must provide the function with the obj that the bot stands on in that moment.
I'd probably suggest you try to avoid the dependency on usr.loc then, if at all possible. Presumably this data is inputted into the bot quite infrequently? For example, a player has to actually click on the bot and edit the details?
No it's autonomous and gathers data every second in order to operate autonomously. It does not only receive data from the obj in usr.loc, but I need to know the obj that the bot stands on at any time so I can call certain functions.
See my first code. o stands for obj in usr.loc. But usr.loc changes when usr moves and I need the o at any moment. Learning about the project and the other functions that are called will only confuse you. Focus on the issue. Also thanks for the interest in helping.
Presumably then, the bot should be gathering data from src.loc, it's own location, and using that as the argument to functions?
Normally you are right but I am experimenting and I need to also control the bot directly through the usr. So usr is the bot.
PS. I am not developing a game. So other mobs aside from the user won't be needed.
Best response
So in that instance, the bot would take values from the person controlling it? My inclination would be, you pass the object controlling the bot in, then, on each iteration of your loop:

verb/loop(mob/controlling)
while(expression)
for (var/obj/O in controlling.loc)
function(O)


This means the loc freely changes (as controlling moves about etc) and on each iteration of the loop, you pick up the objects at their current location, and apply it to all of those.
Thanks man this worked!
Excellent! Glad I could help.
Sorry for opening this again but does the same apply for turfs too?
I'd assume so, yup.
It wont' work for me.
I'm doing this
Code:
for(var/turf/t as turf in usr.loc)
function(t)

and it won't work. It won't even get past the for() line
usr.loc IS the turf. You're searching for a turf inside a turf.
You're right but what can I do to efficiently find the turf the usr is standing on?
Page: 1 2