Int to String problems

Started by Kinoko, Wed 02/03/2005 04:40:01

Previous topic - Next topic

Kinoko

I'm creating a function for deleting items, funnilly enough called "DeleteItem", here:

Code: ags

function DeleteItem () {
  if (currentitem>0) {
    currentitem--;
    }
}


My problem is that 'currentitem' is a string, created elsewhere in the script when the 'cursor' is on a particular item in the inventory. Everytime the cursor is on an item, there's a piece of code like: StrCopy (currentitem, ltunic);

'ltunic' (and the others like it, one for each item) is an int that contains how many of that item exist.

I know why this doesn't work, obviously, but there are about 100 items and I've already put the "StrCopy (currentitem, ltunic);" line in for all of them... is there anything I can do to the DeleteItem function code to make the 'currentitem' string "revert back" to it's original string value... ie, Can I make 'currentitem' read as 'ltunic' (or equivalent) containing that int's value?

To stop me from going, say:
Code: ags

if (currentitem==ltunic) ltunic--;
else if (currentitem==gchilli) gchilli--;
else if (currentitem==rchilli) rchilli--;
... etc


Sort of stupid and confusing, I know. I've been doing this for hours now and I can't think straight.

EDIT: Okay, I just realised using StrCopy like that doesn't work. Instead, I could simply place the value of the int into currentitem, but I still have the problem of having to write "ltunic--;" over and over for each item. Is this unavoidable?

Gilbert

I'm afraid, that the best way is to change the 100+ lines.

Moreover, what version of AGS are you using? As far as I know (just checked with V2.62), StrCopy (currentitem, ltunic); shouldn't work as it will generate a compiler error.

Kinoko

Heheh, thanks Gilbot. Just realised that while you were replying, I think.

Ah well. Perhaps I could make this a suggestion then...? To allow you to somehow transport the name of an int then use it elsewhere.

My global script is just getting so long, I'm looking for any way to stop repeating lines over and over.

strazer

What you're doing sounds overly complicated.

You do know that you can have more than one of the same inventory item?

Depending on what you want to do exactly, also consider using arrays.

Kinoko

Well, it's because I'm not using the engine's inventory functions, because of the way my game works...

strazer

Then how about something along the lines of:

Code: ags

// main script header

#define LCHILI 0
#define GCHILI 1
#define LTUNIC 2


Code: ags

// main global script

int itemcount[3];


Code: ags

currentitem = LTUNIC;


Code: ags

DeleteItem() {
  itemcount[currentitem]--;
}

Kinoko

Ah, it's a good code but I do have multiples of each item. That's what it's deleting, the number of a certain item.

strazer

I know.

int itemcount[3];

is an array, basically 3 ints in 1. One int for each item. Each int holds the amount for each item. You can access the int of each item through the [index].

Kinoko

Ohhh right. Sorry, I'm still almost completely unfamiliar with arrays. Thanks for that, anyway ^_^ I'll give it a go. Incidentally, during other parts of my game, can I still incrwase item counts by using, say ltunic++ ? Or will I have to use itemcount[ltunic]++ ?

strazer

You have to decide what the variable is used to represent.

The way you have it now, you have seperate variables for each item, so ltunic itself holds the amount for this item, so you do ltunic++;

But if you go the array way, ltunic is only the index where to find the amount of the item in the itemcount array, i.e. you have to do itemcount[ltunic]++;
(Since the index of each item will stay the same, it's simpler to just use an LTUNIC define as in my example above.)

Again: The array itemcount holds the amount for all 3 items and you access each amount via the item's index (0 to size_of_array-1):

itemcount[0]; // amount of first item
itemcount[1]; // amount of second item
itemcount[2]; // amount of third item

Just be sure never to access the array out-of-bounds, i.e. if you have an array of size 3 as in my example above, the valid index is 0 to 2, so DON'T do:
itemcount[3]++; // out-of-bounds error

Kinoko

#10
Thanks very much for your help ^_^ I'm a bit slow on the arrays, but I think I have it now.

EDIT: One other thing, does the ltunic in the define line have to be in capitals? I notice that's how people always write these things but I don't know if it has to be like that or not.

Also,

Code: ags

currentitem = ltunic;


Is currentitem an int or string here?

Traveler

No, it doesn't HAVE to be all-capitals, but it'll make reading your own code much easier to read, since you can just differentiate between variables and defines simply by looking at them. Most programmers use capital defines in other languages, too (like C/C++, etc.)

Using capital defines also gives you more name combinations (since the language is case-sensitive, so 'ltunic' and 'LTUNIC' are different to the compiler. This way you can have both symbols for different purposes and you can still tell them apart easily.

If you don't like them, however, you can type it differently.

strazer

QuoteIs currentitem an int or string here?

It's an int, the index of the currently selected item.

SMF spam blocked by CleanTalk