GDevelop-extensions icon indicating copy to clipboard operation
GDevelop-extensions copied to clipboard

New extension: Pseudo random

Open github-actions[bot] opened this issue 9 months ago • 46 comments

Description

Set seed to generate pseudo-random numbers in sub-events.

This extension will be useful for:

  • Re-creating a randomly generated level.
  • Debugging everything related to randomness in your game.
  • Use in multiplayer so that all players have the same random event without having to synchronize its components.

How to use the extension

How to use:

  • Put the action "Set seed" with a specific seed in the form of a number, for example "1", then all expressions and conditions of randomness in subevents will give the same result (each expression and condition separately) until you change the seed, meaning the expressions Random(), RandomInRange, RandomFloat() etc. and the condition "Pick a random instance".

Example:

  1. Create a new event with the condition "At the beginning of the scene" and the action "Set seed" and the seed equal to "1".
  2. Create a sub-event with the action "Log message" and the message "ToString(Random(100))+";"+ToString(Random(100))+";"+ToString(Random(100))".
  3. Look in the console and you will see that the result at each restart will be equal to "17;21;53" until you change the seed.

Checklist

  • [x] I've followed all of the best practices.
  • [x] I confirm that this extension can be integrated to this GitHub repository, distributed and MIT licensed.
  • [x] I am aware that the extension may be updated by anyone, and do not need my explicit consent to do so.

What tier of review do you aim for your extension?

Community (Unreviewed)

Example file

PseudoRandomExample.zip

Extension file

PseudoRandomExtension.zip


Extension by @Jurfix

You can update the extension on this "Pull Request" by commenting the update command: in the comment field, type !update, then drag and drop a zip file with your extension file in the commenting field, like how you initially submitted the extension. It should look like this:

