The online racing simulator
[Solves] Random Location Job help!
Hello everyone!

I have made a job system where it will randomly generate a location name and coordinates to a specific area.


<?php 
#Random Locations 
GlobalVar $RandomLoc$RandomLoc[0] = "0"$RandomLoc[1] = "1"$RandomLoc[2] = "2"
$RandomLoc[3] = "3"$RandomLoc[4] = "4"$RandomLoc[5] = "5"$RandomLoc[6] = "6"
$RandomLoc[7] = "7"$RandomLoc[8] = "8"$RandomLoc[9] = "9"$RandomLoc[10] = "10"
$RandomLoc[11] = "11"$RandomLoc[12] = "12"$RandomLoc[13] = "13";

#Location number 1
CASE "1":
$RandomLocation round(ToNum(RandomNum(0,13)),0)."";
SetCurrentPlayerVar("CurrentJob"$RandomLoc[ToNum($RandomLocation)]);
PrivMsg("^7Delivery text example to ^3" $RandomLoc[ToNum($RandomLocation)]);
BREAK;
?>

What I have realized is, If location 1 generated a random location THAT IS THE SAME
The player can simply finish the job without moving a finger at the same place.

I added this code below
$RandomLocation = round(ToNum(RandomNum(0,13)),0)."";

Result:

<?php 
#Avoid generating location number 1 at location number 1.
IF (ToNum($RandomLocation != 1))
THEN
#Set current job blah..
ENDIF
?>

But one issue again, if it generates location 1 THEN ignores it, then the player has no location to go to, and therefore the job isn't set.

Any solutions would be highly appreciated!
You should make a loop with a check if the new location isn't the same as the current location.

Make draw -> Check if number isn't same as current location ->
- If the same, draw new number, go back to check
- If not the same, procede
#3 - lucaf
Not sure if I understood your full procedure but can't you just simply create a loop that reads new $RandomLocation until it differs from the current one
Quote from Yisc[NL] :You should make a loop with a check if the new location isn't the same as the current location.

Make draw -> Check if number isn't same as current location ->
- If the same, draw new number, go back to check
- If not the same, procede

I did a loop with PrivDelayedCommand, unfortunately it doesn't work. i get no errors as well.
Better create a FOR or WHILE loop, using a variable you set to 'false' before the loop and change it to 'true' when a valid number has come up.
Quote from Yisc[NL] :Better create a FOR or WHILE loop, using a variable you set to 'false' before the loop and change it to 'true' when a valid number has come up.

Mind giving me an example? I barely work with FOR loops and never worked on WHILE loops.
I have found a example somewhere on the lapperforum. There is not proper tutorial made for this.

But the code below, is an explaination how the Splittoarray() function works.
See if it is usefull or not. Tongue



<?php 
$tobesplit 
"one,two,three";
            
$splits SplitToArray(  $tobesplit,"," ); 
            
########################################################

            //Result on screen with a single line

                
WriteLine"The result for pos 1 is " $splits[1] ); #Displayed "one" in command window
                
WriteLine"The result for pos 2 is " $splits[2] ); #Displayed "two" in command window
                
WriteLine"The result for pos 3 is " $splits[3] ); #Displayed "three" in command window
            ########################################################
            ########################################################

            //Display results with a FOREACH loop
            
            
FOREACH( $val IN $splits)
                    
WriteLine"Pos = " $val["value"]);
            ENDFOREACH
            
            
#Result on screen
            
Val one
            Val 
two
            Val 
Three
            
########################################################
            ########################################################

            //Display results with a FOREACH loop
            
            
$i 1;    
            WHILE(
$i 4)
                
WriteLine"Pos ".$i."=" $splits[$i]);
                
$i=$i+1#Count up
            
ENDWHILE

                
Results that will be displayed in Command window
                Pos 1 
one
                Pos 2 
two
                Pos 3 
Three
            
########################################################
            ########################################################

            //Display results with a FOR loop
            
            
