ID:2656842
 
(See the best response by FKI.)
Code:
mob/verb/Buy_Egg()
set name = "Buy Monster Egg"

if(!src.monster)
if(!src.new_player)
spawn() alert("Thank you for playing Monsuta! As a new player your first egg is free!","Buy Monster Egg")
src.new_player = 1
src.monster = 1
var/mob/Egg/M = new(locate(3,2,1))
src.client.mob = M

//------------------------------------------------------------

mob/Egg
name = ""
icon = 'Egg.dmi'
icon_state = "egg"

New()
sleep(50)
spawn() src << 'Hatch.wav'
src.icon_state = "hatch"
sleep(40)
var/mob/Monster/Tamajakushi/M = new(src.loc)
src.client.mob = M
del(src)


Problem description:
This should look fairly straight forward, but the player uses a verb they have to "buy a monster egg", the monster egg is sent to their proper location and then the player is supposed to be transfered over to the egg.

Once a new egg is made, it's supposed to have a timer, in which it "hatches", creates the new mob, changes the player over to that and then deletes the old egg mob.

The sound I have for the hatching monsters does not play and when the new monster mob is generated and sent to the proper location, I get this error:

runtime error: Cannot modify null.mob.
proc name: New (/mob/Egg/New)
usr: (src)
src: (/mob/Egg)
src.loc: the turf (3,2,1) (/turf)
call stack:
(/mob/Egg): New(the turf (3,2,1) (/turf))
HartWing (/mob): Buy Monster Egg()

I am assuming the music isn't playing because the sound isn't being sent to the proper mob, which leads me to believe I am not changing the mobs correctly even though it compiles fine.

I had originally done this as a independant mob from the player, but decided since the player is only allowed one monster at a time, it'd be easier to just MAKE the player the mob instead.

I am hoping someone here will be able to help me out, I feel like I am probably missing something simple. I've done a quick google search, but the responses I have seen and tried haven't helped me.
A couple things:
  • You are trying to play a sound on src, which, at the point of running that line of code, is a mob with no client. So you hear no sound.
  • If I'm understanding what you want correctly, you want the client to experience two mob swaps, right? As of now, clients will only experience one swap. You create the egg, which has sleep() delaying the code until it creates the monster. The client is then switched to the monster, the egg is deleted, then the code attempts to give the client control over the egg, which has already been deleted.
I appreciate the input and I think I understand what you are saying here.

I am just trying to understand why it's doing what it is. If the game was running in order of lines programmed, why isn't the character being changed over to the egg when I tell it to? Because it should be changing over to that mob after it's created, right?

Why is the code attempting to give control to the egg when the egg should have been in control first? Why is it trying to give control to the egg, when the program is being told to give it to the monster?

I played the sound as SRC because the player SHOULD have been the egg, so wouldn't SRC be the player/egg?
In response to HartWing
Best response
I think you missed this part:

You create the egg, which has sleep() delaying the code until it creates the monster.

In other words:
            var/mob/Egg/M = new(locate(3,2,1))
src.client.mob = M // This line does not run immediately because you delay the code for 9 seconds inside Egg.New() -- and by the time it does, the Egg has already been deleted.
Okay, see, I didn't understand that. I figured two different things could be running at the same time. I didn't realize how I had done that was interrupting other things.

So I ended up fixing it, I simply took the stuff under Egg, turned it into a proc, and then did spawn(500) Hatch().

Fixed everything up, thank you!