// Example of a bad way.
var/obj/Apple/A = new/obj/Apple()
A.loc = usr.loc
Guess what? There are easier ways to do this!
If you want to make an object without a variable reference, do this:
// No variable
new/__PATH__
new/obj/Apple
If you want to create an object and ONLY want to change the location, you do not need to make it a variable and set to it:
// No varible, loc change
new/__PATH__(__LOC__)
new/obj/Apple(usr.loc)
If you do want to have a variable reference, there's two ways:
1) Specific variable pathway (easier + faster):
// Variable, shortcut creation
var/__PATH__/__VAR__ = new(__LOC__)
var/obj/Apple/A = new(usr.loc)
A.name = "Yum yum yum"
2) A generalized variable (pointless if you think about it; ex: variable pointing to /obj rather than the whole path):
// Variable, generalized path
// Point #1 (above) is recommended over this.
//This is shown as another possible way of defining for something strange
var/__TYPE__/__VAR__ = new/__PATH__(__LOC__)
var/obj/A = new/obj/Apple/A(usr.loc)
A.name = "Yummy yum yum"
Did you know that if you keep making an object and doing a same type of method (ex: create object, add to a client screen), you can make it so when you create it, it'll do the same?
Here's an example of this "old way":
// ALWAYS SAFETY CHECK!
if(!usr.client)return // No client, no point!
var/obj/Apple/A = new // Look Ma, no location!
usr.client.screen += A // Remember, usr is valid to use in very specific places.
A slightly cleaner way (this is best if you have multiple objects in the same parent who will need this type of quick modification, such as making a multi-tiled object - so it joins all the pieces at once without you programming it every time you make it)
var/obj/Apple/A = new(usr) // Wait, why did we use usr? Wouldn't the apple go into the usr's content?
obj/Apple // The path of the apple
New(mob/M) // This is called when this object is made. The item is already moved into the location, which is M here.
if(!M||!ismob(M)||!M.client) // If M is not a mob, is nothing or does not have a client, the object is deleted.
M.client.screen += src // Auto-add!
There's a quick way to check if an object referenced to a variable is a valid path:
var/__PATH__/__VAR__ = __REF__
if(istype(__VAR__)) It is in the __PATH__ branch
else It lied :(
var/obj/Apple/A = __Some Reference__
if(istype(A)) world<<"Yum, apple"
else world<<"o_O What is this!?"