ID:148300
 
i had made a monster wander code and it doesnt wander nor will it attack heres the code

mob/monsters
var/mob/C/Male/P
New()

Wander()
proc/Wander()
set background = 1
do

if (P in oview(10))
step_towards(src,P)
else

for(P in view(src))
break

sleep(0)
while(src)
Bump(mob/M)
if (istype(M,/mob/C))
if(src.Speed - M.Speed > 0)
var/hitting = rand(1,12)
var/damge = src.Strength
if(hitting <= 2)
view(5)<<"[src] misses the attack"
else
if(damge - src.Defence > 0)
var/damage = damge-M.Defence
M.Health -= damage
view(5)<<"[M] Has been attacked for [damage] damage point by [src]"
M.DeathCheck()
else
M.Health -= 1
view(5)<<"[src] hits [M] for 1 damage point"
M.DeathCheck()

else
var/Hitting = rand(1,10)
if(Hitting <= 2)
view(5)<<"[src] misses the attack"
else
var/damge = src.Strength
if(damge - src.Defence > 0)

var/damage = damge-M.Defence
M.Health -= damage
view(5)<<"[M] Has been attacked for [damage] damage point by [src]"
M.DeathCheck()
else
M.Health -= 1
view(5)<<"[src] hits [M] for 1 damage point"
M.DeathCheck()











mob/Monsters/ToothSlug
icon = 'Monsters.dmi'
icon_state = "Tooth Slug"
Health = 15
Strength = 3
Defence = 3
Speed = 1
MaxHealth = 15
Exp = 10
Gold = 100
Egads! What's all this? Tidy it up man, tidy it up! =P I'll go through it section by section.

mob/monsters
var/mob/C/Male/P

I've seen people define vars like this more often recently. What's with it? It's perfectly allowable to do so, but there's no point to it in this situation. Such a var is better defined within the proc it's used in. (So get rid of that var, for a start.)

New()
Wander()

This ties up New(). If you call anything like this from New(), make sure you spawn() it, like this: <code>spawn() Wander()</code>

proc/Wander()
set background = 1

If a proc has to be a background proc in order to function, it's not set up properly. Delete that <code>set background</code> line.

do

Well, at least you're not using goto. However, calling the Wander() proc again from within the Wander() proc - this is called "recursive calls", or recursion - is a more efficient way of doing this loop. Just make sure you spawn() the call off, with a delay. (This is IMPORTANT! If you don't spawn() recursive calls, your game will slow down dramatically and you'll probably get runtime errors too.)

if (P in oview(10))

At last, we come to the heart of the problem. if() statements don't work like for() statements, with <code>in</code>. You need to use locate():

<code>if (locate(/mob/player) in oview(src,10))</code>

Also notice my use of <code>src</code> in oview(). oview() defaults to using <code>usr</code>, which is bad in a proc. (For more information on how usr can sometimes be evil, see usr Unfriendly.)

step_towards(src,P)

This would work, except that we've got rid of the P var. So it's probably best to use locate() again.

<code>step_towards(src, locate(/mob/player) in oview(src,10))</code>

else
for(P in view(src))
break

Eh? What's this for? You're looping through all mobs that are the same type as P, in view of src. If you find one, you quit the for() loop. This accomplishes absolutely nothing. Delete those three lines above.

sleep(0)

Arrrgh! If you were going to use sleep() here at all, you should put a delay in. Like sleep(5), half a second, or sleep(10), a whole second. But seeing as I'm going to show you a different (and most likely better) way of doing this whole Wander() looping thing, you don't need it. Delete the line.

while(src)

Now it's time for me to tell you how to replace that do-while loop. Delete the while() line, and replace it with a spawn(5) to Wander():

<code>spawn(5) Wander()</code>

Okay, let's look at that line. spawn() ensures that the following piece of code will execute after a given amount of time, but not lag the CPU or interfere with the rest of the game while waiting for that time to elapse. The 5 indicates 5 ticks, which is half a second. (10 ticks is one second, 20 is two seconds, etc.) To make the monster move faster, decrease this value; to make it move slower, increase this value. Finally, <code>Wander()</code> will execute the Wander() proc again.

