ID:141468
 
Code:
mob
DblClick()
..()
if(src == usr)
return
if(src.owner == usr)
return
if(src == client)
return
if(istype(src,/mob))
usr << "You have targeted [src]"
usr << "The enemies health is [src.HP]"
for(var/mob/Koromon/K in world)
if(K.owner == usr)
K.Target = src
else
usr << "You cant target a Tamer!"

Attack_Panel
name = "Attack"
icon = 'Digipanel.dmi'
screen_loc = "4,1"
icon_state = "Attack"
Click()
for(var/mob/Koromon/K in world)
if(K.owner == usr)
walk(K,0)
repeat
for(var/mob/E in world)
if(K.Target == E)
if(E.HP > 0 )
if(K.Target != oview(1))
step_towards(K,K.Target)
sleep(1)
E.Attack()
E.Death(K)
else
walk(K,0)
goto end
goto repeat
end
walk_towards(K,usr)
Attack()
for(var/mob/Koromon/K in world)
if(K.owner == usr)
if(src == K.Target)
if(src in oview(1))
var/damage = rand(src.Str,src.Str*2)
src.HP -= damage


Problem description:
Im currently working on a Digimon game and I want to code it so that your a Tamer and you have a digimon. I've coded all the vars for the digimon so they save and everything Im just having trouble with the code.

The problem is the usr which is the tamer must be standing next to the enemy for any damage to take place.
if(src in oview(1))


That means if the src(Person your attacking) is in 1 tile of the usr(tamer). What you want I think is replace usr with K(The digimon) so it would look like the following. Which means that the src has to be within 1 tile of K.

if(src in oview(1,K))

Ungh, bad. You have a lot of redundant code, and you're using goto. Never use goto.
Go to BYOND Resources and search for Rifthaven's Pet Demo, it would help you greatly I believe.
In response to SadoSoldier
Ick, probably not. That's a good example of how not to write a demo.
In response to Popisfizzy
I know its not exactly the best but its still better than his code, am I not right?
All of that code is very very wrong, especially the loops. Delete it and try again, after reading about for(), while() and if().
In response to Bakasensei
Thanks, I think I added that in before but I had some other problem with it. It works now.
The use of goto in those loops is troubling since it's really not an appropriate place for goto. Actually it's not really an appropriate place for loops at all.

The for(var/mob/E in world) loop is quite troubling. If you'll notice, you're only allowing a single mob, K.Target, to be used as a valid E. For that reason, the loop is just a huge waste of time since you already know which mob you want to use. A better format would be:

if(K.Target)
...
// walk K toward K.Target
else
...
// walk K toward K.owner


And although it's obviated by removing the loop, a break statement would serve you better than "goto end".

The for(var/mob/Koromon/K in world) also is problematic: It's looping through all mobs in the world just to get at a select few who may be owned by you. Better solution: Keep a list of which mobs you own, and then you can loop through all of those to force them to attack. The list of mobs you control is going to be a much smaller list than all the mobs in the world.

Probably you should take all of this walking code out of the attack panel and such to begin with. That all really properly belongs in a master AI routine for the mob itself, not in your interface. That will also let you make the AI more complex, so different mobs can have different attack patterns and when coming back "home" they might dally to pick up a juicy fruit they see. The only thing your attack panel needs to do is set a few vars like telling them who their target is, and their AI routine can do the rest.

Also I'm not sure how you're saving, but this warning might be helpful to you: Be careful when saving the mobs. Because you gave them a src.owner var that points to the mob who owns them, whenever one of them saves it's saving a different version of your character. If they all save and load within the same savefile with you then there's no problem, but if they can save separately from you you could end up in trouble. A better option in that case is to make the owner var /tmp so they lose affiliation after being reloaded, or use a key so src.owner might be "SSJCoop" instead of your actual mob.

Lummox JR
In response to Lummox JR
Thanks for the advice I changed the code like you said but now Im having issues with the Mob following the usr around because I tried inputting the walk_towards proc in with the mob.

**Edit**

The follow code is fixed now, nevermind.