ID:39585
 
I have been noticing that people now-and-then on the forum create objects such as this:
// 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
Simple neh?

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.
Seems simple enough but imagine if you had to do this a lot and for other things as well.

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!
Again, the above method is best to used for when you keep copy+pasting a same method for a certain object over and over, such as a multi-tiled object.

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!?"
Note that the typical way (for those that did/do not read/remember the DM Entry for istype()) of doing this otherwise is istype(A,/obj/Apple). So if the var path is /obj (ex: var/obj/A), istype(A) would be looking for /obj (in other words, will act like isobj())
You're just over complicating it.
and uh sorry for being a newb, but couldn't you just do var/obj/apple/A = new() without retyping out the path?
That is shown Lord, the first snippet is showing what I see in the forum. The one you mentioned is in the 4th snippet

And yeah, I tend to over-complicate stuff but if you take time to dissect what has been mentioned, you would realize it is not complicated at all, just the way I showed the examples are

Edit: Just edited snippets, adding comments about what the examples are about for those skimmers
GhostAnime wrote:
That is shown Lord, the first snippet is showing what I see in the forum. The one you mentioned is in the 4th snippet

And yeah, I tend to over-complicate stuff but if you take time to dissect what has been mentioned, you would realize it is not complicated at all, just the way I showed the examples are

Edit: Just edited snippets, adding comments about what the examples are about for those skimmers

Get on MSN please.

Legendary Goku 10 wrote:
Get on MSN please.

I am on MSN o_O

GhostAnime wrote:
Legendary Goku 10 wrote:
Get on MSN please.

I am on MSN o_O

All i see is your mobile device.

Check your emails dude.

should have shown how you can add parameters to New(), something lots of people dont know how to do and is really usefully for initializing things.

Plus this is a very elementary thing your teaching but you teach it using very confusing methods, but yeah anywho not bad.