Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → GetNumPlayers() Not Working
GetNumPlayers() Not Working
2004-12-16, 8:20 PM #1
Look at this code, if you would. When the code is executed, it prints "Number is...", but doesn't print the number of players, which should be x. It seems like GetNumPlayers() isn't working. And I notice that it's missing from the JK Specs (Millennium is down).

I need this code to make sure that no players are still in the sector before executing another piece of code. But it always thinks a player is still in the sector, even if no one is.

Code:
exited:
	print("Someone exited sector.");
	// Make sure there are no other players in the sector
	for (x=0; x<GetNumPlayers(); x=x+1) {
		print("Number is...");
		print(x);
		print("Checking players' sectors...");
		if (GetThingSector(GetPlayerThing(x)) == theSector) {
			print("Someone is still in the sector.  Not resetting.");
			return;
		}
	}
	print("No one else in the sector.  Resetting.");
KOP_blujay
Just dancin'...and singin'...in the Force.
2004-12-16, 8:53 PM #2
print(x); won't work. PrintInt(x) will print the index number of the current player (if there's 1 player, it'll print 0). If you had two players, neither in the important sector, and if you remove the line print("No one else in the sector. Resetting.");, you'd see:
0
Checking players' sectors...
Number is...
1
Checking players' sectors...

If you want the number printed to be the number of players, use
PrintInt(GetNumPlayers());

GetNumPlayers() definately works.

QM
2004-12-16, 9:18 PM #3
Ok, thanks, QM.

Aside from the debugging messages, though, do you have any idea why the code isn't functioning like it should?

I just tried this other method, but this method always thinks that whenever a player leaves, there are no other players in the sector, even if someone is still in there.

Code:
// Make sure there are no other players in the sector
	for(i=FirstThingInSector(doctorSector), j=0; j<GetSectorThingCount(doctorSector); i=NextThingInSector(doctorSector, i), j=j+1) {
		if(GetThingType(i) == 10) {
			print("Player still in sector.  Not resetting.");
			return;
		}
	}
KOP_blujay
Just dancin'...and singin'...in the Force.
2004-12-16, 9:50 PM #4
Hm, I dunno why the first code wouldn't be working. The second set of code has a misused verb.
i=NextThingInSector(doctorSector, i)
should be
i=NextThingInSector(i)

Based on that misused verb I'd think the code would always think that a player IS in the sector though... try fixing that verb and let me know if that fixes it or not. Or correct me if I'm wrong about the verb, but according to the DataMaster, the way I showed is proper use.

QM
2004-12-16, 9:53 PM #5
The DataMaster? What is that?

The JK Specs say this:

Quote:
NextThingInSector() Info

Gets the next thing in sector. After FirstThingInSector finds something. Also see PrevThingInSector.

Use: Next = NextThingInSector(sector, previous thing); Thing should be the last thing found or the return value of FirstThingInSector()
KOP_blujay
Just dancin'...and singin'...in the Force.
2004-12-16, 9:58 PM #6
The DataMaster was made by SaberMaster, it's like the JK specs but just for COG and way more complete and accurate. I'll see if I can find a link. It says:
Quote:
Returns the thing that has the next lowest thing number than the given thing and is in the same sector.
Syntax:

thing=NextThingInSector(thing);


QM

Edit: http://www.geocities.com/sabersdomain/
2004-12-16, 10:03 PM #7
well..... im no cog master but.....
WAIT! i am a cog master! Muhahaha!

how about placeing a ghost in the top corner of the room (by the glass) and useing:
Code:
if(FirstThingInView(ghostcamera, 180, 25, 0x404) != 0) print("player detected, not resetting");
I am Darth PJB!
well, go on, run away!

i have a plastic lightsaber and a jedi cape.. am i a nerd?

If gravity is a crule mistress, and bar tenders with bad grammar are untrustworthy, what is air?
2004-12-16, 10:14 PM #8
Because that wouldn't work.
FirstThingInView returns -1 when it doesn't detect anything.

COG master my butt. =D

QM
2004-12-16, 10:22 PM #9
Ok, QM, I changed the verb to the way you described, but it still isn't working. It still always thinks the sector is empty of players when any player exits the sector. I don't know whether to believe the DataMaster specs or the JK Specs. :) But either way, this code isn't working:

