ID:149562
 
I can not seem to get this code to work correctly. Background: Every intercom has a different sname like "outside intercom". I get bad index errors
Send_To(msg as text)
set src in view(1)
var
list
globalintercom
intercomchooser
for(var/obj/electronics/messagenet/reciever in world)
globalintercom[reciever.sname] = reciever
intercomchooser += reciever.sname
var/chose = input("Which intercom would you like to broadcast to?","Intercom Chooser") in intercomchooser + "nothing"
var/obj/reciever = globalintercom[chose]
if(reciever)
view(reciever) << "[usr]:MessageNet:[sname] sends, [msg]"
Exadv1 wrote:
I can not seem to get this code to work correctly. Background: Every intercom has a different sname like "outside intercom". I get bad index errors
Send_To(msg as text)
set src in view(1)
var
list
globalintercom
intercomchooser
for(var/obj/electronics/messagenet/reciever in world)
globalintercom[reciever.sname] = reciever
intercomchooser += reciever.sname
var/chose = input("Which intercom would you like to broadcast to?","Intercom Chooser") in intercomchooser + "nothing"
var/obj/reciever = globalintercom[chose]
if(reciever)
view(reciever) << "[usr]:MessageNet:[sname] sends, [msg]"

The problem may be that you're using two different lists when you only need one. You set up the globalintercom list as an associative list, but then you're also using a second list of names (intercomchooser) that's not associative; that's the one you're passing to input().

Your problem may be fixed by eliminating the redundant intercomchooser list, and changing all the other references to globalintercom:
Send_To(msg as text)
set src in view(1)
var/list/globalintercom
for(var/obj/electronics/messagenet/reciever in world)
globalintercom[reciever.sname] = reciever
var/chose = input("Which intercom would you like to broadcast to?","Intercom Chooser") in globalintercom + "nothing"
var/obj/reciever = globalintercom[chose]
if(reciever)
view(reciever) << "[usr]:MessageNet:[sname] sends, [msg]"

The other thing I'd watch out for: Make sure sname isn't a number. If it's a number, list association won't work the way you expect it to, and that will definitely give you a bad index. If sname is a number, then change the list to go by "[receiver.sname]" instead of receiver.sname directly.

Lummox JR
In response to Lummox JR (#1)
Here is the entire code. An example sname is north building.
obj
electronics
icon = 'electronics.dmi'
var
on
messagenet
var
sname as text
icon_state = "mnrecieving"
verb
Broadcast(msg as text)
set src in view(1)
for(var/obj/electronics/messagenet/M in world)
view(M) << "[usr]:Broadcasts on MessageNet, [msg]"
Send_To(msg as text)
set src in view(1)
var
list
globalintercom
for(var/obj/electronics/messagenet/reciever in world)
globalintercom[reciever.sname] = reciever
var/chose = input("Which intercom would you like to broadcast to?","Intercom Chooser") in globalintercom + "nothing"
var/obj/reciever = globalintercom[chose]
if(reciever)
view(reciever) << "[usr]:MessageNet:[sname] sends, [msg]"