Problem with script - shuffling cards

Started by NiksterG, Sun 17/02/2008 02:00:22

Previous topic - Next topic

NiksterG

Hello all,

I'm having the most mysterious problem with this script. I've been working on this game in version 2.72 for a while, and when 3.0 cam out, I upgraded. No problems. Then 3.0.1 came out, I upgraded again.

Then, in order to make scripting a bit easier for myself, I redid a lot of my older script and changed it to manage inventory with characters rather than arrays. That's when these weird errors occured.

This code worked in the previous, non-updated code and shuffled correctly. Now, not only does it not shuffle, but it also causes and error which crashes the game.

Code: ags

function ShufflePlayerDeck() { // shuffles player's deck twice
  int i=0;
	
  InventoryItem* playerdeck[40];
  SetGameOption(OPT_MULTIPLEINV, 1);
  
  while (i<PlayerDeck.ItemCount) {
    playerdeck[i] = PlayerDeck.ItemAtIndex[i];
    i++;
  }
  
  SetGameOption(OPT_MULTIPLEINV, 0);
	
  while (PlayerDeck.ItemAtIndex[0] != null) {
    cPlayerdeckc.LoseInventory(PlayerDeck.ItemAtIndex[0]);
  }
	
  InventoryItem* holder;
	
  i=0;
  while (i<GetCardsInPlayerDeck()) {
    int randomspot = Random(GetCardsInPlayerDeck()-1);
    holder = playerdeck[i];
    playerdeck[i] = playerdeck[randomspot];
    playerdeck[randomspot] = holder;
    i++;
  }

  i=0;
  while (i<GetCardsInPlayerDeck()) {
    int randomspot = Random(GetCardsInPlayerDeck()-1);
    holder = playerdeck[i];
    playerdeck[i] = playerdeck[randomspot];
    playerdeck[randomspot] = holder;
    i++;
  }
	
  i=0;
  while (i<40) {
    cPlayerdeckc.AddInventory(playerdeck[i]);
    i++;
  }
	
}





The GetPlayerCardsInPlayerDeck function simply checks to see how many cards in the player's deck.

Code: ags

function GetCardsInPlayerDeck() {
  int i=1;
  int numofcards = 0;
  while (i<=Game.InventoryItemCount) {
    numofcards = numofcards + cPlayerdeckc.InventoryQuantity[i];
    i++;
  }
  return numofcards;
}




I have 56 inventory items (cards) in my game so far. Anybody see the problem?
Check out my games! (Not all made with AGS)

Khris

What's the error message you are getting?

NiksterG

The error I'm getting is this:

Code: ags

---------------------------
Adventure Game Studio
---------------------------
An error has occurred. Please contact the game author for support, as this
is likely to be a scripting error and not a bug in AGS.
(ACI version 3.01.1002)

in "GlobalScript.asc", line 753
from "GlobalScript.asc", line 2111
from "room100.asc", line 7

Error: AddInventoryToCharacter: invalid invnetory number

---------------------------
OK   
---------------------------




Also, I should clarify that I have a total of 56 inventory items, but the player is holding only 26 of them.
Check out my games! (Not all made with AGS)

monkey0506

Which is line 753 of your global script?

Also, you wouldn't by chance happen to be using a custom text-window GUI would you? :o

NiksterG

What do you mean, which line is number 753? Don't you guys have ESP? Figure it out yourself!  :P

Line 753 is near the bottom of the ShufflePlayerDeck function where it says

Code: ags

cPlayerdeckc.AddInventory(playerdeck[i]);



And yes, I am using a custom text window GUI. How does that relate?
Check out my games! (Not all made with AGS)

Radiant

It would seem that playerdeck is out of range.

NiksterG

It's actually not. I checked by adding a Display command that shows the value of i (the index of playerdeck). The highest it ever got to was 26, which is obviously not out of bounds.

I also added a Display command to show which item was being added back to the deck, and though they were added back, it seems they were all added back in the order they were taken out. Is my shuffling algorithm messed up?
Check out my games! (Not all made with AGS)

monkey0506

The reason I asked about the custom text-window GUI is that I encountered some strange errors apparently relating to the Inventory myself...but when I sent CJ the Crash.dmp he said that it was a problem with the custom text-window GUI. IIRC he discovered there was an error in importing text-window GUIs, but it should be fixed now. Perhaps you ported to 3.0 before the fix was implemented, corrupting the text-window?

Of course...you say that you've used Display to check the value...but then I only encountered the error when trying to display information about the inventory...I don't know it just sounded somewhat similar to the errors I was getting.

However, in that display statement that you used "that shows the value of i", did you check the value of playerdeck itself? Something such as:

Code: ags
Display("i: %d, playerdeck\[i]: %d", i, playerdeck[i]);


Because it seems that the value stored in the playerdeck array is out of range.

NiksterG

I added that Display command again, like you suggested. Now I'm getting a brand new error, which I shouldn't be getting from adding a display command.

Code: ags

---------------------------
Adventure Game Studio
---------------------------
An internal error has occurred. Please note down the following information.
If the problem persists, contact Chris Jones.
(ACI version 3.01.1002)

Error: run_text_script1: error -6 running function 'room_a':
Error: Null pointer referenced
in "GlobalScript.asc", line 753
from "GlobalScript.asc", line 2112
from "room100.asc", line 7