Code:
exited:
	print("Someone exited sector.");

	// Make sure there are no other players in the sector
	for(i=FirstThingInSector(doctorSector), j=0; j<GetSectorThingCount(doctorSector); i=NextThingInSector(i), j=j+1) {
		if(GetThingType(i) == 10) {
			print("Player still in sector.  Not resetting.");
			return;
		}
	}

	print("No players left in sector.  Resetting.");


It doesn't seem like it should be this hard! *sigh*

Any more ideas? Thanks for your help, BTW.
KOP_blujay
Just dancin'...and singin'...in the Force.
2004-12-16, 10:22 PM #10
For the record, there is a non-player thing in the sector.
KOP_blujay
Just dancin'...and singin'...in the Force.
2004-12-17, 4:02 PM #11
Probably doing the LEC way will do.
Here we go, cogging after 3 years of blank ;)

Code:
theSector = GetSenderRef();
playersInside = 0;

jkClearString();
jkStringConcatAsciiString("In sector ");

jkStringConcatInt(theSector);
jkStringOutput();

stuffInside = FirstThingInSector(theSector);

while(stuffInside != -1)
{
   if(GetThingType(stuffInside) == 10)
   {
      jkClearString();
      jkStringConcatAsciiString("Player thing ");

      jkStringConcatInt(stuffInside);
      jkStringConcatAsciiString(" exists");

      jkStringOutput();
      playersInside = 1;
   }

   stuffInside = NextThingInSector(theSector);
}

if(!playersInside) print("There were no players");


It's questionable that why NextThingInSector requires parameters while NextThingInView does not. Maybe you want to get rid of the parameter in NextThingInSector if it won't work.

Yours shows, putting the 'thing' value in parameter, but I guess you need to put the sector value, though jkspec tells to put 2! instead of 1 or 0... don't know sure.

Also, your way getting the sector thing count at the start of the loop, though very unlikely to happen, will not count things that entered after the loop had started, so it's probably better using 'while' loop.

And also, is there a chance that 'TypeFlag' ever gets flagged with multiple values? If that happens, maybe using '&' with 0x400 (as shown in jkspec) instead of '== 10' would do better.
2004-12-17, 10:16 PM #12
I tested the code in-game and it worked flawlessly for me. My best guess is you don't have sector mask flags set properly.

sector doctorSector mask=0x400

should work. This

sector doctorSector mask=0xFFF

would be good for debugging purposes because anytime a projectile leaves the sector, it'd trip the exited message.

Let me know if this isn't the problem, and I'll keep brainstorming.

I know this solutions eems obvious but I figured it was as good a guess as any.

QM

P.S. - By "the code" I mean your code that uses GetSectorThingCount.

P.P.S. - I'd have replied sooner but the forums were messed up.
2004-12-18, 1:44 PM #13
What code did u test in game and it worked? there are so many codes on this thred i think my head is gonna explode.
||||||||||||||||||||
2004-12-19, 9:42 PM #14
Hideki,

I tried yours, but when I exit the sector, it says that player thing 0 exists in the sector. I tried a lot of stuff to make it check whether stuffInside was equal to -1 or 0, but all I could manage to do is get JK to go into an infinite loop and freeze.

I don't understand why this is so hard! I'm going to post the whole cog. Maybe someone can figure out why it isn't working. :(

Code:
# Jedi Knight Cog
# ap_evildr_server.cog
# By KOP_blujay
# For Spiral's Haven level
# December 9, 2004
# ========================================================================================
# Credits:
# LEC: m2_ffieldswitch.cog used as a reference, and for JK of course.  :)
# Code-Alliance: for Jed, JK Specs, etc.  :)  [url]www.code-alliance.com[/url]
# The Massassi Temple <[url]www.massassi.net[/url]>
# ========================================================================================

symbols

flex damageAmount=30
flex fadeOut=0
flex maxDist=10
flex minDist=0 local
flex volume=50

sector theSector mask=0x400

sound damageSound

thing doctor
thing soundThing

message entered
message exited
message pulse
message timer

message startup

end

# ========================================================================================

code

startup:
	sleep(5);
	print("evildr_server Build # 3");

	return;

entered:
	// When the player enters the sector, send the start trigger and start the timer
	SendTrigger (-1, 510000, 1, 0, 0, 0);
	setTimerEx(30, 1, 0, 0);

	return;

