[Solved] Add to and Display Array Strings

I’m having an issue with arrays. I have created a simple shop where a user can click an item to “purchase” it. It subtracts the amount from their total currency, adds the item to an inventory, and adds the item name to an array.
On a different scene I am trying to have a random element’s text displayed. I have attempted to do this by setting the range from 0 to a variable that stores the length of the array (adds 1) every time an item is “purchased”.
The issue is, all the text displays is “0” instead of a random object name from the array. The images of what I have done are posted here as well. Any help would be appreciated. Thanks!!!

I think the issue might stem fron the ToString(GlobalVariable(…)). GlobaleVariable returns an int. You’re trying to read a string. Remove the ToString part, and use GlobalVariableString(…), and see if that makes a difference.

As MrMen said, GlobalVariable will convert your variable to a number (as there is no number in your text, it uses the default 0). But there are other improvements you could make.

  1. One of the main purposes of arrays is not having to bother with tracking it’s length. You can simply use the GlobalVariableChildCount(Inventory) expression

  2. Your buying events are repeating a lot of time the same thing. You could use an object group and object variables to make it just one event B) :

  • Put BerserkBtn, GoldKeyBtn, etc into a Buy button objects group
  • Right click on the object in the object list and click on edit object variables, and add in each of them a variable price
    • The price should correspond to the numbers you hardcoded, e.g. 30 for GoldKeyBtn or 45 for BerserkBtn
  • Finally, remove all your semi duplicate events and only keep one. In it, replace the object name by the object group name and the hardcoded price with ObjectGroupName.Variable(price)

That way, you can add many objects to your store while keeping your event sheet readable :wink:

2 Likes

Thank you, both of those worked like a charm. The only issue now is the “Client” and “ClientSpeech” objects are spawning infinitely on top of each other. I tried adding the “Trigger Once” condition but it just stops it from spawning altogether.
I’m not sure if I’m supposed to start a new thread or if you can answer this here but thank you again.

That’ll be because in your second to last set of events, you are creating the “Client” and “ClientSpeech” objects without ever deleting them.

I’d suggest that either before the create actions, you delete the objects, or after a set time delete them (delete Client and delete ClientSpeech will remove all instances of those two objects on the scene).

I don’t want to delete all of the instances right away. I only want it to create one instance in the first place. I intend to require more user input before I remove the objects.

Then you will need to reset the timer when you create the text boxes. Otherwise they’ll be created every single frame after a while.

I’ve changed a few things and now it only spawns once, however the text still defaults to zero sometimes. I’m not sure why this is.

That is due to the keying of arrays: arrays are keyed by indices beginning at 0. That means that to access the first item in it, you would access array[0]. The length also doesn’t return the last index name, but the amount of items.

For this array ["hello", "world", "foo"], there is 3 items, so the child count expression returns 3. To access the 3rd element though, you need to do array[2], as indices begin by 0 (0 → first element, 1 → second element, 2 → third element). To make it select a random element, you must therefore use RandomInRange(0, GlobalVariableChildCount(Inventory) - 1).

1 Like

Thank you, I got it now. Such a silly mistake!

1 Like

It’s not a silly mistake, it’s not something really intuitive, especially if you haven’t coded before. I’m glad you got it working :slight_smile: Good luck with your game!