Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | • 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
Script Help Check if 4 local int variables all carry different values?
HappyMatt12345 Junior Member

Posts: 16
Joined: Oct 2018
Reputation: 0
11-19-2018, 07:17 PM
Mudbill Muderator

Posts: 3,816
Joined: Apr 2013
Reputation: 177
 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: (This post was last modified: 11-19-2018, 11:43 PM by Mudbill.)
11-19-2018, 11:37 PM
HappyMatt12345 Junior Member

Posts: 16
 RE: Check if 4 local int variables all carry different values? (11-19-2018, 11:37 PM)Mudbill Wrote: 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: Thank you, this works and is more compact than an if statement with a ton of conditions. I need to learn more about utilizing variables and return statements clearly. Developer of Order of the Skull