pulse:
	for (x=0; x<GetNumPlayers(); x=x+1) {
		if (GetThingSector(GetPlayerThing(x)) == theSector) {
			damageThing(getPlayerThing(x), damageAmount, 0x2, doctor);
			si = PlaySoundThing(damageSound, soundThing, volume, minDist, maxDist, 0);
			sleep(getSoundLen(damageSound));
			stopSound(si, fadeOut);
		}
	}

	return;

timer:
	// When the player's been in the sector for 30 seconds, send the stage-2 trigger
	SendTrigger (-1, 510000, 2, 0, 0, 0);

	// Wait 3 seconds before doing damage
	sleep(3);

	// Sector damage pulse
	setPulse(1);

	return;

exited:
	
	//theSector = GetSenderRef();
	playersInside = 0;

	jkClearString();
	jkStringConcatAsciiString("In sector ");

	jkStringConcatInt(theSector);
	jkStringOutput();

	stuffInside = FirstThingInSector(theSector);

	while(stuffInside != 0)
	{
	   if(GetThingType(stuffInside) == 10)
	   {
	      jkClearString();
	      jkStringConcatAsciiString("Player thing ");
	
	      jkStringConcatInt(stuffInside);
	      jkStringConcatAsciiString(" exists");
	
	      jkStringOutput();
	      playersInside = 1;

	      return;
	   }

	   stuffInside = NextThingInSector(theSector);
	}
	
	if(!playersInside) print("There were no players");

	// When all players exit the sector, kill the timer and send the reset trigger
	killTimerEx(1);
	setPulse(0);  // Stop sector damage
	SendTrigger (-1, 510000, 3, 0, 0, 0);

	return;

end
KOP_blujay
Just dancin'...and singin'...in the Force.
2004-12-20, 4:57 AM #15
The reason it indefinately loops is that in your while loop, it has if(stuffInside != 0). When NextThingInSector does not find any more thing in that sector it should return -1 and not 0, so since it never returns 0, it hangs or if the 0 ID thing (which I presume the host's player) is found, it stops the looping and prints out 'Player thing 0 exists'.

As for some 'sleep's, you really shouldn't use sleep commands, because when it is in effect, all the other messeges stops arriving, exited and entered as well, so using SetTimerEx is probably the better way, if I remember correctly.

I'm assuming you're trying to damage players being inside the sector for over 33 seconds and stop it when he leaves it in this cog.

And since you're setting the sleep for the ammount of the sound played, then shutting down the sound, stopSound doesn't seem to be neccesary though, but I just left it there in case you got some other use.

Figured that counting the number of players made it in much smaller code :)
I just rewrote some parts, sorry for changing it quite a bit.
Code:
code

startup:

	soundLength = getSoundLen(damageSound);

	sleep(5);
	print("evildr_server Build # 3");

	return;

entered:

	// When the player enters the sector, send the start trigger and start the timer
	SendTrigger (-1, 510000, 1, 0, 0, 0);

	// Set timer with entering thing as sender ID and mode as parameter 0
	setTimerEx(30, GetSourceRef(), 1, 0);

	numPlayers = numPlayers + 1; //Increase player count

	return;

timer:

	playerID = GetSenderID();

	if(GetParam(0) == 1)
	{
		// When the player's been in the sector for 30 seconds, send the stage-2 trigger
		SendTrigger (-1, 510000, 2, 0, 0, 0);

		// Wait 3 seconds before doing damage
		SetTimerEx(3, playerID, 2, -1);
	}
	else if(GetParam(0) == 2)
	{
		soundPlayed = GetParam(1);

		if(soundPlayed != -1) stopSound(soundPlayed , fadeOut);
		si = PlaySoundThing(damageSound, soundThing, volume, minDist, maxDist, 0);

		damageThing(playerID, damageAmount, 0x2, doctor);
		SetTimerEx(soundLength, playerID, 2, si);
	}

	return;

exited:

	// When the player exits the sector, kill the timer for his ID
	killTimerEx(GetSourceRef());

	numPlayers = numPlayers - 1; //Lower the player count

	// When all players exit the sector, send the reset trigger
	if(numPlayers == 0)
	{
		print "There are no more players left";
		SendTrigger (-1, 510000, 3, 0, 0, 0);
	}
	else
	{
		printInt(numPlayers);
		print("number of players still left inside");
	}

	return;


