Sometimes, Linked Text is wrong place

Hello everyone,
I am making a random shop. Now I encountered a problem is that the price sometimes placed in wrong place.
When it’s ok
(upload://6xjH0KRr6GNX49FOXwJlNlfBU5U.png)
When it’s wrong
(upload://mgFLlEJsQMQ7BtPYC2PqXvBmHnr.png)
Here’s the code

What should I do now?

When it’s ok

When it’s wrong

I don’t fully understand what’s happening but I have a couple of theories.

When you pick random animations, there could be duplicates. If there are then the price gets added. Although, because the events are only expecting 1 object they can’t add more than 1 text object. It adds 1 text object for the older object or since it’s a group, it might be the object that was added to the group list the earliest.

Either way, it only adds 1 text object and because of the trigger once, it won’t add another.

Now the 2nd issue. Say a sword was picked on the first roll. If a sword is picked on the 2nd roll (or a repick when the items are the same multiple times) , I don’t think it will trigger again. A different object might have the sword animation but since it’s using a group list, the trigger once didn’t get reset. To clear the trigger once all of the objects would have to not be the selected animation.

My recommendations.

If the text objects are always going to be in the same place then they don’t need to be added and deleted each time.

You could add and link them at the beginning with a for each object using the group name. You would then just need to pick the linked objects.

You could simplify the adding of the prices by using a structure with the prices. Get the price using the animation name as the child name. It’s best (required to) not use spaces with child names , so the animation names shouldn’t have spaces. Another option would be to have the price at the end of the animation name and use SubStr() to get the last say 3 characters. I prefer the structure.

As for picking the items. There are a lot of posts about picking random numbers. My preferred method is to create an array with the animation numbers, say 0,1, 2. Shuffle the array and then there’s an array tools action that removes the value from the array. That way, you can’t get duplicates. You then recreate the array before picking new numbers. Again, there are a lot of threads about picking numbers that don’t repeat.

Try to simplify and fix things. If you need more help, we’re all here at various times.

Here’s a concept.

At the beginning add the text and link

When you roll
Create an array with the possible animation numbers. You can use repeat with a variable counter or the array tools split a string into an array action.
Use for each object to pick a random item for each object in the group. The action removes the item from the array and assigns it to a variable. Use that variable to set the animation.
Pick the text linked to the object.
Use the value in a price structure to change the text to the price and set the objects price variable.

All of the events should be subevents of the roll button. There’s no need to have them processed on every frame.

1 Like

You should link objects by checking if object does not have linked object yet
And create then object to link

Here i create text for each stone that does not have text and is in some range

1 Like

I’d like to suggest a similar approach to what @Keith_1357 suggested. Store the cost values in a structure variable, with the object name as the key:


Then assign the text to each object using the variable structure:


That’ll cut down on the number of events to write, make it scalable for when you add more items and easier to modify the cost of items.

2 Likes

Hello guys, I tried to store costs in Structure but it seems sometimes it still put the text in wrong place. And the text is bold, it seems many texts in one position.


I tried RandomInRange for just one ItemList, it seems ok, no wrong placed texts but the text still bold (Bold is not enable)
But the problem of Randominrange is the items are sometimes duplicated.


Here when they are duplicated


Anw, I think randominarray sometimes make wrong with animations so the text will place in wrong placed.
I will test linked text to each items object (no more animation change in use), it may work but more work to do.

Yes, because the deletion of text happens once when Reroll is clicked, but the creation is happening every frame.

Move the second event to be a subevent of the first one, so it’s only actioned when the Reroll object is clicked:


BTW, you could add the button state extension and behaviour to the Reroll object, and then you just need one condition “Reroll is clicked”.

1 Like

Well, the bold is gone but prices seem duplicate now and the first frame (beginning of preview) wont show price anymore until I click the reroll button



Ah, my bad, I only looked at the later screen snip you posted with just the reroll events…

To fix the price being displayed:

  1. Move the Delete ItemCostsText action to be the first event in the Repeat for each AllItemList subevent, and
  2. Move that subevent to a stand-alone event.

am I doing something wrong? it doesn’t work

How do you mean it doesn’t work? Does it not show the values initially, does it show “0” for some or all of them or does it do something else?

If it shows “0”, then it’s an indication that the Structure key and animation name don’t match. remember they’re case sensitive. “Flame” is treated as different to “flame”.

Only one price in the third place is shown


hello guys, I will testing more on random shop but if we set price and item name in variable array/structure, it means it is static price, right? Now I want each reroll will increase the price in a way (ex: percentage increase), what should I do?

I think the random shop and price is the hardest part of my project, haha :joy:

But after all, the Gdevelop is a very interesting engine. I hope I will not encounter a serious bug that make me give up :wink: of course, now I’ve not encountered one like that

smh, I can’t believe I missed that. The Delete ItemCostText action is being applied to all the ItemCostText objects and will delete all the previous ones created every time a new ItemCostText is created.

Put the action Delete ItemCostText in its own event, between the “Reroll is clicked” event and the “Repeat for each AllItemsList” events

I will test again when I reach my computer.
Do you have any idea about my new concept?