Referring to object's current location by name

ShadowsEdge19
26 Mar 2022, 09:37

I'm getting stuck trying to use an object's room.name attribute with this error:

"Error running script: Object reference not set to an instance of an object"

roomObj = NPC.parent
msg ("NPC Parent - " + roomobj.name) \\ Returns "Object: NPCRoom1" 
msg ("NPC Parent Object Name - " + roomObj.name) \\ Returns " "
switch (roomObj.name) {
    case ("NPCRoom1") {
        msg ("NPC is in NPC Room 1")
     }
    case ("NPCRoom2") {
        msg ("NPC is in NPC Room 2")
    }
}

Can you please tell me what I'm missing.


mrangel
26 Mar 2022, 10:16

msg ("NPC Parent - " + roomobj.name) \\ Returns "Object: NPCRoom1"

That shouldn't return something weird like the comment suggests – it should give an error, because roomobj isn't defined (your variable is named roomObj, with a capital letter)

msg ("NPC Parent Object Name - " + roomObj.name) \\ Returns " "

That should display the name of the room (presumably outputting "NPC Parent Object Name - NPCRoom1").

switch (roomObj.name) {
    case ("NPCRoom1") {
        msg ("NPC is in NPC Room 1")
     }
    case ("NPCRoom2") {
        msg ("NPC is in NPC Room 2")
    }
}
This should work, but is a weird and inefficient thing to do. You really have no reason to change the object to its name. You can just do:
switch (roomObj) {
    case (NPCRoom1) {
        msg ("NPC is in NPC Room 1")
     }
    case (NPCRoom2) {
        msg ("NPC is in NPC Room 2")
    }
}

ShadowsEdge19
26 Mar 2022, 14:04

Gah, so simple. Why didn't I think it was possible to just use the object themselves in the Switch as cases.
Thanks again.