ID:179966
 
I have come across a nasty bug in my game. In my game, you are able to build cities. But I have seen that people can build cities on top of other cities. Is there a way to check to see if there is another city at a location before you can build one there?
Deadlocke wrote:
I have come across a nasty bug in my game. In my game, you are able to build cities. But I have seen that people can build cities on top of other cities. Is there a way to check to see if there is another city at a location before you can build one there?

Yup!

mob/verb/look_in_location(turf/T)
var/obj/O
for(O in loc) break //now O will contain whatever was there.
if(O)
usr << "There is an [O] there."
else
usr << "That space is empty."
In response to Spuzzum
Thank you again Spuzzum. But I would like to ask if you could explain how the Break proc works as I am taking down notes. I would like to know how it operates :-D
In response to Deadlocke
Deadlocke wrote:
Thank you again Spuzzum. But I would like to ask if you could explain how the Break proc works as I am taking down notes. I would like to know how it operates :-D

The break proc (it's technically a statement, but BYOND refers to everything as procs =) will terminate execution of a certain block of code (only certain code sections are accepted). However, this can be used to your advantage.

Whenever for() loops through objects, it sets the variable equal to the object it is looking for.

eg.

for(var/obj/O in world)
usr << O //O is set to each object in the world, in turn

After the loop is finished, O is set back to null.

But, if you abruptly halt the for loop;

var/obj/O
for(O in world)
usr << O
break

O will not be reset to null because for() will not have finished executing. Thus, O will contain the very first object of that type found.



Note that the following:

for(var/obj/O in world)
usr << O
break

is useless, because the variable "O" only belongs to that block of code. If you tried the following:

for(var/obj/O in world)
usr << O
break
if(O) usr << "There is an obj."
else usr << "There is no obj."

you would get bad var errors for "O".


These are the types of code sections you can use "break" with.

while()
do...while()
for() list (for(var/obj/O in world))
for() loop (for(x = 1, x <= 20, x++))
In response to Deadlocke
A strategy:

Look in the F1 help in Dream Maker. I did it before I even looked ahead and saw your request or spuzzum's answer.

This is what F1 told me:

break proc

Format:
break Label
Terminate the loop with the given label. If no label is specified, the innermost loop containing the break statement is assumed.

Example:
obj/zapper
verb/use()
var/mob/M

for(M in view())
if(!M.key) break

if(!M) M = usr
M << "ZAP!"
del(M)

The zapper object kills the first mob it finds that doesn't belong to a player. If none can be found, it kills the user. Be careful! Note how this code takes advantage of the fact that the loop variable M will be null if the loop terminates normally.

For an example of how to use labeled loops, see the reference section for the continue statement.



That told me just how to use the break proc. Good luck!
In response to Deadlocke
Another thing worth mentioning (which you may have deduced on your own or not) is that by "typing" the variable O, you can specify only a particular type of object to look for.

For instance:

var/O
for (O in...)

will look for anything and everything in the turf.

var/obj/O
for (O in...)

will only look for objs.

var/obj/city/O
for (O in...)

will only look for cities.