ID:160894
 
I want to make a proc that check all mobs in the game and checks who they are fighting. I want it to make it check to see if the person they are fighting is still online. If so it lets them still be fighting them but, if they are not online it sets them to fighting null. thanks!
mob
var/mob/opponent //The mob they are fighting, null if nobody.
verb/StartFight(mob/M as mob in view()) //Initiates a fight
src.opponent=M
M.opponent=src

proc/CheckOpponent() //Checks if the person they're fighting is still online. Returns 0 and ends the fight if they aren't online, or if they aren't fighting anyone.
if(src.opponent && src.opponent.client)
return 1
if(src.opponent)
src.opponent.opponent=null
src.opponent=null
return 0

proc/UpdateFights()
for(var/mob/M)
M.CheckOpponent()
In response to Nickr5
Nickr5 wrote:
>   proc/CheckOpponent() //Checks if the person they're fighting is still online. Returns 0 and ends the fight if they aren't online, or if they aren't fighting anyone.
> if(src.opponent && src.opponent.client)
> return 1
> else if(src.opponent)


Rule #2 of programming: In addition to making sure code isn't unnecessarily copied, do not have your code repeat itself needlessly.

(Also, 'else' is naturally unneeded after 'return')
Fixed:
>   proc/CheckOpponent() //Checks if the person they're fighting is still online. Returns (null) and ends the fight if they aren't online, or if they aren't fighting anyone.
> if(src.opponent && src.opponent.client)
> return 1
src.opponent = null
>


More importantly (I realize this was requested by the OP, so this in reply to him), this kind of design is bad, and unneeded. You can use a much better, simpler approach.
1) Simply don't let players fight non-players.
2) If your game doesn't delete player mobs on logout and therefore you need to manually clear the opponent var (note that if the mob is deleted, the var is automatically cleared internally!), simply do it whenever the player logs out of the game, eg:
mob/Logout()
if(src.opponent)
//this can be kind of confusing to understand, but:
//set the logging-out player's enemy's 'opponent' var to null
src.opponent.opponent = null
return ..() //do the usual stuff


Depending on your game, something more like this might be needed:
mob/Logout()
for(var/mob/M) //loop through all the mobs in the game
if(M.opponent == src)
M.opponent = null
return ..()
In response to Kaioken
Kaioken wrote:
Nickr5 wrote:
> >   proc/CheckOpponent() //Checks if the person they're fighting is still online. Returns 0 and ends the fight if they aren't online, or if they aren't fighting anyone.
> > if(src.opponent && src.opponent.client)
> > return 1
> > else if(src.opponent)

Rule #2 of programming: In addition to making sure code isn't unnecessarily copied, do not have your code repeat itself needlessly.

(Also, 'else' is naturally unneeded after 'return')
Fixed:
> >   proc/CheckOpponent() //Checks if the person they're fighting is still online. Returns (null) and ends the fight if they aren't online, or if they aren't fighting anyone.
> > if(src.opponent && src.opponent.client)
> > return 1
> src.opponent = null
> >

More importantly (I realize this was requested by the OP, so this in reply to him), this kind of design is bad, and unneeded. You can use a much better, simpler approach.
1) Simply don't let players fight non-players.
2) If your game doesn't delete player mobs on logout and therefore you need to manually clear the opponent var (note that if the mob is deleted, the var is automatically cleared internally!), simply do it whenever the player logs out of the game, eg:
> mob/Logout()
> if(src.opponent)
> //this can be kind of confusing to understand, but:
> //set the logging-out player's enemy's 'opponent' var to null
> src.opponent.opponent = null
> return ..() //do the usual stuff

Depending on your game, something more like this might be needed:
> mob/Logout()
> for(var/mob/M) //loop through all the mobs in the game
> if(M.opponent == src)
> M.opponent = null
> return ..()



well here is the exact problem i run into, when i reboot the mud, i have it so only one monster is set on the map period..well sometimes when i reboot. theres 2 mobs loaded and it follows me and still tries to fight me...now i have the repop time for 10 mins.. i just want to nullify if that does happen so other players can fight it. let alone try to figure out why 2 mobs load instead of 1.
In response to Rikishi
Please don't fully quote big posts when it is unneeded. As long as you click the appropriate Reply button, people can still know which post you are replying to.

As for your problem (which I don't fully understand, well, due to your explanation and its spelling and punctuation... or the lack of it), does your game save and load mobs (either player-controlled or Non-Player-Controlled)? If so, your problem probably occurs because when an object that has a reference to another object, that other object is also saved into the savefile, and subsequently loaded. Combating this is simple: define all the variables that you do not wish to be saved (such as opponent) as temporary. This way this variable is not saved, neither are other objects.
In response to Kaioken
after more testing, it seems when a player logs out, it does not log them out of the game, who still records them as being on and there body is still ther
In response to Rikishi
This is the case by default. If you do not want this functionality in your game, then you need to manually delete the player's mob after he has logged out, yourself.
In response to Kaioken
Kaioken wrote:
This is the case by default. If you do not want this functionality in your game, then you need to manually delete the player's mob after he has logged out, yourself.


mob/Logout()
del(src)



is that it or do i got the wrong idea?
In response to Rikishi
That looks just fine to me. =) It may possibly cause expected behavior if your game has something like a Take_Over() verb to allow a player to login into a different mob or something - but otherwise, it should work.
In response to Kaioken
hmm well its not deleteing them with that ...
In response to Rikishi
It could be that you have overridden the Del() procedure and have caused them to not get deleted. Or, it could be that you have another Logout() override, that does not call ..() and so prevents your new one from executing. Note that if that is the case you should best combine it into a single override, because multiple ones in the same project can only be problematic, as well as untidy.
In response to Kaioken
well there is only one Logout(). so its not that

maybe this?

client/Del()
if (istype(src.mob, /mob/other/choosing_character))
return ..()


but even that seems right
In response to Rikishi
This is it - the mob/Logout() proc is actually called by default from client/Del(). Your override of that procedure prevents the parent/default behavior (the above) in the case that the mob type is NOT /mob/other/choosing_character, since only if it is, then ..() is called.

Since that procedure override doesn't seem to serve any specific purpose, you can just delete it in whole to fix the problem.
In response to Kaioken
here's something really odd.

it seems like its saving the monster next to the player. when i reboot the monster i was last fighting also appears even tho there should only be one in the game.

it just seems like it leaves the icon, i have Dblclick() set and it does not work on the one that loads with the player, but works with the one that is suppose to load. hmm any idea?
In response to Rikishi
Did you read this post? [link]
Also, you're going to need to get rid of the old savefiles that already have monsters saved in them, in order for the changes to take effect.
In response to Kaioken
true, but making them temp..umm screws up the whole game why, cause those vars are actually used in the combat system.
In response to Rikishi
There's no reason it would. The only thing marking a var as tmp does is cause it to not get automatically saved (which is what you need to solve your problem).
In response to Kaioken
but when i defined as
mob/temp/var/player_found = null

i got 60+ errors
In response to Rikishi
This is because you've done it very wrong. READ the page I've linked to in this post: [link]