ID:155651
 
Alright, today I was increasing my knowledge of the DM language, my focus today was to get a working AI system going. I knew how to do nearly all of the movement procs. Long story short, it didn't work till I used new() on the mob. Why did I have to use new()?
Unlike C or C++, you can't define objects on the stack. Doing "var/mob/m" does NOT create a mob, it just defines a variable that is type-casted to potentially hold a mob. Until it's assigned a value, M will be null. Objects are created by using the new() process.
In response to DarkCampainger
Ah, I understand. I thought that since the mob was already on the map, it would would on its own. So new actually brings the mob to life. Thank you for your assistance!
In response to Gtgoku55
Gtgoku55 wrote:
Ah, I understand. I thought that since the mob was already on the map, it would would on its own. So new actually brings the mob to life. Thank you for your assistance!

Well, yes and no. The mob on the map already exists, so using new would create a separate mob. If you have a mob that already exists, you need to get a reference to it. It would be helpful if you showed us some code.
In response to DarkCampainger
mob/enemy
icon='Enemy.dmi'
src.AI()

mob
proc
Guard()
while(src)
step(src,NORTH)
sleep(10)
step(src,NORTH)
sleep(10)
step(src,SOUTH)
sleep(10)





//That's how it was before I used New().

mob/enemy
icon='Enemy.dmi'
New()
src.AI() // This is afterwards




In response to Gtgoku55
Ah, you meant New(). DM is a case-sensitive language, so new() and New() are two separate processes.

The first version shouldn't even compile, because it's incorrect syntax. You can only call processes and verbs from other processes and verbs. The New() process for an atom is called when the atom is created. So to call your Guard/AI() process when the enemy is created, you have to redefine the mob/enemy's New() process to call your process.

Also, a quick tip: you should spawn() off any process that will sleep() unless you want the calling process to wait for it to finish (which if it's an infinite loop like this, it may never do so).

mob/enemy
icon='Enemy.dmi'
New()
..()
spawn()
src.AI()


The ..() bit is to call the parent's (/mob's) version of New(), in case you need to define that one later and also want it to affect the /mob/enemy.
In response to DarkCampainger
Just came to say thx for the tips before I go off to bed.