ID:2315619
 
Code:
var/obj/machinery/computer/shipcomms/connectedConsole

/obj/machinery/computer/shipcomms/attack_hand(mob/user)
var/obj/machinery/computer/shipcomms/sc
for(var/obj/O in stsc)
sc = O
if(sc.id != src.id)
dat += "<center><li>ID: [sc.id]"
dat += "<A HREF='?src=\ref[src];action=viewmessagelogs'>Message Logs</A> | "
dat += "<A HREF='?src=\ref[src];action=sendmessage;[src.connectedConsole = sc]'>Send Message</A></li></center><BR>"


Problem description:
Currently having a problem with this loop behaving odd.

the result of sc.id produces different results, but when I make connectedConsole a reference to sc, it's always the same object within the last iteration in the loop.

What is it I've done wrong? Thanks in advance.

Note: there is other code in the proc (such as var/datum/browser/popup) the reason I left it out is because it would make it easier to read. If it's requested I'll happily add the code onto the post.

Edit: To make things easier, here is the code of the entire class.

var/global/stsc[0]

/obj/machinery/computer/shipcomms
name = "STSC Console"
desc = "A console used for ship to ship communications (STSC)."
icon_screen = "comm"
icon_keyboard = "tech_key"

var
const
STATE_DEFAULT = 1
STATE_VIEWMESSAGELOGS = 2
STATE_SENDMESSAGE = 3
state = STATE_DEFAULT

id //must assigned in New() and not to be changed
messages[0]
obj/machinery/computer/shipcomms/connectedConsole


/obj/machinery/computer/shipcomms/New()
id = stsc.len+1
var/obj/machinery/computer/shipcomms/sc = src
stsc += sc
..()

/obj/machinery/computer/shipcomms/attack_hand(mob/user)
if(..())
return

user.set_machine(src)
var/dat = ""

var/datum/browser/popup = new(user, "shipcomms", "STSC Console", 400, 500)
popup.set_title_image(user.browse_rsc_icon(src.icon, src.icon_state))

switch(src.state)
if(STATE_DEFAULT)
dat += "<ul>"
for(var/obj/machinery/computer/shipcomms/sc in stsc)
if(sc.id != src.id)
dat += "<center><li>ID: [sc.id]"
dat += "<A HREF='?src=\ref[src];action=viewmessagelogs'>Message Logs</A> | "
dat += "<A HREF='?src=\ref[src];action=sendmessage;[src.connectedConsole = sc]'>Send Message message to [sc.id]</A></li></center><BR>"
dat += "</ul>"
if(STATE_VIEWMESSAGELOGS)
dat += "<A HREF='?src=\ref[src];action=default;'>Back</A><BR>"
for(var/datum/Message/M in messages)
if(M.receiver.id == connectedConsole.id & M.sender.id == src.id)
dat += "<center>[M.body]</center><BR>"
dat += "<center>+-----------------------------+</center><BR>"
if(M.receiver == src & M.sender == connectedConsole.id)
dat += "<center>[M.body]</center><BR>"
dat += "<center>+-----------------------------+</center><BR>"
if(STATE_SENDMESSAGE)

var/input = stripped_input(user, "Send a message to [connectedConsole.id].", "What?")

if(!input)
return
state = STATE_DEFAULT

var/datum/Message/M = new
M.body = input
M.sender = src
M.receiver = src.connectedConsole

src.connectedConsole.messages += M
messages += M

state = STATE_VIEWMESSAGELOGS

popup.set_content(dat)
popup.open()

/obj/machinery/computer/shipcomms/Topic(href, href_list)
if(..())
return

usr.set_machine(src)

if(!href_list["action"])
return

switch(href_list["action"])
if("default")
state = STATE_DEFAULT
if("viewmessagelogs")
state = STATE_VIEWMESSAGELOGS
if("sendmessage")
state = STATE_SENDMESSAGE

src.updateUsrDialog()

/obj/machinery/computer/shipcomms/Del()
stsc -= src
..()
What is stsc? What's in it?
In response to Spunky_Girl
it's just a global list used to store objects of type shipcomms
Why not just
for(var/obj/machinery/computer/shipcomms/sc in stsc)

instead of
var/obj/machinery/computer/shipcomms/sc
for(var/obj/O in stsc)

?

Written as you have it, any variables, procs, verbs, etc accessbile as a /shipcomm typing, are inaccessible as just a /obj typing.

May we see the shipcomm class code?
In response to Spunky_Girl
it was originally that, but some messing around with another previous problem lead me to try that, I solved the problem, but it didn't make a difference to have it worked anyway so I left it as is

And sure

Edit: I've added the entire class code into the post just incase

/obj/machinery/computer/shipcomms
name = "STSC Console"
desc = "A console used for ship to ship communications (STSC)."
icon_screen = "comm"
icon_keyboard = "tech_key"

var
const
STATE_DEFAULT = 1
STATE_VIEWMESSAGELOGS = 2
STATE_SENDMESSAGE = 3
state = STATE_DEFAULT

id //must assigned in New() and not to be changed
messages[0]
obj/machinery/computer/shipcomms/connectedConsole
I've been looking at this completely wrong this whole time. My fault. The reason connectedConsole is the "same object as the last one iterated" is because it's only a single variable. If you want to store all of the looped connectedConsoles, you need to use a list variable.
var/list/stored_consoles = new
for(var/obj/machinery/computer/shipcomms/sc in stsc)
stored_consoles += sc
In response to Spunky_Girl
ok, while your solution didn't exactly solve the problem, it did clear something up that I had mistaken, so let me show you what I've come up with, a long with an example of where and why I went wrong.

this is intended to be a button in the window, I had the intention that [src.connectedConsole = sc] would just run when the button is clicked, this is where I went wrong in assuming that sc is just an intance (which I realized was wrong thanks to Spunky_Girl), and with each iteration of the loop, the instance would still be different.
dat += "<A HREF='?src=\ref[src];action=sendmessage;[src.connectedConsole = sc]'>Send Message message to [sc.id]</A></li></center><BR>"




This is the fix. It now actually looks at sc as a reference and passes it to the Topic() proc, and uses it as I originally intended.
dat += "<A HREF='?src=\ref[src];action=sendmessage;target=\ref[sc]'>Send Message message to [sc.id]</A></li></center><BR>"


I'm not really sure if I made the fix clear, I could have misspoken at some point, or missed something. PM or something if so.

And thanks for the help Spunky_Girl.
In response to HeadyBucket
Your code looked like it wanted to store each an every shipcomms object it looped through (since I saw you setting a variable and were confused why it was only storing the last one found and not all of them).

Login to reply.