Just thought, using timer instead of pulse was better, since cog have no good way of handling arrays in smart way and do it in pulse.
2004-12-20, 5:09 PM #16
Hideki,

Wow, you sure did a lot of work on that. I thank you for it, but I have to also apologize for not using it exactly. It wasn't quite what I needed. But I did get ideas from yours, and finally, the cog works perfectly. There is one thing that I could do to make it technically better, but Spiral and I decided that it would be rare to encounter the problem, so left it alone. (The problem would be if two players were in the sector, and one of them was already dead, and then a third player entered the sector at some point...it gets really complicated...so we decided this was good enough.)

So here's the final version that works, in case this might help someone someday:

Code:
# Jedi Knight Cog
# ap_evildr.cog
# By KOP_blujay
# For Spiral's Haven level
# December 9-December 20, 2004
# ========================================================================================
# Credits:
# LEC: m2_ffieldswitch.cog used as a reference, and for JK of course.  :)
# Code-Alliance: for Jed, JK Specs, etc.  :)  [url]www.code-alliance.com[/url]
# The Massassi Temple <[url]www.massassi.net[/url]>
# ========================================================================================

symbols

flex damageAmount=30
flex fadeOut=0
flex maxDist=10
flex minDist=0 local
flex volume=50

sector theSector

sound damageSound

thing doctor
thing soundThing

message startup
message entered
message exited
message pulse
message timer

end

# ========================================================================================

code

startup:
	
	numPlayers = 0;

	stageOneActive = 0;
	stageTwoActive = 0;

	soundPlaying = 0;

	return;

entered:

	if (getSenderRef() != theSector) {
		
		return;
	}

	// When a player enters the sector
	numPlayers = numPlayers + 1;

	// Kill the stop-timer
	killTimerEx(3);

	// If stage-one or stage-two are already counting down, don't do anything
	if (stageOneActive == 0) {
		if (stageTwoActive == 0) {

			stageOneActive = 1;

			timerLength = rand() * 20;

			if (timerLength < 10) timerLength = 10;

			// Send start trigger
			sendTrigger (-1, 510000, 1, 0, 0, 0);

			// Start stage-two timer
			setTimerEx(timerLength, 1, 0, 0);
		}
	}

	return;

pulse:
	for (x=0; x<GetNumPlayers(); x=x+1) {
		if (GetThingSector(GetPlayerThing(x)) == theSector) {
			damageThing(getPlayerThing(x), damageAmount, 0x2, doctor);

			if (soundPlaying == 0) {
				soundPlaying = 1;
				
				si = PlaySoundThing(damageSound, soundThing, volume, minDist, maxDist, 0);

				setTimerEx(getSoundLen(damageSound), 4, 0, 0);
			}

			if (getThingHealth(getPlayerThing(x)) < 1) {
				call diedOrExited;
			}
		}
	}

	return;

timer:

	// Stage two timer
	if (getSenderID() == 1) {

		stageTwoActive = 1;
		stageOneActive = 0;

		// Send the stage-2 trigger
		SendTrigger (-1, 510000, 2, 0, 0, 0);

		// Set a timer for damage to start
		setTimerEx(4, 2, 0, 0);
	}
	else if (getSenderID() == 2) {

		// Sector damage pulse
		setPulse(.25);
	}
	else if (getSenderID() == 3) {

		// Stop sector damage
		setPulse(0);

		// Send reset trigger
		SendTrigger (-1, 510000, 3, 0, 0, 0);

		stageTwoActive = 0;
		stageOneActive = 0;
	}
	else if (getSenderID() == 4) {
		stopSound(si, fadeOut);

		soundPlaying = 0;
	}

	return;

exited:
	call diedOrExited;

	return;

diedOrExited:
	// When a player exits the sector

	numPlayers = numPlayers - 1;

	// Don't keep resetting while the pulse is running after someone dies
	if (numPlayers < 0) {
		numPlayers = 0;
		return;`
	}

	// If no players are in the sector
	if (numPlayers == 0) {

		// Stop stage-two timer
		killTimerEx(1);
		stageOneActive = 0;  // Say that stage-one is not active...
// even though flashing may...
// still be happening until timer ID 3 runs

		// Stop damage
		killTimerEx(2);

		// Set 5-second timer to stop everything
		setTimerEx(5, 3, 0, 0);
	}

	return;

end
KOP_blujay
Just dancin'...and singin'...in the Force.

↑ Up to the top!