ID:1292571
 
(See the best response by FIREking.)
Code:
mob/Animals

Sheep
icon = 'Magicsheep.dmi'
Level = 1
Exp = 1
MaxHp = 10
Str = 1
Def = 1


proc
addAmount(n as num)
if(!src.stackable)return
src.amount+=n
src.suffix="[src.amount]"
removeAmount(n as num)
if(!src.stackable)return
src.amount-=n
if(src.amount<=0)
del(src)
else
src.suffix="[src.amount]"

var
stackable=TRUE
amount=1
suffix="1"
verb
Harvest()
set src in oview (1)
var/obj/wool/object = locate(text2path("[src.type]")) in usr.contents
if(object)
object.addAmount(src.amount)
else
new/obj/wool(usr)


Problem description:
I'm trying to make a harvest verb that collects wool from sheep and it would stack, so it only takes up one inventory space, but when I tested it out, the wool took up multiple spaces with a suffix of 1, I had to drop the wool and pick it up again so it would stack up. Please help, thanks.


Best response
Just wanted to say that locate(src.type) is a valid call btw
This works:


(Sorry for image, don't know what tag to put for code)

Hope I helped.
@FIREking
So...what can I do with that?

@Konlet
I have a get verb that may or may not be affected by your code. I might try it though.

@Akando
Evaluate please.
You're checking for /mob/animal/Sheep in the player, and then adding /obj/wool. Players presumably have wool in their inventory, not sheep, so it's always taking the 'else' path.

Why do you have your harvest/addamount/removeamount verbs/procs and related stacking variables defined on /mob/Animal/Sheep, instead of something more general? Surely the harvest verb should be on /mob/Animal, and the addamount/removeamount procedures and stacking variables on /obj (or some /obj derivative)

also, as someone mentioned earlier, locate(text2path("[type]")) is redundant - you can just say locate(type). Using locate for this is better than iterating through the player's contents, though.

You probably want something more like this:
obj/stackable
var
amount = 1
stackable = TRUE

New()
.=..()
suffix = "[amount]"

proc
addAmount(n)
amount += n
suffix = "[amount]"

removeAmount(n)
amount -= n
suffix = "[amount]"
if (amount <0) del src

wool

mob/Animal
var
harvest_type
harvest_amount

verb
Harvest()
set src in oview (1)
var/obj/stackable/object = locate(harvest_type) in usr.contents
if(object && istype(object, /obj/stackable))
object.addAmount(harvest_amount)
else
for(var/i = 1 to harvest_amount) new harvest_type(usr)

sheep
harvest_type = /obj/stackable/wool
harvest_amount = 1


Akando: Please don't try to help unless you know what you're doing.
I'm pretty sure TehBlackHole was aware which path the code was going down. He just wasn't sure why.

I was referring more to comments like "So you could new new/obj/wool as an. Addamount(new/obj/wool)" which don't match, even remotely, the addAmount procedure TehBlackHole posted. It takes a number.

I did not, in fact, use 'the istype you mentioned'. Your specific change wouldn't have worked - object is /null/, not an instance of /obj/wool. I'm checking for /stackability/ because TehBlackHole's original code didn't do that properly.
So why the istype() you had? I'm still not entirely certain what your mental model of what is going on is. That locate() call was always going to find an object of the type specified or null, so it was either stackable (/obj/Animals/Sheep is stackable) or null, and the code was already checking for null. Checking whether an object that can never be /obj/wool is of type /obj/wool seems a strange suggestion.

The problem that the harvesting/stacking code was defined in a specific way - as behaviour of sheep and wool, not of animals and objects - is a problem that needs to be solved before you even need to worry about properly checking for stackability. Why didn't you point that out?
In response to TehBlackhole
If you'd like quick help, you can add me on skype (konlet) or message me on the pager and I'd be glad to help ^^
src.type is always initialised. In the original example, it's /mob/Animals/Sheep. Again, I'm not sure how you're mentally modelling this code.
In response to Konlet
The tag is called DM. Just put the HTML brackets around the dm!
Jp, I tried your code out, and it works, thank you so much, Akando, thanks for trying to help, but next time, try to post something that others can understand, and thanks for the offer Konlet, I would have paged you as a last resort. So apperantly it kept going to else because it saw that I didnt have a sheep in my inventory and it thought it ment sheep? I know now I should have set it to wool but I still don't understand why if(object) doesnt work if I set the object to wool.
Let's break down your original code:
mob/Animals
Sheep


You have a /mob/Animals/Sheep

        proc
addAmount(n as num)
if(!src.stackable)return
src.amount+=n
src.suffix="[src.amount]"
removeAmount(n as num)
if(!src.stackable)return
src.amount-=n
if(src.amount<=0)
del(src)
else
src.suffix="[src.amount]"


/mob/Animals/Sheep has an addAmount and removeAmount proc on it, which adds and removes amounts of sheep to a single /mob/Animals/Sheep instance if it's stackable.

        var
stackable=TRUE
amount=1
suffix="1"


Sheep are also stackable.

        verb
Harvest()
set src in oview (1)


Sheep can be Harvest()'d if you're next to them

                var/obj/wool/object = locate(text2path("[src.type]")) in usr.contents


This means "Find an object of type 'src.type' in usr's contents and store it in variable 'object'". src.type is /mob/Animals/Sheep - this is trying to find a /mob/Animals/Sheep in the player's contents. (And 'locate(type) in usr.contents' would do the same thing)

The variable being defined as /obj/wool doesn't change that. Typepaths on variables are a suggestion made by you, the programmer, to the compiler about what the variable contains. They don't constrain what a variable contains, and they don't change the output of functions that are asked to give you objects of a different type.

                if(object)


If we found a /mob/Animals/Sheep in the player...

                    object.addAmount(src.amount)


Add to the amount of Sheep.

                else
new/obj/wool(usr)


Otherwise, make a /obj/wool in usr's inventory.

Essentially the problem was that you're stacking sheep, but adding wool in the failure case.

And as I noted earlier, it's strange that you're defining addAmount, removeAmount and Harvest on /mob/Animals/Sheep instead of some kind of /obj subclass (for addAmount/removeAmount) and /mob/Animals (for harvest).
Oh, I see, well one more question, how would I make an item not stackable when I pick it up from the ground using the get verb? I can't seem to make an item non stackable.

Here is the code


obj
proc
addAmounta(n as num)
if(!src.stackablea)return
src.amounta+=n
src.suffix="[src.amounta]"
removeAmounta(n as num)
if(!src.stackablea)return
src.amounta-=n
if(src.amounta<=0)
del(src)
else
src.suffix="[src.amounta]"

var
stackablea=TRUE
amounta=1
suffix="1"

verb
Get()
set src in usr.loc
if(stackablea)
var/obj/object = locate(text2path("[src.type]")) in usr.contents
if(object) //could find previous type of object
object.addAmounta(src.amounta)
del(src)
else //could not find previous type of object
usr.contents.Add(src)
else
usr.contents.Add(src)
Drop_All() //will drop every single of the stacked object
set src in usr
src.Move(usr.loc)
Drop_Amount(n as num)
if(n>src.amounta)n=src.amounta //if it's above the items amount
if(n<0)n=1 //if it's below 0
if(src.amounta==n)
src.Move(usr.loc)
else
src.removeAmounta(n)
var/obj/O = new src.type
O.addAmounta(n-1) //n-1 because objects default amounts are 1
O.Move(usr.loc)


As you can see, I had to change a few variable and proc names because of the code I put in. I try changing the parent to obj/stack or something different but it gives me an error that doesn't make sense

Vars.dm:19:error: src.stackablea: undefined var
Verbs.dm:61:error: object.addAmounta: undefined proc
Verbs.dm:78:error: O.addAmounta: undefined proc

Also, I got this code from a resource.
So... thanks... I guess...
What lines are those? I don't know which line line 19 is, or 61 or 78.
DM never really came with a line counter or whatever thats called, why, whats the problem?
Ctrl + L = show line numbers
I don't know, I suppose the problem would be that the compiler is telling you what line the error is on...and you are giving us a snippet (i.e. not all of the code), which means the line numbers aren't going to be the same for us...so if you want help we need to see where the error is being reported. Yeah, I think that's it.
Gee alright Albro,
I only put the number lines on the ones where it says it has an error.

obj/stack
proc
addAmounta(n as num)
if(!src.stackablea)return
src.amounta+=n
src.suffix="[src.amounta]"
removeAmounta(n as num)
if(!src.stackablea)return
src.amounta-=n
if(src.amounta<=0)
del(src)
else
src.suffix="[src.amounta]"

var
stackablea=TRUE
amounta=1
suffix="1"

verb
Get()
set src in usr.loc
if(stackablea)
var/obj/object = locate(text2path("[src.type]")) in usr.contents
if(object)
/*61*/ object.addAmounta(src.amounta)
del(src)
else
usr.contents.Add(src)
else
usr.contents.Add(src)
Drop_All()
set src in usr
src.Move(usr.loc)
Drop_Amount(n as num)
if(n>src.amounta)n=src.amounta
if(n<0)n=1
if(src.amounta==n)
src.Move(usr.loc)
else
src.removeAmounta(n)
var/obj/O = new src.type
/*78*/ O.addAmounta(n-1)
O.Move(usr.loc)

and this code, which is in another tab
obj
New()
..()
/*19*/ if(!src.stackablea)src.suffix=""


Vars.dm:19:error: src.stackablea: undefined var
Verbs.dm:61:error: object.addAmounta: undefined proc
Verbs.dm:78:error: O.addAmounta: undefined proc

here is the error again.
Lol to end a tag you have to do </dm> :P

Your error on line 19 is because you defined stackablea under /obj/stack.

Your error on lines 61 and 78 are because you defined those procs under /obj/stack and are trying to call them with an /obj.

Typecast the object you want - i.e. var/obj/stack/O = new src.type

Either that or you can use the : operator but I don't recommend that unless you know the problems that it could cause.
Page: 1 2