# Object name depending on a variable & Random pick among values with weightings

Hello! I’ve been using GDevelop for a while now, and I have 2 suggestions about new functions/possibilities that could be made. I programmed things with a lot of Variables involved, so my suggestions might make things easier for that kind of games/softwares.

An object name depending on a variable.

It would be handy if the conditions and actions dealing with objects could accept object titles depending on a variable. Currently, if I’m not wrong, such conditions and actions can only accept a specific object, that can be picked in the objects picklist.

Let’s say I have 5 objects named “button.1”, “button.2”, “button.3”, “button.4”, “button.5”. It would be great if I could run a condition / action about them, calling them button[VariableString(n)], and then vary the variable n.

It could allow us to run loops about objects, or make an action on any object depending on a variable.
Maybe is that already possible? (but I’ve never seen that) Or is there a reason why it is not possible…?
I know an object is not a variable, but it could be handy to be able to deal with the object name more freely.

Pick a random value (with weightings)

It would be great to have a random function picking a value among several given values.

Something like :

RandomPick(2;5;8;9;11)

The same function could exist with weightings (a probability between 0 and 1. Either the probabilities sum has to be 1, either the left probability leaves the variable unmodified.)

Something like:

RandomPickWeighted(2,10/100 ; 5,15/100 ; 8,36/100 ; 9,20/100 ; 11,19/100)

I know such random results can be made with the current tools and functions available in Gdevelop, for example :

• set Variable(q) to RandomInRange(1,100)
• if 0<q<11 set Variable(Q) to 2
• if 10<q<26 set Variable(Q) to 5
• if 25<q<62 set Variable(Q) to 8
• if 61<q<82 set Variable(Q) to 9
• if 81<q<101 set Variable(Q) to 11

Nevertheless in some kind of games dealing with a lot of variables, random events, auto-generation, such a random function could be more than useful.

So such a function would make things easier, and the game developer could see things more clearly : RandomPickWeighted(2,10/100 ; 5,15/100 ; 8,36/100 ; 9,20/100 ; 11,19/100)

Let me know what you think

I think the biggest issue is that objects aren’t identified by their name, and even then their name is a reference and not actually a string.

(Also, separate feature requests should be separate posts, but not a huge issue for this one)

I think for your math request, I believe that would need to exist in the main Javascript.math() library to be implemented, and I’m not seeing anything that would be close to that natively.

However, someone may be able to make an extension using some of the scripts here?

Thank you for you reply! And sorry for the single post about two requests.
My main need would be the Random function, so thanks for the link, though I’ve never use the Javascript. I guess it should not be that difficult to code.
Let’s see if anyone has ever needed the same functions that me, or if it’s only me.

To simulate wieghted random numbers in GDevelop, you could create an array with each number being added the number of times according to it’s weight, and then picking a random array entry.

So, say the weightings are 1:1/10, 2:4/10, 4:3/10, 5:2/10, then the array would end up looking like:
[1, 2, 2, 2, 2, 4, 4, 4, 5, 5]

1 Like

Thank you for this other idea MrMen!

Actually, since I don’t know nothing about Javascript, I’ve just created an external event in GDevelop that gives a random value with weightings. I’ll use it like a function in my scenes.
I enter in my scene for example this event :
Change the text of scene variable RandomPickWeighted set to “1,20 ; 3,15 ; 4,30 ; 8,12 ; 11,10 ; 13,1 ; 14,1 ; 15,11”
And then I use the link to the exteral event and the variable RandomPickWeighted becomes a number among the values I wrote before.
(the external event first extracts the values from the string and then uses the values to give a random number).
If anyone ever interested I can share that external event.

Still, if such a function would exist, coded in Javascript, it would be great!