Can't add item to array

chongyunxiang

  • Posts: 271
I'm trying to add an item to a List, but an error came up saying that it can't convert the item to array. What does it mean?

Luyren

  • *
  • Posts: 2385
The error is in your ToyPreference behavior. Somewhere in your code (line 81) you are trying to do something with the Toy2 attribute as if it were a list (like adding an item to it), when that attribute isn't a list.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

chongyunxiang

  • Posts: 271
It can't be that the attribute was not a List. I tried to set CurrentTpreference to Tpreference and it works. But when I tried to get an individual number from the Tpreference list, it says it failed to convert to Array. Both are Lists.

Luyren

  • *
  • Posts: 2385
The error is in your ToyPreference behavior. Somewhere in your code (line 81) you are trying to do something with the Toy2 attribute as if it were a list (like adding an item to it), when that attribute isn't a list.
Did you go to that behavior and checked that line? You can click either the preview code button at the top or the binocular icon at the bottom right to see the code and the corresponding line number. That should point you towards the offending blocks. If you can then post a screenshot of those blocks, that would help more.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

chongyunxiang

  • Posts: 271
This is the screenshot for Tpreference behavior. I did the same for my Food preference and it works.

Luyren

  • *
  • Posts: 2385
As I said, you need to look for the line of code the error message says its the problem, and find the corresponding blocks in your code.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

chongyunxiang

  • Posts: 271
I've looked at the error message and it pointed to line 81, which is these blocks of code. However, its the same blocks used for my FoodPreference behavior.

Tpreference and CurrentTpreference are lists, just like foodpreference and currentfoodpreference. Only Tpreferencenumber and PreferenceNumber are number attributes.

It looks the same to me, and I have no idea why it doesn't work. I can set the CurrentTpreference list to be the same as Tpreference. But the error only happens when I try to select an item from the list, be it random or not.

TypeError: Error #1034: Type Coercion failed: cannot convert "ToyA" to Array.
   at MethodInfo-10187()[C:\Users\YunXiang\AppData\Roaming\Stencyl\stencylworks\games-generated\Virtual Pet V3\Source\scripts\Design_61_61_TPreference.hx:81]
   at com.stencyl.behavior::TimedTask/update()[D:\Stencyl\Stencyl\plaf\haxe\lib\stencyl\1,00\com\stencyl\behavior\TimedTask.hx:48]
   at com.stencyl::Engine/update()[D:\Stencyl\Stencyl\plaf\haxe\lib\stencyl\1,00\com\stencyl\Engine.hx:2574]
   at com.stencyl::Engine/postUpdate()[D:\Stencyl\Stencyl\plaf\haxe\lib\stencyl\1,00\com\stencyl\Engine.hx:2836]
   at com.stencyl::Engine/onUpdate()[D:\Stencyl\Stencyl\plaf\haxe\lib\stencyl\1,00\com\stencyl\Engine.hx:2828]

The error message is pointing to line 81 right?

Luyren

  • *
  • Posts: 2385
Quote
TypeError: Error #1034: Type Coercion failed: cannot convert "ToyA" to Array.
You are doing a list operation with the "ToyA" attribute in the "TPreference" behavior on line 81, when ToyA is not a list. That's what the error is saying. The screenshot you posted don't match the information in the error message, so you probably got the wrong place. There's a camera icon on the bottom right corner. You can click that and post a screenshot of your entire code, that outgh to make it easier to find the problem.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

chongyunxiang

  • Posts: 271
I've attached the screenshots of the block codes (using the camera icon), the error message and the preview code.

I've also tried to add items to the Tpreference list attribute using Settings > Attribute, but it's still the same error.

Luyren

  • *
  • Posts: 2385
That makes a bit more sense. Your problem is in that "do every 3 seconds, set CurrentTPreference" block. When your random item from the Tpreference list is selected, it is trying to be converted into a list before setting the value of "CurrentTpreference". My only logical conclusion is that you made "CurrentTpreference" into a list game attribute, instead of text or whatever you originally wanted.

« Last Edit: October 24, 2020, 01:32:59 pm by Luyren »
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

chongyunxiang

  • Posts: 271
Yes, my CurrentTpreference is a list game attribute, same as what I did for my food behavior, which are also List attribute and yet it works. But why does it not work for this behavior?

If it has to be a text attribute instead of a list, then how do I check if 2 Text Attribute are the same?

Luyren

  • *
  • Posts: 2385
You have a list game attribute. If you try to set its value using the "set" block, Stencyl will understand that whatever you place inside that block has to be another list, or a block that returns a list. You are putting a text in there, and Stencyl fails to convert that into an accepted value (list) and you get the error. Whatever you are doing in your other behavior is different.

Either switch to a textgame attribute, or use the set index block with your game attribute to the value you want.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

chongyunxiang

  • Posts: 271
Got it, I'll check my other behavior again to see what the difference was. What do you mean by set index block? I've searched for index blocks but the blocks doesn't seem like it's something I could use for what I'm doing.

Luyren

  • *
  • Posts: 2385
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.
Twitter

chongyunxiang

  • Posts: 271


Oh got it. I was searching for Index. Will certainly use this block for the texts. Thanks!