FOR($i=1;$i<4;$i=$i+1)
                
WriteLine"Pos ".$i."=" $splits[$i]);
            ENDFOR

                
Results that will be displayed in Command window
                Pos 1 
one
                Pos 2 
two
                Pos 3 
Three
            
########################################################
?>

** Best answer **
Quote from iceman121 :Mind giving me an example? I barely work with FOR loops and never worked on WHILE loops.

Of course I don't mind Smile
I think this example does what you want and shows how to use a WHILE-loop.


<?php 
CatchEvent OnLapperStart
()
    
OnLapperStart_Random_number_loop();
EndCatchEvent

CatchEvent OnMSO
$userName$text # Player event
    
$idxOfFirtsSpace indexOf$text" ");

    IF( 
$idxOfFirtsSpace == -THEN
      $command 
$text;
      
$argv "";
    ELSE
      
$command subStr$text,0,$idxOfFirtsSpace );
      
$argv trimsubStr$text,$idxOfFirtsSpace ) );
    ENDIF

    SWITCH( 
$command )
        CASE 
"!rnbr":
            
Random_number_loop$KeyFlags );
            BREAK;
    ENDSWITCH
EndCatchEvent

Sub OnLapperStart_Random_number_loop
()
    
### Declare GlobalVar ####
    
GlobalVar $RandomLoc;

    
$RandomLoc[0] = "0";
    
$RandomLoc[1] = "1";
    
$RandomLoc[2] = "2";
    
$RandomLoc[3] = "3";
    
$RandomLoc[4] = "4";
    
$RandomLoc[5] = "5";
    
$RandomLoc[6] = "6";
    
$RandomLoc[7] = "7";
    
$RandomLoc[8] = "8";
    
$RandomLoc[9] = "9";
    
$RandomLoc[10] = "10";
    
$RandomLoc[11] = "11";
    
$RandomLoc[12] = "12";
    
$RandomLoc[13] = "13";
    
### End ###
EndSub

Sub Random_number_loop
$KeyFlags )
    IF ( 
GetCurrentPlayerVar("CurrentJob") == "")
    
THEN
      SetCurrentPlayerVar
("CurrentJob"1);
    ENDIF

    
privmsg "^7DEBUG 1: " GetCurrentPlayerVar("CurrentJob") );

    
$valid_Loc="false";

    WHILE (
$valid_Loc == "false")
        
$RandomLocation round(ToNum(RandomNum(0,13)),0)."";

        IF ( 
$RandomLocation != GetCurrentPlayerVar("CurrentJob") )
        
THEN
          SetCurrentPlayerVar
("CurrentJob"$RandomLoc[ToNum($RandomLocation)]);
          
PrivMsg("^7Delivery text example to ^3" $RandomLoc[ToNum($RandomLocation)]);
          
$valid_Loc="true";
        ELSE
          
privmsg "^7DEBUG 2: Number is the same as RandomLoc" );
        ENDIF
    ENDWHILE
EndSub
?>


Quote from Yisc[NL] :Of course I don't mind Smile
I think this example does what you want and shows how to use a WHILE-loop.


<?php 
CatchEvent OnLapperStart
()
    
OnLapperStart_Random_number_loop();
EndCatchEvent

CatchEvent OnMSO
$userName$text # Player event
    
$idxOfFirtsSpace indexOf$text" ");

    IF( 
$idxOfFirtsSpace == -THEN
      $command 
$text;
      
$argv "";
    ELSE
      
$command subStr$text,0,$idxOfFirtsSpace );
      
$argv trimsubStr$text,$idxOfFirtsSpace ) );
    ENDIF

    SWITCH( 
$command )
        CASE 
"!rnbr":
            
Random_number_loop$KeyFlags );
            BREAK;
    ENDSWITCH
EndCatchEvent

Sub OnLapperStart_Random_number_loop
()
    
### Declare GlobalVar ####
    
