Frictional Games Forum (read-only)
[SCRIPT] Check if 4 local int variables all carry different values? - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [SCRIPT] Check if 4 local int variables all carry different values? (/thread-56087.html)



Check if 4 local int variables all carry different values? - HappyMatt12345 - 11-19-2018

Well here I am again, yet again trying to make a puzzle and having troubles getting the script to do what I want it to. Basically, I'm attempting to make a puzzle where the player needs to put rocks on a pressure plate, and it was working until I had the bright idea to try to make the combination randomly generate when the map is loaded. I have tried everything I can think of, and I have everything working except when I try to leave it as it was when it was not giving me errors, it was entirely up to luck whether or not the code would be all different numbers(which is what I want as the player needs to leave 4 rocks on separate pressure plates) or a few numbers being the same. My thought here was to run it on a boolean that would return true if all numbers were unequal to any other. this is where I have been stuck for the last hour or so, and I have decided I'm tired of it so I'm posting here. I will copy NOT the entire code file, but all code that is involved in this particular puzzle, into a code block thing, take a look: 


Code:
void OnStart(){
    SetLocalVarInt("Area1Num", RandInt(1, 9));
    SetLocalVarInt("Area2Num", RandInt(1, 9));
    SetLocalVarInt("Area3Num", RandInt(1, 9));
    SetLocalVarInt("Area4Num", RandInt(1, 9));

       SetRockComboPuzzleActive();
}

void SetRockComboPuzzleActive(){
    if(CheckAreaValues() == true){
        SetLocalVarString("Area1", "CodeRockPlaceArea_"+GetLocalVarInt("Area1Num"));
        SetLocalVarString("Area2", "CodeRockPlaceArea_"+GetLocalVarInt("Area2Num"));
        SetLocalVarString("Area3", "CodeRockPlaceArea_"+GetLocalVarInt("Area3Num"));
        SetLocalVarString("Area4", "CodeRockPlaceArea_"+GetLocalVarInt("Area4Num"));
        AddDebugMessage("Combo is "+GetLocalVarInt("Area1Num")+", "+GetLocalVarInt("Area2Num")+", "+GetLocalVarInt("Area3Num")+", "+GetLocalVarInt("Area4Num"), false);
        for(int i = 1; i <= 4; i++){
            AddEntityCollideCallback("CodeRock1", GetLocalVarString("Area"+i), "PlaceRock", false, 0);
            AddEntityCollideCallback("CodeRock2", GetLocalVarString("Area"+i), "PlaceRock", false, 0);
            AddEntityCollideCallback("CodeRock3", GetLocalVarString("Area"+i), "PlaceRock", false, 0);
            AddEntityCollideCallback("CodeRock4", GetLocalVarString("Area"+i), "PlaceRock", false, 0);
        }
    }
    else{
        ResetAreaValues();
    }
}

bool CheckAreaValues(){
    // Im completely at a loss as to what to put here, as everything I have tried either creates an error or simply doesnt function as I want it to.
}

void ResetAreaValues(){
    SetLocalVarInt("Area1Num", RandInt(1, 9));
    SetLocalVarInt("Area2Num", RandInt(1, 9));
    SetLocalVarInt("Area3Num", RandInt(1, 9));
    SetLocalVarInt("Area4Num", RandInt(1, 9));
    if(CheckAreaValues() == true){
        SetRockComboPuzzleActive();
    }
}

void PlaceRock(string &in asParent, string &in asChild, int alState){
    if(alState == 1){
        for(int i = 1; i <= 4; i++){
            if(asChild == GetLocalVarString("Area"+i) && GetLocalVarBool("A"+i+"Occ") == false){
                SetLocalVarString("A"+i+"OccBy", asParent);
                SetLocalVarBool("A"+i+"Occ", true);
                AddLocalVarInt("RocksPlaced", 1);
                AddDebugMessage("LocalVarString 'A"+i+"OccBy' has been updated to "+GetLocalVarString("A"+i+"OccBy"), false);
                AddDebugMessage("LocalVarBool 'A"+i+"Occ' has been updated to "+GetLocalVarBool("A"+i+"Occ"), false);
                AddDebugMessage("LocalVarInt 'RocksPlaced' has been updated to "+GetLocalVarInt("RocksPlaced"), false);
            }
        }
    }
    else{
        for(int i = 1; i <= 4; i++){
            if(asChild == GetLocalVarString("Area"+i) && asParent == GetLocalVarString("A"+i+"OccBy")){
                SetLocalVarString("A"+i+"OccBy", "CurrentlyUnassigned");
                SetLocalVarBool("A"+i+"Occ", false);
                AddLocalVarInt("RocksPlaced", -1);
                AddDebugMessage("LocalVarString 'A"+i+"OccBy' has been updated to "+GetLocalVarString("A"+i+"OccBy"), false);
                AddDebugMessage("LocalVarBool 'A"+i+"Occ' has been updated to "+GetLocalVarBool("A"+i+"Occ"), false);
                AddDebugMessage("LocalVarInt 'RocksPlaced' has been updated to "+GetLocalVarInt("RocksPlaced"), false);
            }
        }
    }
}
 
Help is much appreciated as I have tried a bunch of things... Thank you...


RE: Check if 4 local int variables all carry different values? - Mudbill - 11-19-2018

PHP Code:
SetLocalVarInt("Area1Num"RandInt(19));
SetLocalVarInt("Area2Num"RandInt(19));
SetLocalVarInt("Area3Num"RandInt(19));
SetLocalVarInt("Area4Num"RandInt(19)); 

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:
for(int i 1<= 4i++) // Loops 4 times
{
    
int random RandInt(19); // Generate a random value
    
    
while(RandomValueExists(random)) // If value has been generated before
    
{
        
random RandInt(19); // Try to generate another
    
}

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


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


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


RE: Check if 4 local int variables all carry different values? - HappyMatt12345 - 11-20-2018

(11-19-2018, 11:37 PM)Mudbill Wrote:
PHP Code:
SetLocalVarInt("Area1Num"RandInt(19));
SetLocalVarInt("Area2Num"RandInt(19));
SetLocalVarInt("Area3Num"RandInt(19));
SetLocalVarInt("Area4Num"RandInt(19)); 

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:
for(int i 1<= 4i++) // Loops 4 times
{
 
   int random RandInt(19); // Generate a random value
 
   
    while
(RandomValueExists(random)) // If value has been generated before
 
   {
 
       random RandInt(19); // Try to generate another
 
   }

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


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


Adding in a debug message after the setter gives me this:
[Image: NdM68c3.png]
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.