ID:1350466
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
One quick question about New() which I don't see documented anywhere, I was able to do it once but don't remember how I was able to do it.

What if I want to assign a variable such as (loc, myCustomVariable1, icon, etc.) at New() without explicitly passing it through and reassigning it?

Here's how I'm doing it now...
var/mob/guard/G = new("specialGuard1[src.class]", locate(1,5,1), src)

mob/guard/
New(var/customState, var/myLoc, var/mob/owner)
src.icon_state = customState
src.loc = myLoc
src.masterOwner = owner


Isn't there a shortcut to say, this is icon_state, this is loc, this is the masterOwner, and automatically assign them?
I don't understand what is wrong with this method, or what you are trying to achieve.
There's this syntax:
new /type { var1 = const1; var2 = const2 } (args)

new /type {
var1 = const1
var2 = const2
} (args)

But it can only be used with constants. It's kind of like defining a type within something by starting a line with a slash, and inserting that modified type into the new instruction.

Is there any reason you need a shortcut, instead of doing something like:
var mob/guard/G = new
G.var1 = a
G.var2 = b
G.var3 = c

// or
var mob/guard/G = makeGuard(a, b, c)

proc/makeGuard(var1, var2, var3)
var mob/guard/G = new
G.var1 = var1
// etc.
return G
You can't implicitly initialize a datum's constructor in DM. The current way you're doing it is the only way to do it. If I understand correctly, what you're trying to do is this:

mob/guard/
New(src.customState, src.myLoc, src.owner)


Opposed to setting the datum's values inside the body of New().

In response to Albro1
Albro1 wrote:
I don't understand what is wrong with this method, or what you are trying to achieve.

Basically, instead of writing it the above way, I wanted to wrote it as:

var/mob/guard/G = new(icon_state = "specialGuard1[src.class]", loc = locate(1,5,1), owner = src)
In response to Flame Sage
The new instruction just sends the arguments to New(). If you want to do that, then you'll have to add all the arguments and handle it properly in New().
Right, I understand that, I was just wondering if there was a way to quickly do mapping like in ID #4 without having to manually take care of it on the New() side.
Erm...no, not that I know of. You only need to do the New() once though, so it shouldn't be that much of a hassle.
Moved and requested as a feature request
I do have concerns about "yet another syntax" being introduced for this, as you probably can't overload the = form you gave in your example (I'm sure you know that anyway).

However conceptually, some shortcut seems fine and 'RAD'ish.
I think this would be best to change the args[] to support named arguments even when they do not exist defined. Then the developer would be able to control them how they want.

In this case, it would be overwriting New to loop though args[] and set the vars[] based on that.
I'm not sure I see a way to automate this. That is, I know it could be automated by checking args and vars in the default New() (as long as ..() was called), but that would be a relatively expensive thing to do every time a new object was created, when only a handful of your objects would need this.
Just throwing an idea out here -

If proc.args was an associative list which kept the arg name; you could technically write something like that on your own, without doing specific variables, I'd think.

datum/New()
for(var/x in args) if(!x || whatever && specific cases)
src.vars[x] = args[x]


But...that's not the case, so...shrug.
In response to Super Saiyan X
It'd have to be backwards to not break backwards compatibility, but it can't be because of numerical argument values.
I was thinking about this, it doesn't need to be native because we could do the following to get the same effect, right?

var/obj/dynParams/dp = new(list("paramList"="True","icon"='icon.dmi',"icon_state"="something","other_stat" = 15));

datum/New(paramList)
if(!istype(paramList,/list) || !paramList["paramList"]) ..(); //if the list doesn't contain paramList item, then it needs to behaive like it is supposed to.
else
for(var/x in paramList)
if(x == "paramList") continue;
if(!vars[x]) continue; //if not a var, we can't add new ones.
vars[x] = paramList[x];
That's not exactly the same thing though, and I was looking for a native solution. :)