Lost in a maze of nested functions!

Started by Zephyr, Wed 22/01/2020 11:20:53

Previous topic - Next topic

Zephyr

Hi Everyone,

Thank you for such a clear explanation about indentation!  That was one thing that was confusing me - being told to indent properly and yet seeing some of you adding a curly bracket right after the "if" bit.  Personally, I'd prefer to stick with the curly bracket on the next line, but I'm sure now that I understand what to do to make it clear. When I look at your coding, I can see what a difference it makes. Not that I intend to go in search of work in IT (this is strictly for fun for me!), but my fun isn't going to stop with this game and I'm always trying to learn more.

A special thanks to Khris for his coding example.  I think I get that and will give it a go.
You all ROCK!   (nod)
-
Zephyr,
:-* :-* :-*
<3 Zephyr <3

Zephyr

Aaargh!!!  Just hit a snag when I tried to run it.
Khris, it doesn't seem to recognise your "-1".  I put in everything (WITH proper indentation - you'd be proud!) in all the appropriate places, but here's what I got:

At the
Code: ags

if (ingredient_added[i]) {
    player.Say(String.Format("I already put %s in the bag.", ii.Name));
    return;

bit I got the error mssg:
"Error running function 'DrawstringBag_UseInv':
Error: Array index out of bounds (index:6, bounds:0..5)".

What went wrong? It made good sense to me!

- Zephyr.   (wtf)
<3 Zephyr <3

Cassiebsg

Remember that 0 counts as a number.

Array of 6 = 0, 1, 2, 3, 4, 5 and not 1, 2, 3, 4, 5, 6...  ;)
There are those who believe that life here began out there...

morganw

Yes. I think the error is likely in the section above, because the loop should only give you values between -1 and 5 (inclusive), but yours has somehow yielded a 6. You will probably have to post the whole function in-order for people to see where the problem is.

Zephyr

Hi MorganW,

The code reads exactly as Khris posted earlier. The only difference I made was to substitute "cChristian" for "Player".

-
Zephyr.
<3 Zephyr <3

morganw

Could you show it anyway? If only to check that a cat didn't walk across the keyboard when your back was turned.

Crimson Wizard

#26
I think Khris's code has a mistake

Code: ags

  int i, found = -1;
  for (i = 0; i < IC; i++) {
    if (ii == ingredients[i]) found = i;
  }
  <...>
  if (ingredient_added[i]) {


the "i" here may be equal to IC (array size) if it searched to the end of array. Should not it be "ingredient_added[found]" instead? Here, and few lines below too.

Spoiler

Code: ags
function iDrawstringBag_UseInv() {
  InventoryItem* ii = player.ActiveInventory;
	
  int found = -1;
  for (int i = 0; i < IC; i++) {
    if (ii == ingredients[i]) found = i;
  }
  // if not an ingredient
  if (found == -1) {
    player.Say("I'm not going to put that in the bag.");
    return; // exit function
  }
  // if already added
  if (ingredient_added[found]) {
    player.Say(String.Format("I already put %s in the bag.", ii.Name));
    return;
  }
  // not already added:
  player.Say("In the bag it goes.");
  ingredient_added[found] = true;
  // check if all ingredients are in the bag
  for (int i = 0; i < IC; i++) {
    if (!ingredient_added[i]) return; // not all ingredients, exit function
  }
  Display("The bag is full.");
  player.LoseInventory(iDrawstringBag);
  player.AddInventory(iFullBag);
}

[close]




PS
Quote from: Zephyr on Mon 24/02/2020 18:31:36The only difference I made was to substitute "cChristian" for "Player".

I'll only mention this, in the majority of cases it's recommended to use "player", which is an alias for "whoever currently is a player character". This lets you write more universal code that works regardless of what your player character is called. This is, of course, especially useful if character may be switched during game and same code should apply to all of them. This also lets you copy code to another game with less changes.

Khris

Yes, sorry, outside the loop it's supposed to be  found  instead of  i.

Zephyr

Hi All,

Thanks, Crimson Wizard, that now works perfectly.  One last thing though - if you take a look at my original script you'll see that I made provision to lose each ingredient after its addition to the bag.  Now, of course, it no longer does this. How and where can I fit this back into the new code please?

Sorry to be such a pain in the ass!

-
Zephyr.
<3 Zephyr <3

Matti

Add a line to this part:

Code: ags

  // not already added:
  player.Say("In the bag it goes.");
  ingredient_added[found] = true;
  player.LoseInventory(ingredients[found]);  // <-- add this line

Zephyr

Hi Matti,

You're a star! Thank you so much.
And thanks again to all of you - you've all been brilliant!  I'll try really hard not to bother you all again for a good long time!

-
Zephyr.   :-D
<3 Zephyr <3

SMF spam blocked by CleanTalk