Skip to content

help setting random local int

I want to check for item when PC opens door then set random of 4 chooses local int on pc and party.
Here's what I have so far.

void main()
{



object oPC = GetLastOpenedBy();

if (!GetIsPC(oPC)) return;

if (GetItemPossessedBy(oPC, "mg_key1")== OBJECT_INVALID)
return;
object oPartyM = GetFirstFactionMember(oPC);
// If not in party avoid loop
if(!GetIsObjectValid(oPartyM))
{
SetLocalInt(oPartyM,"randomport",1);
return;
}
while(GetIsObjectValid(oPartyM))
{
SetLocalInt(oPartyM,"randomport",1);
oPartyM = GetNextFactionMember(oPC);
}
}

thnx in advance as always

Comments

  • CalgacusCalgacus Member Posts: 273
    edited November 2020
    You can use the Random function to get a random number. You also do not need one of those "if" blocks, I removed it.
    You also need to make one pass to get PCs and one to get the NPCs in the party.

    Does this look like what you are going for?
    void main()
    {
    
    	object oPC = GetLastOpenedBy();
    	
    	if ( !GetIsPC(oPC) ) { return; }
    	
    	if ( GetItemPossessedBy(oPC, "mg_key1") == OBJECT_INVALID) { return; }
    	
    	object oPartyM = GetFirstFactionMember(oPC, FALSE);
    	int rnum =  Random(4) +1;  // yields 1 to 4, or do you want the same randomly generated number on every party member?  if so put this line inside the loop as the first line 
    	//We stop when there are no more valid NPC's in the party.
    	while( GetIsObjectValid(oPartyM) )
    	{
    		SetLocalInt(oPartyM,"randomport", rnum);
    		oPartyM = GetNextFactionMember(oPC, FALSE);
    	}
    	
    	object oPartyMember = GetFirstFactionMember(oPC, TRUE);
    	// We stop when there are no more valid PC's in the party.
    	while(GetIsObjectValid(oPartyMember) == TRUE)
    	{
    	        // Do something to party member
    	        SetLocalInt(oPartyM,"randomport", rnum);
    	        // Get the next PC member of oPC's faction.
    	        // If we put anything but oPC into this, it may be a totally
    	        // unreliable loop!
    	        oPartyMember = GetNextFactionMember(oPC, TRUE);
    	}
    
    }
    



  • Knight_ShieldKnight_Shield Member Posts: 51
    Thanks...I will give it a go. :)
Sign In or Register to comment.