ID:1662628
 
Keywords: initial, path, variables
(See the best response by DarkCampainger.)
Problem description:
Is there a way to look for a specific variable from a path?
I would like to avoid having to go back and stick a name variable on for each and every recipe in a long list of recipes. I want it to find the name of the food item from the file path it picks up
example:
// obviously this not my actual code it's just an example
/obj/food/burger
name = "Burger"

/datum/recipe/burger
result = /obj/food/burger

/obj/item/recipe_book
var/foodname = choice_recipe.result
reader << "[foodname]" // this ends up giving the reader the file path which is not what i want
reader << "[foodname.name]" // this of course doesn't work
You could create a new instance of the thing. It's not that bad.
Best response
There's some crazy undocumented behavior with initial() that you can abuse:
var/obj/resultPath = choice_recipe.result
reader << "[initial(resultPath.name)]"


Basically, you create a variable type-cast with a base type that has the desired variable, and store the actual child path you want the value from in it. Then you just use it in initial() as if it were an actual instance of that type.
In response to DarkCampainger
Kaiochao wrote:
You could create a new instance of the thing. It's not that bad.

I don't know what you mean sorry.

DarkCampainger wrote:
There's some crazy undocumented behavior with initial() that you can abuse:
var/obj/resultPath = choice_recipe.result
> reader << "[initial(resultPath.name)]"

Basically, you create a variable type-cast with a base type that has the desired variable, and store the actual child path you want the value from in it. Then you just use it in initial() as if it were an actual instance of that type.

This worked perfectly, thank you. Is there any documentation/tutorials on initial()?
In response to Kaiochao
Kaiochao wrote:
http://www.byond.com/forum/?post=137997
Did Dan do this?

Out of curiosity, I pulled down the oldest BYOND I could find (354) and this method still works! So it's been around for a while.

I marked that feature request as resolved. I had totally forgotten about it.
In response to SodiumShine
SodiumShine wrote:
This worked perfectly, thank you. Is there any documentation/tutorials on initial()?

There's a reference entry for it: initial()

Otherwise, I don't think much has been written about it. The functionality I posted appears to be undocumented.
In response to DarkCampainger
All right thanks!