ID:262415
 
Problem description:
Basicly, I am trying to get players to switch mobs..
It wont do anything after I select a target >_>
Code:
        Body_Switch()
if(src.Ki>=src.MaxKi)
if(src.Body_Target)
var/mob/M = src.Body_Target
var/Rand = rand(1,10)
if(Rand>=10)
view() << "<center><font color=yellow><font size=-1>[src] has switched bodies with [M]"
src.Ki = 0
var/OtherDude = M
var/You = src
var/mob/NewMob = new/mob/Player(src.loc)
NewMob = OtherDude
var/mob/YouMob = new/mob/Player(src.loc)
YouMob = You
src.client.mob = NewMob
M:client.mob = YouMob
else
src << "You need to click on a target!"
return
else
src << "<center><font color=red>You do not have enough Ki!"
return




src isn't neccesary where you're using it in regards to your variables. For Ki, MaxKi, Body_Target, loc, and client.mob, src is the default. Unless you have global variables with the same name, you don't need to add src to those variables. Also, I don't believe using the colon in M:client.mob is neccesary since you gave the variable M a type path of /mob, and DM knows a mob can have a client, there's no obscurity there. However, if M isn't a mob, it will give you runtime errors.

Now, here's a body switch thing I came up with. I believe it should work, despite my never testing it.

mob/verb/Body_Switch(mob/M as mob in view())
if(!M.client) return //If they don't even have a client, dont' switch them
var/mob/temp = new/mob(loc)
var/mob/oldmob=src
var/mob/Moldmob=M.client.mob
client.mob=temp
M.client.mob=oldmob
client.mob=Moldmob
del temp


Notice how I checked for a client. You should definetely do that. Otherwise, you might get a clientless mob. What you should do is use Body_Target (assuming it holds another mob) directly, rather than putting it into another variable. So you could get rid of var/mob/M = Body_Target and var/OtherDude. Also notice that the only new mob I created was a temporary one to move a client to, in order to free a mob to be moved to. You don't need to move both temporarily, just one. So you can get rid of either YouMob or NewMob, probably YouMob would be best. Also, the temporary mob to be moved to probably shouldn't have an icon, and make sure you delete him once you're done with him.

P_S
In response to Prodigal Squirrel
Nope, the problem with switching mobs is that the procedure will terminate once the mob has logged out who called it.

ie:
Mob A calls Switch()
Mob B is chosen to switch with
Mob A logs into a temp mob
// proces stops here.

No matter how much code you put after the src mob is switched, it will never execute. There are two ways to handle this.

1.) Use a switching manager, basically, a datum that does the switching. You just pass the two mobs to it, and it takes care of the rest.

2.) Use src = null in the mob's switch verb, but be sure to save the src mob to a variable first, so you can still reference it in the process. This will cause the procedure to continue running after the mob has been switched, because it is no longer the src of the process.

Hope that helps. (my first snippet was a mob-switching procedure).

~X
In response to Xooxer
Using the 1st suggestion you made, would I set 2 variables on the datum, and set the 2 players to that variable? The Reason I can't get it to work, is becuase I was thingking...if I just try and switch the mobs, after one switched, then..well it wont work becusae ya...
In response to Prodigal Squirrel
Prodigal Squirrel wrote:
src isn't neccesary where you're using it in regards to your variables. For Ki, MaxKi, Body_Target, loc, and client.mob, src is the default.

True, but it's good practice to use 'src', even when it doesn't matter. It's helps you be certain to what you're calling, or assigning when manually debugging your program.
In response to Xooxer
Xooxer wrote:
Nope, the problem with switching mobs is that the procedure will terminate once the mob has logged out who called it.

I think you're thinking of deletion. When the client logs out, Logout() is called, and then client variable should just be null.
Try this:

        Body_Switch()
if(src.Ki>=src.MaxKi)
if(src.Body_Target)
var/mob/M = src.Body_Target
if(!src.client || !ismob(M) || !M.client) return
var/Rand = rand(1,10)
if(Rand>=10)
view() << "<center><font color=yellow><font size=-1>[src] has switched bodies with [M]"
src.Ki = 0
var/client/myClient = src.client
src.client.mob = new/mob(null) // Stick him in null for now!
M.client.mob = src
myClient.mob = M
else
src << "You need to click on a target!"
return
else
src << "<center><font color=red>You do not have enough Ki!"
return
In response to Yota
I was going by something I heard from Lummox in Chatters. I believe he criticized the use of src in those circumstances.

P_S
Did anyone not notice that he is getting a random number between 1 and 10, and if the random number is greater than or equal to 10, the verb carries on? It will only work 10% of the time, because getting a higher number than 10 out of a range of 1 to 10 is impossible. Of coarse, if you're looking for a 1 out of 10 chance chance, you should use:
if(Rand==10)

but your way is fine as well. It's still a 1 out of 10 chance, although it is unecessary.
In response to CaptFalcon33035
Actually, in this situation, prob() should be used instead of rand().
In response to Yota
Ok, when I tried this is logs me out of the game. >_<
In response to ITG Master
This is what i came up with

obj
BodySwq
verb
mob/verb/Body_Switch(mob/M as mob in view())
set name = "BodySwitch"
set category = "Fighting
if(!M.client) return //If they don't even have a client, dont' switch them
var/mob/temp = new/mob(loc)
var/mob/oldmob=src
var/mob/Moldmob=M.client.mob
client.mob=temp
M.client.mob=oldmob
client.mob=Moldmob
del temp


I get this error

test2.dm:6: unterminated text (expecting ")

For some reason can anyone help me out?
In response to Govegtos
I doubt you made that code now. :/
change set category = "Fighting to set category = "Fighting"
In response to Hell Ramen
Lol, thats very funny!