Author Topic: How is this random?  (Read 310 times)

Legacy_Numos

  • Newbie
  • *
  • Posts: 22
  • Karma: +0/-0
How is this random?
« on: October 11, 2012, 07:54:41 am »


               So I'm working on a portal that's supposed to lead you to a variable location each time my server is reset. In theory it seemed fairly simple:

void main()
{
   object oPC = GetLastUsedBy();
   string sPortal;
   location lPortal;
   if (GetLocalInt(OBJECT_SELF, "ao_portal") == 0)
   {
       SetLocalInt(OBJECT_SELF, "ao_portal", Random(3)+1);
   }

   int nExplore = GetLocalInt(OBJECT_SELF, "ao_portal");

   switch (nExplore)
   {
       case 1: sExplore = "ao_wp_portal001";
       case 2: sExplore = "ao_wp_portal002";
       case 3: sExplore = "ao_wp_portal003";
   }

   lPortal = GetLocation(GetObjectByTag(sPortal));
   AssignCommand(OBJECT_SELF, ClearAllActions(TRUE));
   AssignCommand(oPC, ActionJumpToLocation(lPOrtal));
}


Whenever I try to test it in singleplayer I always get brought to the waypoint that corresponds with case 3.

Its very late and I'm at my wit's end. Is there something here I'm over-looking?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
How is this random?
« Reply #1 on: October 11, 2012, 08:07:46 am »


               The problem is in your switch.  You need to add breaks to stop case  2 and 3 from being set if a lower number is selected. 

switch (nExplore)
{
case 1: sExplore = "ao_wp_portal001"; break;
case 2: sExplore = "ao_wp_portal002"; break; 
case 3: sExplore = "ao_wp_portal003";
}


EDIT:  
Just to explain a little encase it was not just  an oversite on your part.  

unlike a chained if.. else if... statement that automaticly enbeds code to jump execution control to the end of the chain once a TRUE case if found.   the switch statment does not.   If your Switch is 1 without the breaks added to the code, all three cases are executing.    So sExplore will be set to equal   "ao_wp_portal001" then to "ao_wp_portal002"  then to "ao_wp_portal003"  Giving you the same resualt as case three every time.   

Basicly 

 



switch (x) 
{
    case 1:
    case 2:
    case 3:
        SpeakString(" x = 1, 2 or 3");      
        break;
    default:
          SpeakString("x is not 1, 2 or 3");
}

 

  
               
               

               


                     Modifié par Lightfoot8, 11 octobre 2012 - 07:31 .
                     
                  


            

Legacy_AgedAlchemist

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
How is this random?
« Reply #2 on: October 13, 2012, 06:29:02 am »


               Wouldn't it be a bit more straightforward to set the target Tag as a string on the Portal?

//  Portal OnUsed script ...
void main()
{
    object oPC = GetLastUsedBy();
    string sPortal = GetLocalString(OBJECT_SELF, "ao_portal");
    //  Check for empty sPortal string ...
    if (sPortal == "")
    {
        sPortal = "ao_wp_portal00" + IntToString(Random(3)+1);
        SetLocalString(OBJECT_SELF, "ao_portal", sPortal);
    }
    location lPortal = GetLocation(GetObjectByTag(sPortal));
    AssignCommand(oPC, ClearAllActions(TRUE));
    AssignCommand(oPC, JumpToLocation(lPortal));
}

               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
How is this random?
« Reply #3 on: October 13, 2012, 02:03:19 pm »


               

AgedAlchemist wrote...

Wouldn't it be a bit more straightforward to set the target Tag as a string on the Portal?


//  Portal OnUsed script ...
void main()
{
    object oPC = GetLastUsedBy();
    string sPortal = GetLocalString(OBJECT_SELF, "ao_portal");
    //  Check for empty sPortal string ...
    if (sPortal == "")
    {
        sPortal = "ao_wp_portal00" + IntToString(Random(3)+1);
        SetLocalString(OBJECT_SELF, "ao_portal", sPortal);
    }
    location lPortal = GetLocation(GetObjectByTag(sPortal));
    AssignCommand(oPC, ClearAllActions(TRUE));
    AssignCommand(oPC, JumpToLocation(lPortal));
}



A bit off topic,  But a good point.   Along that line of thought,  How about just setting the local as the object to jump to.
 
//  Portal OnUsed script ...
void main()
{
    object oPC = GetLastUsedBy();
    object oWayPoint = GetLocalObject(OBJECT_SELF, "ao_portal");

    //  Check for Valid WayPoint ...
    if (!GetIsObjectValid(oWayPoint))
    {
        oWayPoint = GetObjectByTag("ao_wp_portal00" + IntToString(Random(3)+1));
        SetLocalObject(OBJECT_SELF, "ao_portal", oWayPoint);
    }
 
   AssignCommand(oPC, JumpToObject(oWayPoint));
}
               
               

               


                     Modifié par Lightfoot8, 13 octobre 2012 - 01:04 .
                     
                  


            

Legacy_Pattycake1

  • Jr. Member
  • **
  • Posts: 98
  • Karma: +0/-0
How is this random?
« Reply #4 on: October 13, 2012, 05:13:31 pm »


               Here is my question, do you want it to be random per person on reset or one random location for everone per server reset?