Aller au contenu

Photo

How is this random?


  • Veuillez vous connecter pour répondre
4 réponses à ce sujet

#1
Numos

Numos
  • Members
  • 11 messages
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?

#2
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
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 .


#3
AgedAlchemist

AgedAlchemist
  • Members
  • 4 messages
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));
}


#4
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

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 .


#5
Pattycake1

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