---------------------------
OK   
---------------------------



So now, instead of the problem being that I was maybe adding a wrong inventory item, I am actually adding a null inventory item or something like that. I am so confused here! I'm trying everything I can think of. *sigh* Guess I'll keep looking...
Check out my games! (Not all made with AGS)

monkey0506

Wait a second. Character.AddInventory accepts an InventoryItem* as the parameter. Is playerdeck an array of InventoryItem*s? If not it should throw an error as soon as you try to store the InventoryItem via InvWindow.ItemAtIndex.

So for the value of playerdeck to be out of range...it would have to be a null pointer. Otherwise it would have to be related to the text-window GUI issue I was encountering:

Quote from: monkey_05_06 on Thu 10/01/2008 02:27:00There's possibly a very serious bug if you delete inventory items. It seems that although it shows the IDs for any items listed after the deleted item being updated, it doesn't always update them internally. If you'd like I can upload the CrashInfo.dmp file and/or possibly the game projects (working with the Demo Quest project, using multiple (two) projects).

I was ONLY accessing the inventory items via their script o-names.

There isn't any apparent relevance between running the game from the OS or using RunAGSGame; most of the errors persist either way.

Crash instances:

    * Referencing InventoryItem.ID from a room script using the o-name of an item
    * Referencing Game.InventoryItemCount from game_start
    * Using an inventory item (clicking on it in eModeInteract)
    * Looking at an inventory item (clicking on it in eModeLookat)
    * Referencing InventoryItem.ID from the global script using the o-name of an item


I also tried adding blank items back in place of the items I removed which seemed to clear up some of the errors, but most remained.

The error doesn't seem to bear any relevance to where the items may have previously been at in the item list as I thought. The errors seem almost random now. For example, adding the blank items back as placeholders cleared up the interact-mode issue, while the look at-mode issue remains.

Quote from: Pumaman on Mon 14/01/2008 20:50:00Thanks -- have you deleted the sprite for one of the sides/corners of your textwindow GUI? And on a side note, I've just realised that the editor doesn't stop you doing so, which it should really.

Quote from: monkey_05_06 on Mon 14/01/2008 21:51:00I don't think I've changed it at all. Though I did have to export the text window GUI and reimport it.

Quote from: Pumaman on Wed 16/01/2008 20:44:00Ah, it could be that textwindow GUIs don't import properly then. I'll look into that.

Quote from: PumamanWhat are the changes in 3.0 RC 5?

* Sprite Usage check now includes detecting if the sprite is used as a GUI Text Window edge
* Fixed exporting text window GUIs not including the edge images

Pumaman

It seems to me that this code:
Code: ags

  while (i<40) {
    cPlayerdeckc.AddInventory(playerdeck[i]);
    i++;
  }

is always going to try and add 40 items to the inventory -- but if your previous code only set up to playerdeck[26] for example, then when it tries to AddInventory(playerdeck[27]) that's unlikely to work.

monkey0506

Hmm..sadly this is actually the first time I've looked at the original code snippet. CJ's probably right. Changing that to something like:

Code: ags
if (playerdeck[i] != null) cPlayerdeckc.AddInventory(playerdeck[i]);


Would probably correct it.

Guess that's what I get for not actually looking at the actual problem. :-\

NiksterG

Quote from: monkey_05_06 on Sun 17/02/2008 21:58:32
Changing that to something like:

Code: ags
if (playerdeck[i] != null) cPlayerdeckc.AddInventory(playerdeck[i]);


Would probably correct it.

You would think so, right? Well, not quite. :(

I changed the code to this:

Code: ags

i=0;
while ((i<40) && (playerdeck[i] != null)) {
  cPlayerdeckc.AddInventory(playerdeck[i]);
  i++;
}


I have to keep the (i<40) because it is possible for the player to have up to 40 inventory items later in the game.

Yet this script is still not working. I get this error message:

Code: ags

---------------------------
Adventure Game Studio
---------------------------
An internal error has occurred. Please note down the following information.
If the problem persists, contact Chris Jones.
(ACI version 3.01.1002)

Error: Character.InventoryQuantity: invalid inventory index

---------------------------
OK   
---------------------------


Now I'm just really confused. I don't know what to try anymore.
Check out my games! (Not all made with AGS)

Pumaman

Have you tried using the Script Debugger to help with this one?

If not, go to one of the first lines of code in the function (eg. "SetGameOption(OPT_MULTIPLEINV, 1);") and press F9. Then run the game with F5 and it should break out at that point, and allow you to step through the code.

Whilst you can't see the variable values, you might be able to get an idea from the behaviour of the code about where it's going wrong.

NiksterG

No, I hadn't tried the script debugger. I didn't know it existed! I just tried it after checking up on it in the manual. That is a neat debugging tool!

My problem was that I was looking in the wrong place in my code. I naturally thought the problem was in the ShufflePlayerDeck function since that's what I was editing when the error occured. It was actually three functions away. I accidentally set a variable to an index out of bounds, like you guys thought.

Thank goodness (or rather, Chris) for that debugger, or I'd never had found the problem!
Check out my games! (Not all made with AGS)

Pumaman


SMF spam blocked by CleanTalk