Setting up a very simple random number function in emevd - SekiroResurrection/modding-wiki GitHub Wiki
A random number generator function can often be useful to bring variety in your world events. There are primarily two type of random number generators, pseudo random number generators and truly random number generators.
Here we will attempt to discuss how to create a truly (sorta) random number generator between 0
and desired number X-1
.
To make things simple we will also being using a function directly available in the emedf file, since we cannot generate randomness through the game in any other way.
-
//randomly sets event flags within a given range RandomlySetEventFlagInRange()
-
//clears the value in a range of event flags ClearEventValue()
//Random number generator function
//X0_4 : Starting bit flag
//X4_4 : Ending bit flag
//X8_4 : Trigger flag
Event(10000, Restart, function(X0_4, X4_4, X8_4) {
IfEventFlag(MAIN, ON, TargetEventFlagType.EventFlag, X8_4);
RandomlySetEventFlagInRange(X0_4, X4_4, ON);
WaitFixedTimeSeconds(0.1);
RandomlySetEventFlagInRange(X0_4, X4_4, OFF);
SetEventFlag(X8_4, OFF);
EndUnconditionally(EventEndType.Restart);
});
//Random number clear function
//X0_4 : Starting bit flag
//X4_4 : Bits used
//X8_4 : Highest value (excluding)
Event (10001, Restart, function(X0_4, X4_4, X8_4) {
IfEventValue(MAIN, X0_4, X4_4, ComparisonType.Greater, X8_4);
ClearEventValue(X0_4, X4_4);
EndUnconditionally(EventEndType.Restart);
});
- Copy both the above events into
common_func.emevd
, to allow them to be used as per your requirement across other event scripts.1 - Make sure both the events are initialized in the emevd that you want to create the random number.
- Since event scripts represent numbers in flag states, you will need to decide on the following parameters to passed while initializing the event.
- For event
10000
-
X0_4
: Starting bit flag. Defines the flag to be used for the nth bit of the random number. -
X4_4
: Ending bit flag. Defines the flag to be used for the 0th bit of the random number.2 -
X8_4
: Trigger flag. Defines the flag to be set asON
if a new random number is to be generated.
-
- For event
10001
-
X0_4
: Starting bit flag. Defines the starting bit flag to be used for the starting bit of the random number. This MUST be the same flag as the that was used in the previous function. -
X4_4
: Number of bits used. -
X8_4
: This is highest value below which you want random numbers to be generated.
-
- For event
- Whenever the Trigger flag is set to
ON
, the script randomly sets one flag in the specified range toON
and most likely another one toOFF
- This range of flags is then to be interpreted as a whole number which will appear to have a randomness to it. The random number will always be between
0
and desired numberX-1
. - You can check the random number using the function
IfEventValue()
.
Setting a random speffect
on the player, when player uses the healing gourd.
// Initialization placed in event 0
InitializeCommonEvent(10000, 5568, 5569, 5570); // initializes random number generator
InitializeCommonEvent(10001, 5568, 2, 3); // initializes random number clear function
SetEventFlag(5570, ON); // randomizes once in the beginning to set the random number to a non zero value as all flags are initialized to 0 for the very first time.
// Sets the random number trigger flag every time player used healing gourd
Event(10002, Restart, function(){
IfCharacterHasSpEffect(MAIN, 10000, 3000, true, ComparisonType.Equal, 1);
SetEventFlag(5570, ON);
EndUnconditionally(EventEndType.Restart);
});
// checks if the random number is 0 and sets speffect 3030 to the player
Event(10003, Restart, function(){
IfEventValue(AND01, 5568, 2, ComparisonType.Equal, 0);
SetSpEffect(10000, 3030);
EndUnconditionally(EventEndType.Restart);
});
// checks if the random number is 1 and sets speffect 3031 to the player
Event(10004, Restart, function(){
IfEventValue(AND01, 5568, 2, ComparisonType.Equal, 1);
SetSpEffect(10000, 3031);
EndUnconditionally(EventEndType.Restart);
});
// checks if the random number is 2 and sets speffect 3032 to the player
Event(10005, Restart, function(){
IfEventValue(AND01, 5568, 2, ComparisonType.Equal, 2);
SetSpEffect(10000, 3032);
EndUnconditionally(EventEndType.Restart);
});
1. In reality these event numbers can be anything that you have not used across all event files. Please use the same event numbers for initialization as well.
2. Determined by the formula, EndingBitFlag = StartingBitFlag + number of bits to be used - 1