ID:723505
 
(See the best response by Keeth.)
Code:
    Use_an_item(var/i in usr.Items)
set category="Player Verbs"
var/num=text2num(i)
i-="[num]"
i="[--num] [i]"


Problem description:
Hello! I am currently working on a verb that decrements the number of an item in your inventory using a string file, currently what I am doing is using, text2num(thevar) to separate the number, but then I am unsure how to remove the previous number so that I can work with the text file without it. After it would simply be decrement the number and add it to the beginning of the string that was then shortened. Are there any operators I can use on the string or something like that?
I don't really get what your trying to do but:
i-="[num]"
i="[--num] [i]"


Shouldn't be possible. Since you can't subtract text from a number (Vice-versa).

I don't really get what it is you want to do, but. . . I hope that I helped you a bit though.

#Edit: Why did you convert "i" from text into numbers if you use it as an text?
im trying to subtract the number as text from the larger text var that should already have it inside of it. I don't know if or how it is possible, therefore my post.
I guess what I am asking is if it is possible to parse a text var in Byond.
    Use_an_item(var/i in usr.Items)
set category="Player Verbs"
var/num=text2num(i) //Make an number out of the i.
i=(num-1) // This is the same as making i-1 and converting it into a number.
i="[i]" // Convert the variable i back into text.

I'm on my phone so I haven't tested it but. . .
Something like this:
    Use_an_item(var/i in usr.Items)
set category="Player Verbs"
i=text2num(i) //Convert the text into numbers
i-=1 // Subtract 1 (quantity)
i = "[i]" // Convert the number into text.


Should work better.

I still haven't mastered DM language, but I hope this will work the way you want it to.
I'm confused.

What is in this usr.Items list, exactly? From your usage, I would have to assume it is just a list of strings of numbers, which would mean the name of this list is very misleading, so I am willing to bet that isn't the case.

What EXACTLY are you trying to do here? Gimme the play by play. What is in the list, what number are you trying to reduce?
text2num only works if the number is at the very beginning of the string. If it is, then the function will return purely the number as an integer.
If the initial part of the string isn't a number, it will return null.

What I'd suggest you do, and this may be a bit brute-forcey and hacky, is;

    Use_an_item(var/i in usr.Items)
set category="Player Verbs"
var/num=text2num(i)
var/tempi
num-=1
var/strNum=num2text(num)
tempi = i
i="[strNum][tempi]"


Edit:
What exactly is i? An item? If so you need to be setting i.name, or what you've done is force the item to become a string.

Is there a reason you're not just storing a quantity variable for each item, and doing something like /var/item/name = "[quantity] The Name of the Item"?
What is in items is a string in the format "[quantity] [name], [desc]" so the number is Always first, I haven't had the chance to look at any of the coding posted here yet as many of you were asking what was in the string itself, so here it is.
An example would be "100 Crossbow Bolts, These can be used in Crossbows etc."
In response to MasterSpectra
The problem with this one is I want the number followed with the name and description.
i.quantity -= 1
i.name = "[i.quantity] [name], [desc]"

Shouldn't that work?
In response to Deathguard
tempi would contain the previous number if I'm not mistaken, so for the given example would it not make i="99 100 Crossbow bolts, ... etc"?
In response to MasterSpectra
the problem is I do not have the separate variables without a way to parse the string, that is what I am trying to figure out how to do if possible.
There's some fundamental design confusion going on here then; there should definitely be a quantity variable associated with an item, not just included in item.name.

Can you post the code you have for items, please?
Sorry just finished watching the Avengers, here is the code:

mob/verb
Use_an_item(var/i in usr.Items)
set category="Player Verbs"
var/num=text2num(i)
i-="[num]"
i="[--num] [i]"//the verb word for word
/////////items is used in the following context////

statpanel("Inventory")
stat("Platnum: [Plat]")
stat("Gold: [Gold]")
stat("Silver: [Silver]")
stat("Copper: [Copper]")
stat("Equipment:")
for(var/p in Equipment)
stat("[p]")
stat("Items:")
for(var/p in Items)
stat("[p]")
////////The game is meant to be very hands on and this seemed much simpler than the alternatives, at least from a c++ point of view, all I need to know to make this work is a way to parse the string/text after the first space(which is at the end of the quantity) and then i can simply arrange a new string as id like////
You seem to be under a grave misapprehension. Why is your items list a list of strings? Are you gonna make generic verbs that parse your entire item list to look for a particular string that contains "sword" or "mace" or something to equip a sword or mace?

Your Items list should be a list of objects of some kind. You should not be manually parsing strings to determine what kind of item or how many you have.
Keep in mind this is for a Dungeons and dragons game for Byond. I dont plan to do anything specific game wise with this information, therefore it is easier to store and keep up with the information like this(at least to me), but that is besides the point, whether or not I am mistaken, the question remains if there is a way to parse a text variable and if so what is it?
Best response
Of course you can parse "a text variable." Check the reference. We have findtext and copytext. The rest just follows from there. Find the first space in the text, grab all of the text from the beginning of the string until the space, and then assume that is the stack count for the item.

var/found = findtext(string, " ")
if(!found)
CRASH("No space!? What does it mean!?")

var/count = text2num(copytext(string, 1, found))
if(!count)
CRASH("It could have been 0, or not a number at all. Both of these cases would be erroneous. ERRONEOUS I SAY.")

var/theRestOfTheString = copytext(string, found+1)

// then do whatever you need to do with [count] and [theRestOfTheString]


That is, of course, not the only problem with your code. You can't just assign a new string to the variable holding the string, and it'll edit the string in it. All you'll do is reassign the value in there. Not only that, but strings are passed by value, so any modification you make to the string in [i] won't alter anything but itself. You'll have to find the index your string is located at in usr.Items and then reassign the value there.

But more importantly... you've been asked a few times why you're using strings for this, though, and I want to hear the answer for that.

BYOND is object-oriented for a reason.
Use objects.

// use /objs since you can actually interact with them on the map and such. unless you aren't going to be doing that.
obj
var
stackCount = 1

proc
// add to the stack
increaseStackCount(by=1)
stackCount += by

// remove from the stack
decreaseStackCount(by=1)
stackCount -= by

// returns something to the effect of "100 crossbolts - used in crossbows." for an item named "crossbolts", with a count of 100, and a descripton of "used in crossbows."
// simply use this every time you want to represent the item (stack) in text form.
getText()
if(stackCount > 1)
. += "[stackCount] "

. += name

if(desc)
. += " - [desc]"


I really cannot see one good reason for using a list of strings to represent objects. Please tell me why you're doing this. If you can explain why you would want to do so adequately, I'd be happy to help further.
Because They can have as many items as they can possibly think of. (Drastic but in reality the diverseness of the items and the sheer number of them would make trying to include a named object variable for each of them a lubricious waste of time)
I am also already using .contents for an entirely different thing. And I do not see how I could make displaying the items in the players inventory in a stat tab any easier, the way I have it set up atm (parsing not added) takes up only 8 lines of code.
Even if what you're saying is true, using a text string still makes no sense here (or anywhere, really). What I said earlier would be a much better way of implementing this. If your objects don't differ at all, use a generic object, give it a name, an item count, and a description. You are literally already doing this, except you're putting it all in a string that you don't even know how to parse. :|

Use an object. It's what they're for. Grouping data, among other things.
In response to TheVongola10th
I'm happy that I don't think that way, it's a waste of time trying to do that.
Page: 1 2