GlobalVar $RandomLoc;

    
$RandomLoc[0] = "0";
    
$RandomLoc[1] = "1";
    
$RandomLoc[2] = "2";
    
$RandomLoc[3] = "3";
    
$RandomLoc[4] = "4";
    
$RandomLoc[5] = "5";
    
$RandomLoc[6] = "6";
    
$RandomLoc[7] = "7";
    
$RandomLoc[8] = "8";
    
$RandomLoc[9] = "9";
    
$RandomLoc[10] = "10";
    
$RandomLoc[11] = "11";
    
$RandomLoc[12] = "12";
    
$RandomLoc[13] = "13";
    
### End ###
EndSub

Sub Random_number_loop
$KeyFlags )
    IF ( 
GetCurrentPlayerVar("CurrentJob") == "")
    
THEN
      SetCurrentPlayerVar
("CurrentJob"1);
    ENDIF

    
privmsg "^7DEBUG 1: " GetCurrentPlayerVar("CurrentJob") );

    
$valid_Loc="false";

    WHILE (
$valid_Loc == "false")
        
$RandomLocation round(ToNum(RandomNum(0,13)),0)."";

        IF ( 
$RandomLocation != GetCurrentPlayerVar("CurrentJob") )
        
THEN
          SetCurrentPlayerVar
("CurrentJob"$RandomLoc[ToNum($RandomLocation)]);
          
PrivMsg("^7Delivery text example to ^3" $RandomLoc[ToNum($RandomLocation)]);
          
$valid_Loc="true";
        ELSE
          
privmsg "^7DEBUG 2: Number is the same as RandomLoc" );
        ENDIF
    ENDWHILE
EndSub
?>



You're actually a genius haha! thanks a lot, that helped!
One more thing Yisc, I realized if number is the same as randomloc it will spam $RandomLocation like 30+ times as i debugged to check which number it was generating with PrivMsg($RandomLocation) right below it.

Is this normal?
Yes, since the WHILE-loop keeps going as long as $valid_Loc is equal to "false".
There might be a better way to solve this by generating random number in one Sub, then go to other Sub for a check and back to first Sub if check fails.

Something along this line:


<?php 
Sub Random_number_loop
$KeyFlags )
    IF ( 
GetCurrentPlayerVar("CurrentJob") == "")
    
THEN
      SetCurrentPlayerVar
("CurrentJob"1);
    ENDIF

    
privmsg "^7DEBUG 1: " GetCurrentPlayerVar("CurrentJob") );

    
$valid_Loc="false";
    
$RandomLocation round(ToNum(RandomNum(0,13)),0)."";

    
Check_number$KeyFlags );
EndSub

Sub Check_number 
$KeyFlags )
    [
ACTUAL CHECK]
    [if 
okayset the playervar]
    [if 
not okay]
    
Random_number_loop$KeyFlags );
EndSub
?>


Quote from Yisc[NL] :Yes, since the WHILE-loop keeps going as long as $valid_Loc is equal to "false".
There might be a better way to solve this by generating random number in one Sub, then go to other Sub for a check and back to first Sub if check fails.

Something along this line:


<?php 
Sub Random_number_loop
$KeyFlags )
    IF ( 
GetCurrentPlayerVar("CurrentJob") == "")
    
THEN
      SetCurrentPlayerVar
("CurrentJob"1);
    ENDIF

    
privmsg "^7DEBUG 1: " GetCurrentPlayerVar("CurrentJob") );

    
$valid_Loc="false";
    
$RandomLocation round(ToNum(RandomNum(0,13)),0)."";

    
Check_number$KeyFlags );
EndSub

Sub Check_number 
$KeyFlags )
    [
ACTUAL CHECK]
    [if 
okayset the playervar]
    [if 
not okay]
    
Random_number_loop$KeyFlags );
EndSub
?>



Thank you so much for your help! Smile appreciate it.
This thread is closed

FGED GREDG RDFGDR GSFDG