Hi everyone.
Okay, here's the situation...in my game when the playable character uses specific items on himself, he gains a special ability. The first time he does it, a message will pop up explaining the ability and how to use it. After that, when he uses these specific items, it will simply tell him the ability is activated. With my (probably unnecessarily long) script, I did manage to get that working. And then I added the "else" statement at the end, which would display a message for any other item he uses which would not activate his ability.
The problem is that after the activation message comes up as detailed in my "else if" statements, it follows with my "else" message display. i.e., "Scare Stare Activated" "You can't use that on yourself."
I think I understand why it's doing that, I just can't find away around it. Also, I know my scripts can be condensed in some way but mainly I just want it to work. I've tried it in a number of ways with more or less the same results.
Here's my script:
function cPup_UseInv()
{
if ((cPup.ActiveInventory == iRadish1)&&(scarestare == 0)) {
scarestare += 1;
btnIconStare.Enabled = true;
cPup.LoseInventory(iRadish1);
Display(" New Ability Aquired: SCARE STARE!");
Display(" Upon eating anything that's edible, Pupshaw will gain the ability to intimidate with SCARE STARE. However it may only be used once until Pupshaw finds something else to eat. The SCARE STARE ability can be found in the icon bar above.");
}
else if ((cPup.ActiveInventory == iRadish1)&&(scarestare > 0)) {
scarestare += 1;
btnIconStare.Enabled = true;
cPup.LoseInventory(iRadish1);
Display(" SCARE STARE activated!");
}
if ((cPup.ActiveInventory == iRadish2)&&(scarestare == 0)) {
scarestare += 1;
btnIconStare.Enabled = true;
cPup.LoseInventory(iRadish2);
Display(" New Ability Aquired: SCARE STARE!");
Display(" Upon eating anything that's edible, Pupshaw will gain the ability to intimidate with SCARE STARE. However it may only be used once until Pupshaw finds something else to eat. The SCARE STARE ability can be found in the icon bar above.");
}
else if ((cPup.ActiveInventory == iRadish2)&&(scarestare > 0)) {
scarestare += 1;
btnIconStare.Enabled = true;
cPup.LoseInventory(iRadish2);
Display(" SCARE STARE activated!");
}
if ((cPup.ActiveInventory == iRadish3)&&(scarestare == 0)) {
scarestare += 1;
btnIconStare.Enabled = true;
cPup.LoseInventory(iRadish3);
Display(" New Ability Aquired: SCARE STARE!");
Display(" Upon eating anything that's edible, Pupshaw will gain the ability to intimidate with SCARE STARE. However it may only be used once until Pupshaw finds something else to eat. The SCARE STARE ability can be found in the icon bar above.");
}
else if ((cPup.ActiveInventory == iRadish3)&&(scarestare > 0)) {
scarestare += 1;
btnIconStare.Enabled = true;
cPup.LoseInventory(iRadish3);
Display(" SCARE STARE activated!");
}
else {
Display(" You can't use that on yourself.");
}
}
Based on that code, you're not using an else if for the first iRadish3 statement. So if you're using iRadish1 or iRadish2 then it would always display the final else block of the iRadish3 segment.
So you can either make this line:
if ((cPup.ActiveInventory == iRadish3)&&(scarestare == 0)) {
into:
else if ((cPup.ActiveInventory == iRadish3)&&(scarestare == 0)) {
Or you could simply include some return; statements into your function which would prevent the function from executing any further code.
P.S. You can use the [code] and [/code] tags to display your code like I did above. Just put your code between the two tags. ;)
P.S.S. GG has a very good point about the code clean-up below. I was trying to make sure no one beat me to the post! :=
Edit: Monkey beat me to it
The way you put the final "else" only relates to the statement "if ((cPup.ActiveInventory == iRadish3)&&(scarestare == 0)) [...]0 else if ((cPup.ActiveInventory == iRadish3)&&(scarestare > 0))", so as long as any of the previous statements (iRadish1 or iRadish2) are true, the final else will still run. One way around this is to simply put an "else" in front of every "if" except the first. The code could also be cleaned up a bit, since all three radishes seem to have the same effect - but perhaps they are only placeholders?
function cPup_UseInv() {
if ((cPup.ActiveInventory == iRadish1) || (cPup.ActiveInventory == iRadish2) || (cPup.ActiveInventory == iRadish3)) {
if (scarestare == 0) {
scarestare += 1;
btnIconStare.Enabled = true;
cPup.LoseInventory(cPup.ActiveInventory);
Display(" New Ability Aquired: SCARE STARE!");
Display(" Upon eating anything that's edible, Pupshaw will gain the ability to intimidate with SCARE STARE. However it may only be used once until Pupshaw finds something else to eat. The SCARE STARE ability can be found in the icon bar above.");
}
else { //I'm assuming here that scarestare can never be negative
scarestare += 1;
btnIconStare.Enabled = true; //should this be necessary if scarestare is already larger than 0? Depends on the rest of your code
cPup.LoseInventory(cPup.ActiveInventory);
Display(" SCARE STARE activated!");
}
}
else Display(" You can't use that on yourself.");
}
Wow, GG's code worked like a charm. I had seperated the three radishes into three if's because I thought I had to specify the item under LoseInventory. I didn't know I could use Active Inventory. It seems so obvious now. The btnIconStare.Enabled = true is necessary because once the icon is clicked and used, it becomes disabled. See the last if in the following code:
function on_mouse_click(MouseButton button) {
// called when a mouse button is clicked. button is either LEFT or RIGHT
if (IsGamePaused() == 1) {
// Game is paused, so do nothing (ie. don't allow mouse click)
}
else if (button == eMouseLeft) {
ProcessClick(mouse.x, mouse.y, mouse.Mode );
if (mouse.Mode==eModeTalkto)
player.FaceLocation(mouse.x, mouse.y);
if (mouse.Mode==eModeTalkto)
cPup.Say(" ");
if (mouse.Mode==eModeStare){
cPup.FaceLocation(mouse.x, mouse.y);
cPup.Think(" ");
btnIconStare.Enabled = false;
mouse.SelectNextMode();
See, I use the Stare ability as the thinking view. Now I'm trying to figure out a way that if the icon is enabled, and he tries using a radish on himself, it'll display a message saying something like "you've already eaten" and not allow him to lose the radish until he uses the ability and disables it. Maybe I have to use variables?
And thank you monkey for telling me about how to post my code. I was trying to figure that out too. I hope it worked. Thanks a lot guys, I was struggling for two days on this.
Is there a reason you need 3 different items? Why couldn't you give 3 copies of iRadish?
-Supposing they have different graphics, but are not available at once--> you can use the item.Graphic/CursorGraphic properties.
-If they have the same graphic, and can be available together--> switch the general option 'Display multiple inventory items multiple times'
Just a couple ideas to toss out.
~Trent
Quote from: jfarwyke on Thu 30/04/2009 22:46:39Now I'm trying to figure out a way that if the icon is enabled, and he tries using a radish on himself, it'll display a message saying something like "you've already eaten" and not allow him to lose the radish until he uses the ability and disables it. Maybe I have to use variables?
Why use variables when AGS is already keeping track of the icon's status for you ;)? Simply add this to the top of your script:
function cPup_UseInv() {
if ((cPup.ActiveInventory == iRadish1) || (cPup.ActiveInventory == iRadish2) || (cPup.ActiveInventory == iRadish3)) {
if (btnIconStare.Enabled == true) Display("You've already eaten!"); //added line
else if (scarestare == 0) { //changed line
and keep the rest the same.
It's okay, I just set it up for something better, where the player can eat all three radishes if he chooses and then after he uses the ability three times, it'll become disabled. I even have it so the cursor stays the same until the ability is exhausted, that way the player won't have to keep going up to the icon bar.
No Trent, there's no reason except that I didn't know anything about copies. I had to give the objects different names so I assumed the inventory items had to be named likewise. It's okay though, it's already done and works great. Thanks guys. I'm sure I'll be back with a new problem.
SOLVED!