ID:2112190
 
(A bit noob and silly question, may delete post after some hours)

Problem description: I have var/x = "object1" and I want to create an object like this, based on the value of x, var/obj/object1/y = new/obj/object1(), I could use ofc:

if (x == "object1")
var/obj/object1/y = new/obj/object1()
else if (x == "objext2")
var/obj/object2/y = new/obj/object2()


And keep going but thing is I have like 1000 objects and the if-else-if lines would be huge. Is there any more fast way?
(PSA: Please don't delete resolved threads)

The easiest, but least-safe way, is to use text2path():
var type_path = text2path("/obj/[x]")
var obj/y = new type_path // possible runtime error if type_path doesn't exist

A safer way would be to not use text2path(), but provide an associative list to choose from:
var available_types[] = list(
object1 = /obj/object1,
object2 = /obj/object2,
"some other object" = /obj/some_other_object,
)

// ...

var x = input(...) in available_types
var chosen_type = available_types[x] // x == "object1" or "object2"
var obj/y = new chosen_type

Only thing you can't do is declare variables using a variable type. You'll have to use the "greatest common factor" ancestor of all the potential types, such as /obj. See "polymorphism".
With the new keyword, you don't actually even need to use text2path(); from time immemorial it can take strings with the path name just as easily, as long as the string is valid.