ID:149955
 
For some odd reason, when I use a var as an obj, I cant delete it. In game it says bad del. heres a sample code:

mob
verb
Myverb()
var/myvar = /obj/myobj
new myvar(usr.loc)
del(myvar)


Can someone help me out?

Thanks

-Rcet
maybe you could create an obj variable instead:

var/obj/myvar = /obj/myobj

See if that helps any...

~X
Rcet wrote:
For some odd reason, when I use a var as an obj, I cant delete it. In game it says bad del. heres a sample code:
mob
verb
Myverb()
var/myvar = /obj/myobj
new myvar(usr.loc)
del(myvar)

myvar is being assigned a type, not an actual object. The actual object is created in the "new myvar(usr.loc)" line. Thus in the next line, you're not deleting the object you created (which is never assigned to a var or anything), but a type.

Lummox JR
In response to Lummox JR
Thanks, but how do i delete it then?

What im actually trying to do is keep an obj that contantly stays 3 squares below the user.

COuld you help me on that instead?


thanks

-Rcet
In response to Rcet
I use variable references to objects often. Try this:

proc/delsomething(var/range as num)
var/list/objs = list()
for(var/obj/O in oview(range)) objs.Add(O)
var/obj/target = input("Select object") in objs
var/targetname = target.name
del target
return targetname
mob/verb/Edadicate()
var/range = rand(4-rand(0,2),7-rand(-2,3))
alert("You can delete anything in a radius of [range].")
var/something = deletesomething(range)
if(lowertext(something) == "bunny rabbit")
world << "[src] is a MEANIE! \he killed a bunny!!"
return
world << "[src] eradicates \an [something]."


Have fun! Don't kill anything called a bunny rabbit!!
In response to Rcet
Rcet wrote:
Thanks, but how do i delete it then?

You've been trying to delete the wrong thing. You tried to delete the type, not the actual object. To delete the object, assign it to a variable when you create it with new, and then delete the value of the variable. Since you didn't assign the value of new to anything, the object is created but you don't have any kind of reference to it within the proc.

Observe the difference between your code and a corrected example:
var/myvar = /obj/myobj   // original code
new myvar(usr.loc)
del(myvar)

var/mytype = /obj/myobj   // original code, var name changed
new mytype(usr.loc)
del(mytype)

var/mytype = /obj/myobj   // new code
var/myobj = new mytype(usr.loc)
del(myobj)

What you were doing before was, in effect, deleting mytype instead of myobj. Your code didn't have a myobj, nor assign any value to it, so when you went to delete the object, you mistook the only var you had for the one you never put in. Or hopefully to put it more plainly, you created an object but never said what to do with it; then you said "delete something" but didn't have a reference to "something" handy to delete, so you put the wrong thing in del().

Lummox JR
In response to Lummox JR
Well, i got that to work, but how would i MOVE it? i cant use myobj.Move(locate(x,y,z)) becuase its a var. Anyway that i could move it?

Sorry for all the questions :)

-Rcet
In response to Rcet
Rcet wrote:
Well, i got that to work, but how would i MOVE it? i cant use myobj.Move(locate(x,y,z)) becuase its a var. Anyway that i could move it?

Assuming mytype is always an /obj, you can declare myobj as var/obj/myobj, which ought to do. If it can be an obj or a mob, then declare it as var/atom/movable/myobj.

Lummox JR
In response to Lummox JR
If i declare myobj as var/obj/myobj, then new myobj() wouldnt work right, would it? I tried doing that all different ways in my code, but nothin' worked.

-Rcet
In response to Rcet
Rcet wrote:
If i declare myobj as var/obj/myobj, then new myobj() wouldnt work right, would it? I tried doing that all different ways in my code, but nothin' worked.

You're again confusing the type and the object. You'd be using new mytype(), not new myobj().

Lummox JR
In response to Lummox JR
So i would have to do this?:

var/mytype = /obj/shadow
var/myobj = var/obj/shad
new mytype(locate(1,1,1))
sleep(10)
mytype.Move(locate(2,2,1))


Again, thanks for the help. :]

-Rcet
In response to Rcet
Rcet wrote:
So i would have to do this?:
var/mytype = /obj/shadow
var/myobj = var/obj/shad
new mytype(locate(1,1,1))
sleep(10)
mytype.Move(locate(2,2,1))

Egads no. That makes no sense whatsoever.
Look again at the lines you wrote:
var/myobj = var/obj/shad

This isn't even a legal construct. It doesn't say anything that would make any sense in DM.
new mytype(locate(1,1,1))

So you've created an object, but now what? You haven't assigned it to any variable. DM has just created the object and forgotten about it. The only way to get at it would be to dig through objects in locate(1,1,1) and find an object whose type==mytype. Again, go back through my earlier posts because you appear to have missed the central point: When you create the object and you want to do something with it later, use a line like myobj = new mytype(...)--which was the whole point of the myobj var in the first place. I can't figure out where in the world that 2nd line came from.
mytype.Move(locate(2,2,1))

Because the value of mytype is, again, a type, NOT--I repeat, NOT--an obj, it's not a thing that you can move. It's a reference to the type of a thing; you have to create a thing of that type, save a reference to the thing you created (myobj = new mytype(...)), then use the obj reference--NOT THE TYPE REFERENCE--for the moving.

Your code would be correct if it were this:
var/mytype = /obj/shadow
var/myobj = new mytype(locate(1,1,1))
sleep(10)
myobj.Move(locate(2,2,1))

Lummox JR
In response to Lummox JR
Ok, i know im being stubborn, but once again i've run into a problem. I did the code EXACTLY like this:

var/mytype = /obj/shadow
var/shady = usr.y - 3
var/shad = new mytype(locate(1,1,1))
shad.Move(locate(usr.x,shady,usr.z))


but now it gives me this error upon compiling:
error:shad.Move:undefined proc


I woulnd't blame you if you stop trying to help, because im pretty much hopeless lol :\

-Rcet
In response to Rcet
Rcet wrote:
Ok, i know im being stubborn, but once again i've run into a problem. I did the code EXACTLY like this:
var/mytype = /obj/shadow
var/shady = usr.y - 3
var/shad = new mytype(locate(1,1,1))
shad.Move(locate(usr.x,shady,usr.z))

but now it gives me this error upon compiling:
error:shad.Move:undefined proc

As I mentioned earlier, if you assume mytype is always going to be an /obj type, then you can declare your actual obj's variable like so:
var/obj/shad = new mytype(locate(1,1,1))

This is only a problem if mytype is not derived from /obj. Your var, shad, knows it's at least an /obj and therefore can use the Move() proc.

Lummox JR
In response to Lummox JR
YAY! I finally understand now, and got it working. Thanks ALOT Lummox.

-Rcet
In response to Rcet
I just wanted to add... you're not being stubborn. I think you're just hung up in a weird spot trying to get this, and it happens at the moment that you're confused enough that it can be confusing or frustrating for others who are having difficulty trying to explain it. That happens sometimes, though.

Some programming concepts aren't very straightforward and don't really sink in until something "clicks" or snaps into place. Often when you're in transition between one mode of thinking and another like that, the mind throws a gear. So this thread is kind of like grinding the gears for a bit; that's all. :)

Lummox JR