Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Gepard on Thu 03/03/2011 22:34:22

Title: SOLVED: add number to int in a function
Post by: Gepard on Thu 03/03/2011 22:34:22
How, I have this function that basically removes a certain item from a list.

   function LoseQuest(String text){
int p;
while (p < quests.ItemCount){ //Change ListBox1 with the name of your listbox
if (quests.Items[p].CompareTo(text, false) == 0){ //Change ListBox1 with the name of your listbox
quests.RemoveItem(p); //Change ListBox1 with the name of your listbox
PlaySound (10);
return;
}
p ++;
}
}


I also have an int called "experience". Now what I wish to do is add the possibility not only write what quest (item) will the player lose but also how much experience will he get for it. Any idea how? Thanks.
Title: Re: add number to int in a function
Post by: Khris on Thu 03/03/2011 23:12:46
Just add the xp the player gains as parameter to the function:

function LoseQuest(String text, int xp_gained) {     // ADDED int PARAMETER
int p;
while (p < quests.ItemCount) { //Change ListBox1 with the name of your listbox
if (quests.Items[p].CompareTo(text, false) == 0) { //Change ListBox1 with the name of your listbox
quests.RemoveItem(p); //Change ListBox1 with the name of your listbox
PlaySound (10);

experience += xp_gained;          // <------------ ADDED THIS LINE

return;
}
p ++;
}
}


Now call the function like this:
  LoseQuest("Tutorial", 20);
Title: Re: add number to int in a function
Post by: Gepard on Fri 04/03/2011 08:00:25
Hi. Thanks for the cod. Im having a problem with it as it wont compile:

First when I add exgain to integers:
---------------------------
Compile Error
---------------------------
'exgain' is a global var; cannot use as name for local.


Than without it:
---------------------------
Compile Error
---------------------------
Function declaration has wrong number of arguments to prototype.

So I do not know how to do it, although I know Im doing some silly mistake.
Title: Re: add number to int in a function
Post by: Matti on Fri 04/03/2011 14:39:41
If exgain is a global variable then just name it differently  ;)

Of course when you call the function you can put whatever variable you like as the parameter (like 'exgain').
Title: Re: add number to int in a function
Post by: Gepard on Fri 04/03/2011 16:49:41
EDIT: Now I see! I forgot to change the header of that function to match. Sorry guys.