ID:263631
 
Code:
obj
Friends
Friend
name = ""
Status = ""
Level = 0
Race = ""
Class = ""
Source = ""
var/src = /obj/Friends/Friend
verb
Remove_Friend()
set category = null
switch(alert("Are you sure you wish to remove [src.name] from your Friends List?","Removal Confirmation","Yes","No"))
if("Yes")
usr << "You have removed [src.name] from your Friends List"
del(src)
else
return

mob
verb
Add_Friend()
var/friendname = input("Who would you like to add to your freinds list?","Add Friend") as text
for(var/mob/Player/M in world)
if(friendname == "")
src << "You must enter a name to add."
else
if(M.Name == friendname)
src.contents += new /obj/Friends/Friend
for(var/obj/Friends/Friend/O in src.contents)
if(O.name == "")
O.Source = M.ckey
O.name = M.Name
O.Level = M.Level
O.Race = M.Race
O.Class = M.Class
O.suffix = "Level [O.Level] [O.Race] [O.Class] - [O.Status]"
Check_Friend_Status(O)
else
src << "There appears to be no person called [friendname] logged in."

mob
proc
Check_Friend_Status()
for(var/mob/Player/M in world)
if(M.Name == src.name && M.ckey == src.Source)
src.Status = "Online"
sleep(300)
return .(Check_Friend_Status(src))
else
src.Status = "Offline"
sleep(300)
return .(Check_Friend_Status(src))


Problem description:The problem is that the status of the friend does not get updated for some reason.

I am testing this by adding myself as a friend then renaming my character.

This system is attempting to make use of a Statpanel just so you know, hense why O has a suffix.

put in spawn while(1)
in the proc?
also...get rid of the return deals, they aren't needed
and why are you waiting 30 seconds to update?
also, the reason it isn't updating is because it only asks for the variables once and then it keeps them. You need to put that in another proc that repeats.
                        for(var/obj/Friends/Friend/O in src.contents)
if(O.name == "")
O.Source = M.ckey
O.name = M.Name
O.Level = M.Level
O.Race = M.Race
O.Class = M.Class
O.suffix = "Level [O.Level] [O.Race] [O.Class] - [O.Status]"
Check_Friend_Status(O)

see it doesn't update because it only asks once, you need to make it refresh
Oh, ew, your for() loops there need to be fixed.

First off, the if(friendname == "") line should be moved OUTSIDE the loop, and then you should put a return statement in it. There's no reason to loop through all the players in the world if you haven't entered a name.

Next, when you add a friend to your contents, you don't need to loop through friends to find it again. Just create it as such:

var/obj/Friends/Friend/O = new(src)


Then O will be in your contents and you can modify its variables.

Next, your Check_Friend_Status proc. It doesn't accept an argument, but you seem to be acting as if it does. On top of that, it's calling itself recursively, resulting in an infinite loop, and an eventual stack overflow.

My overall suggestion, however, is that you instead make this event-driven. Whenever a player logs in or out, or changes their level, you should update the status of any Friend objects pointing to them. This is a bit involved, but it's the best way to do it, and I'm sure you can manage it yourself.

Also... why the var/src line?
In response to Garthor
I'm not sure, i'm basicly trying to learn the language again after about 4 years from a few old projects i had.

As far as the even driven update how would you propose i do that, as like i said im trying to relearn the language but i'm a bit stuck. It's the procs you see i'm not very good with them.
In response to MrMase_Ezonan
Like i said, put the spawn while(1) at the top of your proc and place the variable deal that changes the object variables in the said proc

get rid of the return and change your sleep time to like 10 or something.
In response to WarLin
I'm not to sure what you mean Warlin, could you post it in code form so i get a clearer picture, sorry i'm just very rusty
In response to MrMase_Ezonan
Here you go =)
obj
Friends
Friend
name = ""
Status = ""
Level = 0
Race = ""
Class = ""
Source = ""
var/src = /obj/Friends/Friend
verb
Remove_Friend()
set category = null
switch(alert("Are you sure you wish to remove [src.name] from your Friends List?","Removal Confirmation","Yes","No"))
if("Yes")
usr << "You have removed [src.name] from your Friends List"
del(src)
else
return

mob
verb
Add_Friend()
var/friendname = input("Who would you like to add to your freinds list?","Add Friend") as text
for(var/mob/Player/M in world)
if(friendname == "")
src << "You must enter a name to add."
else
if(M.Name == friendname)
src.contents += new /obj/Friends/Friend
for(var/obj/Friends/Friend/O in src.contents)
if(O.name == "")
O.Source = M.ckey
O.name = M.Name
O.Level = M.Level
O.Race = M.Race
O.Class = M.Class
O.suffix = "Level [O.Level] [O.Race] [O.Class] - [O.Status]"
O.Check_Friend_Status()
else
src << "There appears to be no person called [friendname] logged in."

