ID:269695
 
I have made up this code:
proc
RandPlace(NameT,Num)
var/X = rand(1,world.maxx)
var/Y = rand(1,world.maxy)
for(var/i = 0, i != (Num + 1), ++i)
X = rand(1,world.maxx)
Y = rand(1,world.maxy)
new NameT(locate(X,Y,1))

This code works though I want it so that the objects dont get created at the location of others like traps not getting created on walls. I have tried using a number of for commands but none of them worked. Could someone help me.
ADT_CLONE wrote:
I have made up this code:
> proc
> RandPlace(NameT,Num)
> var/X = rand(1,world.maxx)
> var/Y = rand(1,world.maxy)
> for(var/i = 0, i != (Num + 1), ++i)
> X = rand(1,world.maxx)
> Y = rand(1,world.maxy)
> new NameT(locate(X,Y,1))
>

This code works though I want it so that the objects dont get created at the location of others like traps not getting created on walls. I have tried using a number of for commands but none of them worked. Could someone help me.

Just check if something is already in the way before placing the object :P.

proc
RandPlace(NameT,Num)
for(var/i = 0, i < Num, ++i)
var/theobj/O = new NameT()
while(!O.Move(locate(rand(1,world.maxx),rand(1,world.maxx),1)));


Though only use this method if there are a lot of locations the object can be place otherwise it may be really slow or even lock up. If you only have a few possible locations it can be geneated at you may want to just store a list of these locations and use pick to select one. You might also have to write your own Enter() proc for the turf atom to disallow these objects to enter if they can.
In the object traps code, use a :

New(){for(var/turf/T in view(src,0)){if(T:name == "Wall"){del(src)}}}


Or something to that degree. That's an easy fix, though if you want it built into the code you just provided, just ask.
In response to Theodis
Its a turf and not an object(that is the trap). Also it comes up with the error O.Move Undefined(or something like that).
In response to FinalFantasyFreak
That wont work as trap is an turf and will destroy wall which is also a turf when it is created.
ADT_CLONE wrote:
for(var/i = 0, i != (Num + 1), ++i)


This is something you should never do in a loop except in cases where it's absolutely required. I refer to the use of the != operator here.

What if something in your loop causes i to skip past Num+1, or modifies Num so the same thing happens? This loop would continue forever.

Clearly you want to bring i only up to Num, and then stop, so the test should be:
for(var/i = 0, i <= Num, ++i)


This is much much more robust.

However, since you're only trying to place Num items, not Num+1 of them, you should either start i at 1 or use i<Num instead. The loop as you have it, and as I've correct it, will both run Num+1 times.

Lummox JR