ID:178747
 
I want to randomly place objects on only one kind of turf icon. I can do this if I can generate a list of xyz locations of these turf and then use pick() to select one. Is there a proc that would help me do this?

-Shwn
Shwn wrote:
I want to randomly place objects on only one kind of turf icon. I can do this if I can generate a list of xyz locations of these turf and then use pick() to select one. Is there a proc that would help me do this?

I would create a list of all turfs of that particular type and update it as those turfs are created. That way when you are placing the random objects, you may quickly access the list without bogging down the game to locate the turfs.

var/list/obj_spawn_list = list()

turf/obj_spawn
New() // called when this turf is created
..() // do the normal stuff
obj_spawn_list += src // add this turf to the obj_spawn_list

obj/proc/random_spawn()
// will move the obj on a random obj_spawn turf
if(!obj_spawn_list.len)
world.log << "obj_spawn_list is empty!"
return
loc = obj_spawn_list[rand(1,obj_spawn_list.len)]

(I changed the topic. You are selecting turfs, not turf icons. That's a different problem.)
In response to Shadowdarke
obj/proc/random_spawn()
// will move the obj on a random obj_spawn turf
if(!obj_spawn_list.len)
world.log << "obj_spawn_list is empty!"
return
loc = obj_spawn_list[rand(1,obj_spawn_list.len)]


Thanks, I understand everything except for the part above. Actually I understand all of it except for his line.

loc = obj_spawn_list[rand(1,obj_spawn_list.len)]

What is that line doing and how would this proc be called to place the object?
In response to Shwn
Shwn wrote:
obj/proc/random_spawn()
// will move the obj on a random obj_spawn turf
if(!obj_spawn_list.len)
world.log << "obj_spawn_list is empty!"
return
loc = obj_spawn_list[rand(1,obj_spawn_list.len)]


Thanks, I understand everything except for the part above. Actually I understand all of it except for his line.

loc = obj_spawn_list[rand(1,obj_spawn_list.len)]

What is that line doing and how would this proc be called to place the object?

rand(1,obj_spawn_list.len) returns a random number between 1 and the number of items in the list. That number is used as an index in the list obj_spawn_list, so that you now have a random item from the list.

Of course, all of that is much simpler to do with:

loc = pick(obj_spawn_list)
In response to Shwn
Shwn wrote:
obj/proc/random_spawn()
// will move the obj on a random obj_spawn turf
if(!obj_spawn_list.len)
world.log << "obj_spawn_list is empty!"
return
loc = obj_spawn_list[rand(1,obj_spawn_list.len)]


Thanks, I understand everything except for the part above. Actually I understand all of it except for his line.

loc = obj_spawn_list[rand(1,obj_spawn_list.len)]

What is that line doing and how would this proc be called to place the object?

That line places the obj in a random loc from the obj_spawn_list. I believe you can do the same thing with this line:

loc = pick(obj_spawn_list)

In response to Shadowdarke
loc = pick(obj_spawn_list)


Then you should be able to do something like this in a verb

Random_Spawn
var/dest = locate(pick(obj_spawn_list))
new /obj/someobj(dest)

And it shoudld create the object on a random location in the spawn list not?

I did this and it does not create the object on the map. I put some print statements in the verify that the list is being populated and it is picking a location from the list. The object just will not apear. Also the object icon has only 1 icon state so I know thats not the problem.

In response to Shwn
Shwn wrote:
loc = pick(obj_spawn_list)


Then you should be able to do something like this in a verb

Random_Spawn
var/dest = locate(pick(obj_spawn_list))
new /obj/someobj(dest)

And it shoudld create the object on a random location in the spawn list not?

I did this and it does not create the object on the map. I put some print statements in the verify that the list is being populated and it is picking a location from the list. The object just will not apear. Also the object icon has only 1 icon state so I know thats not the problem.

Hard to say what's going on without seeing more of the code. You say that obj_spawn_list is being populated, but are you sure that it's being populated with valid turfs?

You might want to try a few more debugging world << lines to see what's going on. For example:

obj/someobj
New()
..()
world << "[src] was created at [x], [y], [z]."
In response to Shwn
Shwn wrote:
var/dest = locate(pick(obj_spawn_list))

if obj_spawn_list is populated with turfs, you do not need locate() in that line. If removing it doesn't fix the trouble, put a debug message in there to see what dest is.
In response to Shadowdarke
Well I found the trouble with the code. The object was being created but was invisable. My object icon has only 1 state has an animation. Its a rock that slowly fades from red to blue and back to red. When I removed the animation frames and left it as a plain icon it creates the object fine. What do I need to set when the object is created to allow the animation?