mob
proc
Check_Friend_Status()
spawn while (1)//this makes it repeat and stuff
for(var/mob/Player/M in world)
if(M.Name == src.name && M.ckey == src.Source)
src.Status = "Online"//you have to update all of the variables all of the time. every time this repeats it should update the variables
src.name = M.Name
src.Level = M.Level
src.Race = M.Race
src.Class = M.Class
src.suffix = "Level [O.Level] [O.Race] [O.Class] - [O.Status]"
sleep(10)
else
src.Status = "Offline"
sleep(10)

I didn't have time to test it and I don't want to screw with it all that much but... this should work.
In response to WarLin
Sorry WarLin but it didn't work when i changed it, maybe if you can test your to make sure it's not something i've done would be appreshiated.
In response to WarLin
It won't, and it's poorly designed, and it's going to hog a whole buttload of resources.
In response to Garthor
Well can you design a better one Garthor, the only problem i'm having is making the Proc do the update, if you can show me how to implement the proc i can do as you suggested and make it event based, which is what i tried to do but again it didnt work, for example:

mob
proc
Event_Update(var/mob/Player/M in world)
for(var/obj/Friends/Friend/O in M.contents)
if(O.name == M.Name && O.Friend_ckey == M.ckey)
O.Level = M.Level
O.Race = M.Race
O.Class = M.Class
O.Status = "Online"
else
O.Status = "Offline"
In response to MrMase_Ezonan
You wouldn't have such a proc at all, if it was event-driven.

Whenever the status of a player changes, go through all the Friend objects in the world (easiest way, and hopefully you won't have hundreds of them). If they're pointing to the player, then update the status.

Additionally, whenever a Friend object is loaded, you'll also need to search for the player its pointing to, and update.

So, an update proc would work like this:

obj/Friends/Friend/proc/Update(var/mob/Player/M)
if(!M)
Status = "Offline"
else
Status = "Online"
Level = M.Level
Race = M.Race
Class = M.Class


It's just a matter of calling Friend.Update(Player)
In response to Garthor
Ok iv tried that but it still doesnt seem to work, here is how i am trying to implement it.

obj
Friends
Friend
proc
Friend_Update(var/mob/Player/M)
if(M.Name == name && M.ckey == Friend_ckey)
Level = M.Level
Race = M.Race
Class = M.Class
Status = "Online"
else
Status = "Offline"


This is being called from
mob
verb
Add_Friend()
var/friendname = input("Who would you like to add to your freinds list?","Add Friend") as text
if(friendname == "")
src << "You must enter a name to add."
else
for(var/mob/Player/M in world)
if(M.Name == friendname)
src.contents += new /obj/Friends/Friend
for(var/obj/Friends/Friend/O in src.contents)
if(O.name == "")
O.Friend_ckey = M.ckey
O.name = M.Name
O.Level = M.Level
O.Race = M.Race
O.Class = M.Class
O.suffix = "Level [O.Level] [O.Race] [O.Class] - [O.Status]"
O.Friend_Update(M)
else
src << "There appears to be no person called [friendname] logged in."


Can you see where I've gone wrong Garthor?
In response to MrMase_Ezonan
Yes. Friend_Update() has to be called whenever a player does something that requires them to be updated. So, in mob/Player/Logout(), mob/Player/Login(), and mob/player/levelup(), at least.

And, once again, you didn't fix that problem when adding a new friend like I said you did. You do NOT need to loop through contents when creating a new friend to find it. You just created it! You can easily get a reference to it just by setting var/obj/Friends/Friend/O = new()!

Also: your Friend_Update() proc isn't updating the suffix.

And it isn't checking if M exists at all, like it should be. Friend_Update() should ONLY be called if M is the proper friend, it only needs to check if M is null (indicating the friend has logged out).
In response to Garthor
Ok thanks for that Garthor, but i have found another problem while testing the Add Friend Verb with more than one user online.

for(var/mob/Player/M in world)
if(M.Name == friendname)
var/obj/Friends/Friend/O = new(src)
if(O.name == "")
O.Friend_ckey = M.ckey
O.name = M.Name
O.Level = M.Level
O.Race = M.Race
O.Class = M.Class
O.Status = "Online"
O.suffix = "Level [O.Level] [O.Race] [O.Class] - [O.Status]"
else
src << "There appears to be no person called [friendname] logged in."


The problem here is i now get the "There appears to be no person called [friendname] logged in." message for each mob that does not have the correct name. Is there a way i can keep this message if there is no person in the world with the correct name but not recieve it if there is?
In response to MrMase_Ezonan
The if(O.name) line is useless. And there's no reason to output that at all, really. You can set Status to "Offline" before entering the loop at all, then it will still be "Offline" when the loop exits. You'll also want a default suffix along the lines of "hasn't been seen online". Default as in obj/Friends/Friend/suffix = "hasn't been seen online". As soon as you see them online, it'll be replaced with data, and that data will be saved with your character.