ID:263392
 
Code:
mob
proc
pickup()
var obj/O
for(O in view(0))
if(O.name == "pistol")
if(usr.machgunuse == 1)
usr.machgunuse = 0
usr.pistoluse = 1
usr.icon_state = "pistoluse"
usr.ROF = 10
new /obj/weapons/machgun(usr.loc)
del(O)
if(O.name == "machgun")
if(usr.pistoluse == 1)
usr.machgunuse = 1
usr.pistoluse = 0
usr.icon_state = "machgunuse"
usr.ROF = 1
new /obj/weapons/pistol(usr.loc)
del(O)


Problem description:
when you pick up the pistol, it gives this runtime error:

runtime error: Cannot read null.name
proc name: pickup (/mob/proc/pickup)
usr: Carved in Shadows (/mob/soldier/blue)
src: Carved in Shadows (/mob/soldier/blue)
call stack:
Carved in Shadows (/mob/soldier/blue): pickup()
Carved in Shadows (/client): Input("m")
Carved in Shadows (/client): m()


how can I fix it?

No put usr in proc. Ugh.

Yes, I stole that line.
In response to Revojake
revojake said:
No put usr in proc. Ugh.

mob
proc
pickup()
var obj/O
for(O in view(0))
if(O.name == "pistol")
if(src.machgunuse == 1)
src.machgunuse = 0
src.pistoluse = 1
src.icon_state = "pistoluse"
src.ROF = 10
new /obj/weapons/machgun(src.loc)
del(O)
if(O.name == "machgun")
if(src.pistoluse == 1)
src.machgunuse = 1
src.pistoluse = 0
src.icon_state = "machgunuse"
src.ROF = 1
new /obj/weapons/pistol(src.loc)
del(O)


I still get:
<font color = "red">
runtime error: Cannot read null.name
proc name: pickup (/mob/proc/pickup)
usr: Carved in Shadows (/mob/soldier/blue)
src: Carved in Shadows (/mob/soldier/blue)
call stack:
Carved in Shadows (/mob/soldier/blue): pickup()
Carved in Shadows (/client): Input("m")
Carved in Shadows (/client): m()</font>

that did absolutly nothing...
P.S. the proc is activated with a macro (m) which I dont think is the problem because firing and switching to the machgun (which is in the same proc) works fine.

just letting you know
In response to Carved in Shadows
Give us the code for your pistol.
In response to Revojake
It's "Ungh." "UNGH"
In response to CaptFalcon33035
add this to your code #define DEBUG
And then give us the error also showing which line it is on.<.<
~Grand~
In response to Revojake
obj
var
speed = 0
range = 10
power = 10
projectiles
pbullet
icon = 'Projectiles.dmi'
icon_state = "bullet1"
range = 8
power = 10
mbullet
icon = 'Projectiles.dmi'
icon_state = "bullet1"
range = 7
power = 3
weapons
icon = 'Objects.dmi'
pistol
icon_state = "pistol"
machgun
icon_state = "machgun"


...I used the bullets to deal the damage and such because it's easier, the acutal pistol is for show
In response to KillerGrand
it's a runtime error
In response to Carved in Shadows
obj
var
speed = 0
range = 10
power = 10
projectiles
pbullet
icon = 'Projectiles.dmi'
icon_state = "bullet1"
range = 8
power = 10
mbullet
icon = 'Projectiles.dmi'
icon_state = "bullet1"
range = 7
power = 3
weapons
icon = 'Objects.dmi'
pistol
O.name = pistol
icon_state = "pistol"
machgun
icon_state = "machgun"


Try something like that...Im not to sure.
In response to Revojake
Revojake wrote:
obj
weapons
icon = 'Objects.dmi'
pistol
O.name = pistol
icon_state = "pistol"

Try something like that...Im not to sure.

Indeed. That O.name line clearly belongs in a proc; it will fail at compile time.

Lummox JR
Aside from the usr abuse, which I take it you've already fixed (except that view(0) needs to be view(0,src)), you have several problems here. I'll get to the more serious one first.

The most serious problem is that your equipment system needs to be replaced with something better. The vars machgunuse and pistoluse are bogus. Instead, you should have a var that actually points to the item in question. I.e., the var should be mob.weapon and it should not hold a number like 0 or 1. It should be null if no weapon is in use, or should point to the actual object being used.

If you ever do need to use vars that are only 1 or 0 (true or false), which isn't the case right here, you should always use if(var) and if(!var) to test them, never if(var==1) and if(var==0). The other way is not only shorter but more robust. Presumably you don't care if the var is exactly 1 or even if it's a number at all, as long as it reads true or false correctly.

When picking up the object, you should not automatically start using it unless (DOOM-style rules here) you never had a weapon like that in your arsenal before. Otherwise the game should assume the current choice is correct.

Also, it does not make sense to delete the object and create a new one. What's the point? The only reason to do that is if you're using world.Repop(), and there are more effective methods you can use instead. Indeed this only gets in the way of you fixing the equipment system.

One other problem is that you're moving the weapon into the mob's loc, where it already was, instead of into the mob itself.

Finally, the immediate source of your runtime error is that you're not using a simple else statement in your second if() block. If the first if() is successful, your code is deleting the item (the above suggestions will fix this too) and therefore when your code gets to the second if(), there is no O left to test. Hence O is null, which is why you're getting the error that you can't read null.name.

The two if() blocks should really be combined anyway. Once you're using a single weapon var as you should be doing, there's no reason to do all this checking of names and working with different types. The ROF var should be set only when you actualy put the item to use anyway, and that can be done by giving that info to the weapon itself. The ROF var really shouldn't belong to the mob at all. Here's how this should look:
for(O in oview(0, src))
O.loc = src
if(istype(O, /obj/weapon))
var/obj/weapon/W = O // type casting so we don't abuse the : operator
// if this weapon has never been used before, equip it, but otherwise not
var/count = 0
for(var/weapon/W2 in src) if(W2.type==W.type && W!=W2) ++count
if(!count) W.Equip(src)


And here's roughly what Equip() would look like under a correct system:
obj/proc/Equip(mob/M)
if(!ismob(M) || M!=loc) return // not really equippable yet
if(M.weapon == src) return
if(M.weapon) M.weapon.Unequip(M)
M.weapon = src

obj/proc/Unequip(mob/M)
if(!ismob(M) || M.weapon != src) return
M.weapon = null

Notice I didn't set the ROF var. The weapon itself should be responsible for that; it should not be a mob var at all.

Lummox JR