ID:139813
 
Code:
mob/amonster/proc/AI()
var/mob/M
while(src)
if(M.client)
if(M in oview(5))
walk_to(src,M,1,4) //src walks to M until it is within 1 block away, moving 4/10th of a second
if(M in oview(1))
step_towards(src,M)
else
break
else
for(M in view(src))
break
//sleep 2 ticks between moves
sleep(2)
spawn(20)
AI()


Problem description:


runtime error: Cannot read null.client
proc name: AI (/mob/amonster/proc/AI)
usr: CircleDemon (/mob/amonster/CircleDemon)
src: CircleDemon (/mob/amonster/CircleDemon)
call stack:
CircleDemon (/mob/amonster/CircleDemon): AI()
CircleDemon (/mob/amonster/CircleDemon): New(the turf (4,11,1) (/turf))

for some reason when ever i start the game that error pops up and no npc will attack
Evilmaniac wrote:
var/mob/M
if(M.client)


The obvious design flaws in your program aside the error is simple. By the time you're checking for M.client, M is null as it hasn't been assigned any value, thus the problem.
In response to Schnitzelnagler
no matter where i put if(M.client) even if the error goes away it still doesnt work
In response to Evilmaniac
Evilmaniac wrote:
no matter where i put if(M.client) even if the error goes away it still doesnt work

The fact that you have to randomly guess instead of knowing what a certain instruction would do in a specific position hints at the fact that you never got started with learning the basics. This is naturally bad, as guessing is a troublesome and error prone task, which you shouldn't ever have to do while programming.

Please do not take this as offence, since it's not intended to be one, but as advise on how to achieve better and faster results. You don't try to write a German poem without learning German first either, do you? Or, well, you'd have rather bad success, if you would ;)
In response to Schnitzelnagler
The fact that you have to randomly guess instead of knowing what a certain instruction would do in a specific position hints at the fact that you never got started with learning the basics. This is naturally bad, as guessing is a troublesome and error prone task, which you shouldn't ever have to do while programming.

i have read the guide already -_-, and when i said that i didnt mean that i ment no matter what i do it won't work

Please do not take this as offence, since it's not intended to be one, but as advise on how to achieve better and faster results. You don't try to write a German poem without learning German first either, do you? Or, well, you'd have rather bad success, if you would ;)

:D fact is i know how to speak german..... though i still dont think i would be able to write a poem in it......
In response to Schnitzelnagler
Schnitzelnagler wrote:
Evilmaniac wrote:
no matter where i put if(M.client) even if the error goes away it still doesnt work

The fact that you have to randomly guess instead of knowing what a certain instruction would do in a specific position hints at the fact that you never got started with learning the basics. This is naturally bad, as guessing is a troublesome and error prone task, which you shouldn't ever have to do while programming.

Please do not take this as offence, since it's not intended to be one, but as advise on how to achieve better and faster results. You don't try to write a German poem without learning German first either, do you? Or, well, you'd have rather bad success, if you would ;)

Human beings should be capable of giving better advice than "read the guide" or "learn the basics". If you can't, you should learn the basics about helping people.

The problem with the code is that you can't check M.client if M is null. You need to first check that M has a value, then check if M.client exists, like this:

if(M && M.client)
// do something


This will get run of the runtime error that you get, but because you never assign a value to M the code still won't do anything.

For AI basics you should look at this demo: http://www.byond.com/developer/Forum_account/EnemyAI
In response to Forum_account
I beg your pardon Sire?
I explained the error (exactly the same was you repeated) in my previous post[link], but instead of pointing the person in question to a demonstration that won't be any use without knowing the base syntax, I advised to get a grip on said syntax. If you claim that feeding people copy and paste-able code with no possibility to understand it is better, this is fine to be your opinion, but please allow me a different opinion on education.
I recall not trying to learn English by reading Shakespear right the first day, but by working through vocabulary and grammar in school. That is, English being a second language to me.

Human beings should be capable of providing resources to bold claims. If you can't, you should learn the basics of arguing.

I'm sorry for derailing this thread and am going to be silent from now on.
In response to Forum_account
funny thing is i was reading your demo when you posted this. This AI code i use is from years ago when i first started coding, and every time i try to make it work i get so frustrated i give up and quit byond for a year, then i try again and get so frustrated i quit for another year.... this has happened about 3 times so far.
In response to Schnitzelnagler
Schnitzelnagler wrote:
I recall not trying to learn English by reading Shakespear right the first day

You're assuming that Evilmaniac has no knowledge of programming, much like someone in their first day of learning English. Aside from being rude this is probably incorrect too.

You're also comparing a simple line of code to the works of Shakespeare. In terms of programming, if(M && M.client) ain't Shakespeare!

I advised to get a grip on said syntax. If you claim that feeding people copy and paste-able code with no possibility to understand it is better

We're not talking about some huge problem with intricate details where providing code will not sufficiently explain the solution. We're dealing with a common mistake where a line of code and a one sentence explanation (that says more than the runtime error) is sufficient.
In response to Forum_account
We're not talking about some huge problem with intricate details where providing code will not sufficiently explain the solution. We're dealing with a common mistake where a line of code and a one sentence explanation (that says more than the runtime error) is sufficient.

Schnitzelnagler told him what was wrong with his code in language that would be easily understandable to somebody who knew what they were doing. Schnitzelnagler told him M was null, which was causing the runtime error. The only thing he didn't do was tell him to do was check if M was valid before accessing it. Evilmaniac's response to this was to move the line in question to a different line (or alternatively, he had already moved it to different lines at random before). This demonstrates a clear lack of understanding of the statement's purpose.

As for it being a common mistake, no. Yes, it is not uncommon for people to make a system where a reference might become null and not take it into consideration. No, it is uncommon for people to create a variable and then immediately attempt to access the object it is referencing when you have never set it to reference something.

Telling him to add a check for M to the if statement might fix this runtime error, but providing him with material so he can learn how to do things properly and fix his own problems is probably better for everyone.

Anyway, if you look at his code, you'll see he's pretty much creating an endless loop that will do nothing but sleep for .2 seconds forever.

His problems are much bigger than accessing a null variable.
In response to Keeth
i never sayed i put it in different places b4 if you read the other posts you would see i stated what i ment incorrectly
i finnaly found what i was doing wrong, i went back to the code that i had b4 i remade the one i posted, and found an error and got it working again