!update [MyExtension.zip](https://github.com/GDevelopApp/GDevelop-extensions/files/12709661/MyExtension.zip)

It can take a few seconds for the file to fully upload and show as the above. Once it is like shown above, click "Comment" and let the bot do the rest!

github-actions[bot] avatar Apr 05 '25 22:04 github-actions[bot]

Thank you for submitting an extension.

Extensions are meant to be editable by anyone. This means that:

  • JavasScript code must not be minified (unless there is a technical reason)
  • A link to the repository or topic must be included if any

D8H avatar Apr 06 '25 11:04 D8H

!update PseudoRandomExtension.zip

Jurfix avatar Apr 06 '25 13:04 Jurfix

✅ Successfully updated the extension.

github-actions[bot] avatar Apr 06 '25 13:04 github-actions[bot]

Hi Davy @D8H, I replaced the minified library code with the non-minified one and added the repository link to the extension description, thanks.

Jurfix avatar Apr 06 '25 13:04 Jurfix

  • The link to the repository should be in a JS or event comment not in the description.
  • Overriding Math function may lead to unexpected side effects. For instance, particle emitters or pitch randomization may use Math.rand. If VFX and SFX are disabled, could it mess with the reproducibility of the game run?

D8H avatar Apr 06 '25 15:04 D8H

  • Overriding function may lead to unexpected side effects. For instance, particle emitters or pitch randomization may use . If VFX and SFX are disabled, could it mess with the reproducibility of the game run?Math``Math.rand

The Math.random() function is overridden only in the visibility zone after calling seedrandom(), that is, in the event where it is called and in subevents, in the rest of the game Math.random() works as before (randomly), usually I use it only once when starting a scene to build a level and the rest of the game works as before.

Jurfix avatar Apr 06 '25 16:04 Jurfix

If VFX and SFX are disabled, could it mess with the reproducibility of the game run?

I didn't quite understand the question, but neither the sound reproduction nor the creation of particle emitters affected the game's reproductive ability.

Jurfix avatar Apr 06 '25 16:04 Jurfix

!update PseudoRandomExtension.zip

Jurfix avatar Apr 06 '25 16:04 Jurfix

❗ No updates found. Please check your file.

github-actions[bot] avatar Apr 06 '25 16:04 github-actions[bot]

!update PseudoRandomExtension.zip

Jurfix avatar Apr 06 '25 16:04 Jurfix

❗ No updates found. Please check your file.

github-actions[bot] avatar Apr 06 '25 16:04 github-actions[bot]

!update PseudoRandomExtension.zip

Jurfix avatar Apr 06 '25 16:04 Jurfix

✅ Successfully updated the extension.

github-actions[bot] avatar Apr 06 '25 16:04 github-actions[bot]

I moved the repository link from the description to the js comment

Jurfix avatar Apr 06 '25 17:04 Jurfix

Hi Davy @D8H, I updated the example by adding random value generation next to pseudo random values, this can also be combined into one event if necessary, but then it will be unclear when which sound plays PseudoRandomExample.zip

Jurfix avatar Apr 07 '25 11:04 Jurfix

Yes, it can break certain processes in your game if you call "set seed" for the whole scene or the whole game, but you can easily control it by calling it only in one event (for example when starting a scene), that's why I set the extension category to "advanced", maybe a warning should be added, what do you think?

Jurfix avatar Apr 07 '25 11:04 Jurfix

What do you do to enable it only for 1 event and its sub-events?

D8H avatar Apr 21 '25 10:04 D8H

Just call the set seed function, in the event where it is called there will be a pseudo random, in all other events there will be a regular random

Jurfix avatar Apr 21 '25 10:04 Jurfix

How does the extension make it works?

D8H avatar Apr 21 '25 11:04 D8H

!update PseudoRandom.zip

Jurfix avatar Apr 21 '25 12:04 Jurfix

Can't update the extension, as it doesn't pass automatic tests:


❌ 4 Errors found in extension 'PseudoRandom':

  ⟶ ❌ [Filled out names and descriptions]: Required field 'fullName' of the function 'BackRandom' is not filled out!
  ⟶ ❌ [Filled out names and descriptions]: Required field 'description' of the function 'BackRandom' is not filled out!
  ⟶ ❌ [Filled out names and descriptions]: Required field 'sentence' of the function 'BackRandom' is not filled out!
  ⟶ ❌ [JavaScript disallowed properties]: Found disallowed properties in extension 'PseudoRandom':
{
  allowedProperties: [
    'makeUuid',           'rgbToHex',
    'rgbOrHexToRGBColor', 'rgbToHexNumber',
    'hexNumberToRGB',     'hexToRGBColor',
    'copyArray',          'staticArray',
    'staticArray2',       'staticObject',
    'toDegrees',          'toRad',
    'random',             'randomFloat',
    'randomFloatInRange', 'randomInRange',
    'randomWithStep',     'evtTools',
    'Variable',           'RuntimeObject',
    'Logger'
  ],
  disallowedProperty: '__PseudoRandomExtensionOriginalRandomFunction',
  objectName: 'gdjs'
}


❌ 4 Errors found in extensions - please fix them before generating the registry.

github-actions[bot] avatar Apr 21 '25 12:04 github-actions[bot]

I was wrong, it overrides the Math.random() function globally, not just in the event and sub-event, it seemed to me that this is because gdevelop itself returns the Math.random() function to its original form after some time (about a second). I added an action that overrides Math.random() to its original form forcibly, which must be called when the generation of values ​​is complete, I have not spent time on the design of all this yet, I just want to know if this is a normal solution and whether it solves your concerns before I design it as needed, thanks.

Jurfix avatar Apr 21 '25 12:04 Jurfix

To make this work only on one event and its sub-events, it should look like this: image

Jurfix avatar Apr 21 '25 12:04 Jurfix

I have not spent time on the design of all this yet, I just want to know if this is a normal solution and whether it solves your concerns before I design it as needed, thanks.

I would be more at ease with functions like NumberGenerator::Random() that don't have any side effect on standard libraries.

D8H avatar Apr 21 '25 13:04 D8H

NumberGenerator::Random()

Then it won't be so clean, for each generation of one number you will need to have a unique seed, that is, each time after calling the generation you will have to add 1 to the seed, in my opinion this is extra work, if you use it as I showed in the last message, then there should be no side effects, since Math.random returns to its original form in the same frame

Jurfix avatar Apr 21 '25 13:04 Jurfix

!update PseudoRandomExtension.zip

Jurfix avatar Jun 23 '25 13:06 Jurfix

Can't update the extension, as it doesn't pass automatic tests:


❌ 1 Error found in extension 'PseudoRandom':

  ⟶ ❌ [JavaScript disallowed properties]: Found disallowed properties in extension 'PseudoRandom':
{
  allowedProperties: [
    'makeUuid',           'rgbToHex',
    'rgbOrHexToRGBColor', 'rgbToHexNumber',
    'hexNumberToRGB',     'hexToRGBColor',
    'copyArray',          'staticArray',
    'staticArray2',       'staticObject',
    'toDegrees',          'toRad',
    'random',             'randomFloat',
    'randomFloatInRange', 'randomInRange',
    'randomWithStep',     'evtTools',
    'Variable',           'RuntimeObject',
    'Logger'
  ],
  disallowedProperty: '_PseudoRandom',
  objectName: 'gdjs'
}


❌ 1 Error found in extensions - please fix it before generating the registry.

github-actions[bot] avatar Jun 23 '25 13:06 github-actions[bot]

Hi @D8H , I updated the extension by removing the global override of Math.random() and replacing it with a local copy, and also added 5 expressions identical to the built-in random, only with pseudo random. It works identically to the previous version, except that now you need to call expressions from the extension PseudoRandom::Random(), ... instead of the built-in ones. I think this is a very useful and widely used thing, so I still expect to see it in this repository, thanks.

Jurfix avatar Jun 23 '25 13:06 Jurfix

Updated example if needed, but everything should be intuitive, you don't even have to call "Set seed" if you don't call it then the default seed will be 1, and each call to the expression will return the next step of the randomness PseudoRandomExample.zip

Jurfix avatar Jun 28 '25 09:06 Jurfix

The feature from this other PR could be integrated into your extension:

  • https://github.com/GDevelopApp/GDevelop-extensions/pull/1602

It will add a new NormalRandom function that uses your pseudo Random function. I can add it at the end of the review if you want.

This Godot class has the same kind of features if you want to take some ideas from it: https://docs.godotengine.org/en/stable/classes/class_randomnumbergenerator.html

D8H avatar Jun 28 '25 18:06 D8H