ID:179708
 
Here's the problem code with the problem pointed out.

get()
set src in oview(1)
var/tmp/obj/M
for(M in usr) break
if(M)
M.amount++
M.suffix = "([M.amount])"
del(src)
return
if(Move(usr))
usr << "You get [src]"
amount++
suffix = "([amount])"


drop()
if(amount > 1)
new var/obj/?(usr.loc) <--- here is the problem
amount--
suffix = "([amount])"
return
if(Move(usr.loc))
usr << "You dropped [src]"
amount = 0

(excuse the spacing, that isn't the problem, just bad pasting :p )

The whole point of this is to have only one occurance of the object in your inventory so you don't have to scroll through 50 healing potions or whatever. I don't know what to put where the question mark is because the object type depends on what I'm trying to drop.

Is there anyway to tell it to create a new object that is the same type as the src? I really don't want to use a bunch of if statements to compensate for all possible objects...
Not EXACTLY sure how but i bet the type() var would help ya out there..




-Rcet
In response to Rcet
obj/Pos_Marker
mob/var/myposmarker
mob/New()
myposmarker = new /obj/Pos_Marker(null) // << Here is your answer
mob/proc/MarkPos()
myposmarker:loc = src.loc
mob/verb/GotoPos()
if(myposmarker.loc != null)
src.loc = myposmarker:loc
else
src << "You have not marked your pos yet."


Chew on that...
In response to Lord of Water
Lord of Water wrote:
obj/Pos_Marker
> mob/var/myposmarker
> mob/New()
> myposmarker = new /obj/Pos_Marker(null) // << Here is your answer
> mob/proc/MarkPos()
> myposmarker:loc = src.loc
> mob/verb/GotoPos()
> if(myposmarker.loc != null)
> src.loc = myposmarker:loc
> else
> src << "You have not marked your pos yet."

Chew on that...

Why should he chew on it? It had nothing to do with his question.
In response to Skysaw
Skysaw wrote:
Lord of Water wrote:
myposmarker = new /obj/Pos_Marker(null) // << Here is your answer
Why should he chew on it? It had nothing to do with his question.

Because, I showed him where his problem was. The only reason I elaborated was for perosnal pleasure.

More to the point, you need to omit the var. Just use:

new /obj/whatever (location)

Good luck!
I'd have to be near the compiler to get anything to work, but here's a little something to help.

All atoms have a variable called .type that you can reference. You can compare two types using the istype() proc.

A long way to do it would be to use a switch statement to create a new instance:
switch(type)
(/obj/cheese)
var/obj/cheese = new(usr.loc)
(/obj/yoyo)
var/obj/yoyo = new(usr.loc)
etc...


This is not very elegant, especially with lots of object types. I don't think you can set a variable's type at run-time, otherwise, you could do something like this:
var
this_obj as src.type


That would certainly cut down on the coding.
In response to Lord of Water
Lord of Water wrote:
Skysaw wrote:
Lord of Water wrote:
myposmarker = new /obj/Pos_Marker(null) // << Here is your answer
Why should he chew on it? It had nothing to do with his question.

Because, I showed him where his problem was. The only reason I elaborated was for perosnal pleasure.

More to the point, you need to omit the var. Just use:

new /obj/whatever (location)

Good luck!

He already knew where his problem was. He's the one who marked it! He wanted to know what to put on that line to solve his problem, and you did not answer that question, because apparently you didn't understand his problem.

He wanted to know how to duplicate an object without knowing the object's type. Where exactly in your code do you do that?
In response to Skysaw
Yes, that code would only tell my object where to go (if I read it correctly) but not which object type to make.

Thanks for that snippeet skysaw, that will at least help me to make my code work.

Maybe there isn't a way to creat a new object the way I want to in byond for now.
It occurs to me now that you can specify a type for a new() proc. You might want to try this:
obj
var/amount = 1

drop()
var/child_obj
child_obj = new(src.type, null)
if(child_obj.Move(usr.loc))
amount --
child_obj.amount = 1
child_obj.suffix = "([child_obj.amount])"
usr << "You dropped [src]."
if(!amount)
del(src)
else
usr << "You can't drop that here."
del(child_obj)

get()
set src in oview(1)
var/tmp
obj/M
total = 0
for(M in usr)
total = M.amount
break
if(Move(usr))
usr << "You get [src]"
amount += total
suffix = "([amount])"
if(M)
del(M)


You might want to modify this to be able to drop any number of the object at a time, instead of just one.
In response to Skysaw
The only problem is that child_obj is only a variable and not an object. Because of this it won't let me access child_obj.amount or child_obj.suffix because those only exist for the objects.

I tried to fix it by using var/obj/child_obj

That fixed the compiler errors but I realized that just created a new object type so it would no longer recognize it as my original object.
In response to English
English wrote:
The only problem is that child_obj is only a variable and not an object. Because of this it won't let me access child_obj.amount or child_obj.suffix because those only exist for the objects.

I tried to fix it by using var/obj/child_obj

That fixed the compiler errors but I realized that just created a new object type so it would no longer recognize it as my original object.

As I mentioned, I wrote that code away from a compiler (and here I am away once again). What was the compiler error? The reference states that you can supply a type in new(). Perhaps the assignment should be on one line?

var/child_obj = new(src.type, null)

This topic could use a little help from someone who's done something like this before. Either that, or I might possibly have some free time this evening to try to work it out. No promises.

I do apologize spouting untested theories, but it sounded like an interesting problem, so I stuck my nose in.

/mob/skysaw
In response to Skysaw
Skysaw wrote:
var/child_obj = new(src.type, null)

The type should appear outside the parenthesis:
var/child_obj = new src.type(location)
In response to Shadowdarke
Shadowdarke wrote:
Skysaw wrote:
var/child_obj = new(src.type, null)

The type should appear outside the parenthesis:
var/child_obj = new src.type(location)

Aha! Right you are!

English, go back to my first code post and change the line to:

child_obj = new src.type(null)

That should take care of the compiler error, anyway.
In response to Skysaw
These are the two versions I tried and they came up with the same errors:
var/child_obj
child_obj = new src.type(null)

var/child_obj = new src.type(null)

These are the errors it gives me followed by what they point to.

test3.dm:17:error:child_obj.Move:bad proc
if(child_obj.Move(usr.loc))

test3.dm:20:error:child_obj.amount:bad var
child_obj.amount = 1

test3.dm:21:error:child_obj.suffix:bad var
child_obj.suffix = "([child_obj.amount])"

test3.dm:21:error:child_obj.amount:bad var
child_obj.suffix = "([child_obj.amount])"


Maybe I just need a different approach to the inventory problem. I've seen some games with a system similar to this but I can't remember which ones.
In response to English
English wrote:
These are the two versions I tried and they came up with the same errors:
var/child_obj
child_obj = new src.type(null)

var/child_obj = new src.type(null)

These are the errors it gives me followed by what they point to.

test3.dm:17:error:child_obj.Move:bad proc
if(child_obj.Move(usr.loc))

test3.dm:20:error:child_obj.amount:bad var
child_obj.amount = 1

test3.dm:21:error:child_obj.suffix:bad var
child_obj.suffix = "([child_obj.amount])"

test3.dm:21:error:child_obj.amount:bad var
child_obj.suffix = "([child_obj.amount])"


Maybe I just need a different approach to the inventory problem. I've seen some games with a system similar to this but I can't remember which ones.

I have two approaches that should solve this. The problem is that the compiler is not sure that child_obj will always have those variables. You can either change it to var/obj/child_obj, and make sure that you define each of those variables under obj,

OR

... this is actually a good candidate for the sometimes dreaded colin:

if(child_obj:Move(usr.loc))
child_obj:amount = 1
child_obj:suffix = "([child_obj:amount])"

This approach might even be the better one, though I wonder if some here might give me flack for suggesting using the colin. Actually, this is precisely the sort of situation the colin was intended for. It tells the compiler "trust me... I know this object has these properties."

Give it a try.
I've tested the below version, and it does compile. Whether or not it does exactly what you need, you'll have to let me know.
obj
var/amount = 1

verb
drop()
var/child_obj
child_obj = new src.type (null)
if(child_obj:Move(usr.loc))
amount --
child_obj:amount = 1
child_obj:suffix = "([child_obj:amount])"
usr << "You dropped [src]."
if(!amount)
del(src)
else
usr << "You can't drop that here."
del(child_obj)

get()
set src in oview(1)
var/tmp
obj/M
total = 0
for(M in usr)
total = M.amount
break
if(Move(usr))
usr << "You get [src]"
amount += total
suffix = "([amount])"
if(M)
del(M)
In response to Skysaw
I was tinkering with it and got it to work! I don't know why it wouldn't work for me before because I think I used the same syntax...

Anyway, since you helped me through this I thought I might as well share it with you if you want it. I'll look it over some more to see if I can refine it any but at least it works!

****************************************************

obj
var
amount = 1 //How many of each object

verb
get()
set src in oview(1)
var/obj/O
for(O in usr)
if(istype(O, src)) //checks to see if object on ground is already in inventory
O.amount += src.amount
O.suffix = "([O.amount])"
del(src)
return
if(Move(usr)) //if no match in inventory was found, move object into inventory
if(amount == 1)
usr << "You got [src]"
else
usr << "You got [src]s"
suffix = "([amount])"
else
usr << "You couldn't get [src]"

drop()
var
amountDrop = input("How many?", "Drop", 1)
if(amountDrop <= amount && amountDrop > 0)
var/obj/O = new src.type(null) //creates new object of calling type
if(O.Move(usr.loc))
if(amountDrop == 1)
usr << "You dropped [src]"
else
usr << "You dropped [src]s"
amount -= amountDrop
O.amount = amountDrop
suffix = "([amount])"
if(amount == 0)
del(src)
return
else
del(O)
usr << "You couldn't drop [src]"
return
else
if(amountDrop == 0)
usr << "You drop nothing"
else
usr << "You don't have that many!"

********************************************************

I guess it doesn't like my comments spacing, oh well.

I'm still open to suggestions, I'm sure there are ways I can trim it down a little. I'll keep working on it but I don't mind any hints ;)

I just changed it using for(O in usr) instead of that clumsy way of checking inventory I used ;)