Finding player with highest value in array?

Im making a mode where players capture checkpoints.

lets say, when reaching 20 Cp’s, the count resets and start from zero again.

However, every player has a player variable called “CurrentCP” which adds 1 value for each cp thats collected.

Im making a weapon “orbital laser” thats is supposed to target the player with most CP count, and ONLY that player.

I made the actions ready, effects and stuff, but need help with the condition to find the right player.


Also, i wounder, is there a way to foce players that joined mid game, to enter spectator mode automaticly when a variable is true? like “GameStarted variable”?

I think you can do this with a simple “Is True for All” condition, and if you have a PLAYER variable that holds the CP count of each player in a match, Like this condition will compare the CP counts of all the players versus a certain player and checks if it’s true. I haven’t tested it yet and i think i might be wrong with this but the event can be “Ongoing - Each Player, for all teams and all players”, and the format for the “Is True for All” condition would be

Is True for all,
All living players,
Team: All,
Condition: Compare
Player Variable
of Current Array Element
(The variable that holds the CP Counts of a player)
<
Player Variable
of Event Player
(The variable that holds the CP counts of a player)
== True

With this condition, It will determine if which player has the greater amount out of all players, This is a rough solution since I haven’t fully mastered workshop but i hope it works, I tried my best! and tell me if it doesn’t

Also for your second question, I don’t think that’s possible yet in workshop, unless you move the players manually to spectator :0 goodluck on it!

You can sort any arrays to any weighting you want with “sorted array”. In your case, it would look like this:

sorted array(all players(all teams), player variable(current array element, CurrentCP))

The array that returns is now sorted in the order of the player variable CurrentCP of all players, with the lowest one being the first and the highest being the last.

So to get the player with the highest score you use:

last of(sorted array(all players(all teams), player variable(current array element, CurrentCP)))

No. But a workaround could be this idea:

  • disable game mode respawning
  • if a player joins and the “GameStarted” variable is false you set his player variable “starter” to 1
  • you kill all players as soon as they have initially spawned “has spawned(event player) == true”
  • in a “player died” rule you respawn the player with the ressurect or respawn action only if their player variable “starter” is 1

So players who will join later are permanently dead, wich means they can at least spectate their teammates.

1 Like

Alright thanks both of you!:slight_smile:
I’m still a bit confused here how to set this up, honestly.

So i have a Rule that checks if variable “using laser” is true.
When that rule is true, it will execute a damage over time, an effect, and then set itself to false again “using laser = false”

Using laser will become true for the event player that picks up an effect in a unique vector, or just make it simple, Pressing Right click.

Now how would i make it so that the player that have the using laser variable enabled is the attacker, and the rule with the effects targets another player?

here is a messy example:

rule(“Laser”)
{
event
{
Ongoing - Each Player;
All;
All;
}

conditions
{
	Event Player.UsingLASER == True;
	Last Of(Sorted Array(All Living Players(All Teams), Current Array Element)) == Event Player.CurrentCPLOC;
}

actions
{
	Start Damage Over Time(Event Player, Null, 4, 15);
	Small Message(Event Player, Custom String("Atacked by Orbital Laser"));
	Create Beam Effect(All Players(All Teams), Bad Beam, Event Player, Vector(0, 1500, 0), Red, Visible To Position and Radius);
	Play Effect(Event Player, Buff Explosion Sound, White, Event Player, 150);
	Set Status(Event Player, Null, Stunned, 4);
}

}

Could this rule be used or wont work?:

conditions
{
Event Player == Last Of(Sorted Array(All Living Players(All Teams).CurrentCPLOC, Current Array Element));
}

So the first thing: I think you still don’t understand how “sorted array” works.
Sorted array takes 2 values. The first is the array that gets sorted (in my example it is “all players”). The second value is the rating that each element gets: The array elements get associated with some number and then sorted in the order of those numbers. In my example the players get sorted by their variable “CurrentCPLoc”, while “current array element” is just a reference to the elements of the array that gets sorted.

I would suggest you to put the stuff into the rule where you set the variable to true. But if you prefer this way, its ok aswell.

Not needed as condition (not to mention its wrong), so remove it.

Here is how I would do it and what should work:

- set global variable(targetPlayer, last of(sorted array(all players(all teams), player variable(current array element, CurrentCPLOC))))
- start damage over time(global variable(targetPlayer), ...)
- small message(global variable(targetPlayer), ...)
- create beam effect(..., ..., event player, add(event player, vector(0, 1500, 0)), ..., ...)
- play effect(...)
- set status(global variable(targetPlayer), ...)

As you can see, the idea is to set a temporary global variable to the player with the highest current score (or the highest “CurrentCPLOC”) so you can reference this variable whenever you want to do something with this player.

Superb! got it working thanks alot!
Now i just tried it alone and it works on myself, i checked inspector too to make sure it tracked me as “leader” which also worked.

Just gotta try it with more players.

i have a problem with if like more players are sharing the same amount of CP’s,
i dont really mind the laser hitting everyone in the lead, though. just wounder if the variable which tracks the player(s) with most cp’s, can store multiple values, lets say 3 players together, or if it will pick one randomly?

Global.Leader = Last Of(Sorted Array(All Players(All Teams), Current Array Element.CurrentCPLOC));

This needs a 2 actions. Instead of this:

Do this:

Global.Leader = Last Of(Sorted Array(All Players(All Teams), Current Array Element.CurrentCPLOC)).CurrentCPLOC;
Global.Leader = Filtered Array(All Players(All Teams), Current Array Element.CurrentCPLOC == Global.Leader)

The “==” is the “compare” action. Global.Leader now contains an array of the players with the highest CurrentCPLOC. This means that you need to execute the damage/message/beam actions multiple times aswell.
In the “start damage over time”, “small message” and “set status” actions you can just input the Global.Leader, as they are working with arrays.
The “create beam effect” and “play effect” need to be executed n times though (n = length of the array). You can do this with a for-loop over the Global.Leader array. Keep in mind that you have to set the reevaluation of the beam effect to “visible to” in this case.