Whew! Finally, the end. =) I hope that helped you.
In response to Crispy
At least he has some whitespace

*glares at Lummox*
In response to Jotdaniel
Some white space? He's got loads of white space. Too much, even. =P Plus some of it is in the "wrong" place - look at /mob/monsters/New(). Wouldn't it be more readable if the blank line was AFTER Wander()? Not that it matters at all. =)
In response to Crispy
Could You Put That All In 1 Chunk?? Cuz I Dunno How Many Tabs To Use Its Really Skrewing Me Up.Thanks.
In response to JohnReaper
Please Don't Talk With The First Letter Of Each Word In Uppercase. It's Really Annoying, And I Have No Idea Why People Get In The Habit Of Doing It. Thanks. =)

You came here to learn, right? So start learning! =P Learning how to indent things in DM properly is a crucial skill. Just struggle through, and hopefully you'll learn something in the process! If you get really stuck, post what you have so far and I, or someone else, will help you with it. Good luck! =)
In response to Crispy
Well i dont have much wont be able to indent everything and its really hard i tried already and it didnt work so i had somone give me a code and its :

mob/monsters
var/mob/C/Male/P
New()
spawn() Wander()

proc/Wander()
set background = 1
while(src)
if(!P || !(P in oview(10,src)))
P = locate() in oview(src)
if(P)
step_towards(src,P)
sleep(3)
else
sleep(rand(8,16))
hmm that doesnt work for me =(
In response to JohnReaper
JohnReaper wrote:
Well i dont have much wont be able to indent everything and its really hard i tried already and it didnt work so i had somone give me a code and its :

mob/monsters
var/mob/C/Male/P
New()
spawn() Wander()

proc/Wander()
set background = 1
while(src)
if(!P || !(P in oview(10,src)))
P = locate() in oview(src)
if(P)
step_towards(src,P)
sleep(3)
else
sleep(rand(8,16))
hmm that doesnt work for me =(

That was from me, a modification of the code you put up in Chatters. In hindsight I should have changed that step_towards() to step_to().

The monster doesn't wander because there's no step_rand() or anything for the "else" portion. There never was, though, so that problem was in your original code. You can make a more interesting wander by inserting these lines (after "else", before sleep()):
if(prob(40)) dir = turn(dir, rand(1, 7) * 45)
walk(src, dir, 3)
The 3 is the monster's speed, matching the sleep(3) above. I'd also change the sleep(rand(8,16)) to sleep(rand(3,6)*3) now, so it's an even multiple of steps.

As for why it won't attack, that I don't know. Is your player in fact a /mob/C/Male? If you aren't, then the monster won't see it when it uses locate() to look for a player.

Lummox JR
In response to Lummox JR
uhh yea thanks but 2 more problems, turn is an undefined proc ( if(prob(40)) dir = turn(dir, rand(1, 7) * 45) )
and invalid expression ( walk(src, dir, 3) ) im guessing because the first line is wrong. reply soon please.

heres the code incase you want to take a look at it



mob/monsters
var/mob/C/Male/P
New()
spawn() Wander()

proc/Wander()
set background = 1
while(src)
if(!P || !(P in oview(10,src)))
P = locate() in oview(src)
if(P)
step_to(src,P)
sleep(3)
else
if(prob(40)) dir = turn(dir, rand(1, 7) * 45)
walk(src, dir, 3)
sleep(rand(3,6)*3)
step_rand()
In response to JohnReaper
Don't indent anything under that if(). The dir=turn(...) command after it is the only thing that goes with it.

Lummox JR
In response to Lummox JR
could you just wright it out for me?
In response to JohnReaper
Look at the if() line that has the turn() stuff in it. Now look at the three lines below it. Those three lines are indented too far. You don't want them indented under if(), because if()'s instruction has been put on the same line as if(). Take one tab off each of those three lines.
In response to JohnReaper
JohnReaper wrote:
could you just wright it out for me?

No, nor will I write it out. All you have to do is unindent a section you shouldn't have indented, which was pretty clear from what I said.

Lummox JR