Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Check if 4 local int variables all carry different values?
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#2
RE: Check if 4 local int variables all carry different values?

PHP Code: (Select All)
SetLocalVarInt("Area1Num", RandInt(1, 9));
SetLocalVarInt("Area2Num", RandInt(1, 9));
SetLocalVarInt("Area3Num", RandInt(1, 9));
SetLocalVarInt("Area4Num", RandInt(1, 9)); 

This code will generate 4 numbers between 1 and 9. There's no guarantee that these will be different numbers, so you need to make sure they are.

A quick and dirty way would be changing the ranges to for example 1-3, 4-5, 6-7 and 8-9, however that would give it a fairly clear pattern.

I might've done something like this:

PHP Code: (Select All)
for(int i = 1; i <= 4; i++) // Loops 4 times
{
    
int random = RandInt(1, 9); // Generate a random value
    
    
while(RandomValueExists(random)) // If value has been generated before
    
{
        
random = RandInt(1, 9); // Try to generate another
    
}

    
SetLocalVarInt("Area"+i+"Num", random); // Set the value
} 

PHP Code: (Select All)
bool RandomValueExists(int value)
{
    for(
int i = 1; i <= 4; i++)
    {
        if(
value == GetLocalVarInt("Area"+i+"Num")) return true;
    }
    return 
false;
} 

Adding in a debug message after the setter gives me this:
[Image: NdM68c3.png]

(This post was last modified: 11-19-2018, 11:43 PM by Mudbill.)
11-19-2018, 11:37 PM
Find


Messages In This Thread
RE: Check if 4 local int variables all carry different values? - by Mudbill - 11-19-2018, 11:37 PM



Users browsing this thread: 1 Guest(s)