ID:2127529
 
(See the best response by Multiverse7.)
Code:
var/obj/ball/b = new/obj/ball() in locate(3, 3, 1)


Problem description: I want to create a variable obj/ball/b that equals a ball object but only if that is in a specific location (3, 3, 1). I use the above code but it gives error, if I erase the in locate and after code it works though but I want the obj in the above loc...

It's very simple. You just pass the locate() call as an argument of new(). It assumes that the first arg you pass will be the loc.

Also, you already defined the type as part of the variable definition, so you don't need to include the type again.

var/obj/ball/b = new(locate(3, 3, 1))


The in operator is only used to check if something exists in a list or if a number falls within a certain range:
if(item in list)

if(x in 1 to 10)

You can use for() to iterate through lists or numbers with the same logic.
Um, the first code will create an obj/ball in pos (3, 3, 1. What I am trying to do is: In loc 3, 3, 1 it may be already a ball obj or it may be not, because you know balls move here and there. If it does exist, assign it to a variable b so I can then call some functions ie b.Move() and such...
Best response
If I'm understanding this right, you want to create a new ball only if one doesn't already exist at that location?

// Set t as the turf.
var/turf/t = locate(3, 3, 1)

// Try to find a ball at that turf.
var/obj/ball/b = locate(/obj/ball) in t

// If no ball was found there.
if(!b)
// Create a new one.
b = new(t)
In response to Multiverse7
It sounds to me like he doesn't actually want to create a new ball, he just wants to locate one that may already exist.

OP: You definitely shouldn't use the "new" keyword if you don't actually want a "new" object instance.
yes, just locate it not create a new one
In response to Bassnectar
Look up the locate() proc in the DM Reference. Multiverse's example includes it, too.
Yes, Multiverse's code works fine and is very informative!
I made it to work with only one line of code though (using locate as you mentioned), can you guys please tell me if it's correct because I am somewhat new to byond:

var/obj/ball/b = locate() in locate(3, 3, 1)
if (b)
//...


Seems to work for me but does this hide any danger?
In response to Bassnectar
That's a pretty good way of doing it... if you need it.

If you need there to be a ball there, it wouldn't necessarily help to include the if-statement. For example, if the entire game depends on that ball being there, you might not want the game to continue, because someone forgot to put the ball there. If it's important enough, you could crash the proc (or throw an exception):
var obj/ball/b = locate() in locate(3, 3, 1)

// if the ball doesn't exist, crash the proc (the proc won't continue past here)
if(!b) CRASH("Where's the ball!?")

// ... (ball exists)

If you can take a step back and can determine that you should be "remembering" a ball rather than "searching for" a ball, then it would be more appropriate (and more efficient) to already have a reference to the ball, e.g. as soon as the ball is created:
// Reference to the ball that we need to keep in mind
var obj/ball/the_important_ball

// Call this when the game starts
proc/StartGame()
the_important_ball = new (locate(3, 3, 1))

proc/DoSomething()
// Assuming the game has already started,
// we should already have a reference to the ball,
// so there's no need to search for it.
the_important_ball.Explode()

An alternative to having a "direct" reference to the ball is to have a "soft" reference through the datum.tag variable. The advantage of this is that you can set it in the map editor, rather than in some proc in the code. The major disadvantage is that it requires a string in the code that matches the string in the map editor (see "stringly typed", as opposed to "strongly typed").
proc/StartGame()
// this part isn't necessary if you set the tag of a ball in the map editor
var obj/ball/the_important_ball = new (locate(3, 3, 1))
the_important_ball.tag = "the important ball"

proc/DoSomething()
var obj/ball/the_important_ball = locate("the important ball")
the_important_ball.Explode()

Of course, maybe none of these apply to your situation.