Page 1 of 3
A syntax error that I can't see whats causing it.
Posted: June 19th, 2011, 10:08 pm
by F |Madness| U
Trying to create a HUD element to show the number of people on each team.
Code: Select all
init()
{
.......
level thread huntersAlive();
}
Code: Select all
huntersAlive()
{
    zAlive = NewHudElem();
    zAlive.x = 0;
    zAlive.y = 0;
    zAlive.elemtype = "OBJECTIVE";
    zAlive.alpha = 1;
    zAlive.fontscale = 1.6;
    zAlive.foreground = 1;
    zAlive.color = (1, 0.4, 0.4)
    level thread CountHunters(axis);// <--------- this should be inside the for(;;), but when it is here it indicated that the syntax is with this line
   Â
    for(;;)
    {
        wait 1;
      Â
        zAlive SetText("Hunters: " + a)
    }
}
Code: Select all
CountHunters(team)
{
    a = 0;
    for(i = 0; i < level.players.size; i++)
        if(level.players[i].pers["team"] == team)
            a++;
    return a;
}
When trying to run I'm receiving a bad syntax with level thread CountHunters(axis);.
Any help would be appreciated!
Re: A syntax error that I can't see whats causing it.
Posted: June 19th, 2011, 10:20 pm
by iCYsoldier
It should be something like:
Code: Select all
for(;;)
{
   wait 1;
   a = CountHunters("axis"); // Sets the amount of players to variable 'a'
   zAlive SetText("Hunters: " + a) // Prints it out
}
Because the 'CountHunters' function returns a value, you should set the return value to a variable, in this case 'a'. Also 'axis' should be in quotes ("axis") as it is a string, not a variable.
Re: A syntax error that I can't see whats causing it.
Posted: June 19th, 2011, 10:35 pm
by F |Madness| U
Arghhh I'm so stupid, one of the syntax was simply because I missed a ; (if you look back in my code, the zAlive.color line).
But also you're right about the function returning a value, how it should be a variable such as a. I still have so much to learn about scripting
Edit: Infact I missed out many } lol, I must be tired or something :S
Re: A syntax error that I can't see whats causing it.
Posted: June 19th, 2011, 10:40 pm
by iCYsoldier
F |Madness| U wrote:Arghhh I'm so stupid, one of the syntax was simply because I missed a ;
Haha don't worry, I missed that too

I just looked at it again.
Re: A syntax error that I can't see whats causing it.
Posted: June 19th, 2011, 10:56 pm
by F |Madness| U
Haha, well it works great now anyway so thanks for help

Re: A syntax error that I can't see whats causing it.
Posted: June 19th, 2011, 11:44 pm
by Drofder2004
Also, for the sake of a little server stress relief, you could setup a "waittill ("death")" style function and then you would only need to loop once per death and not per second...
I cba to code it atm and tbh it really isn't that hard to setup yourself.
Re: A syntax error that I can't see whats causing it.
Posted: June 20th, 2011, 1:02 am
by F |Madness| U
I may do that, but I would also like it to count as people join the game, not just once the teams change. Is there anyway to do waittill("death" || "spawn") for example?
Re: A syntax error that I can't see whats causing it.
Posted: June 20th, 2011, 3:15 am
by Drofder2004
Just create your own "notify" and add it to any function you want.
i.e
level notify("update_a");
add that in any function, such as player death, player spawns, etc...
Then you can simply use "level waittill("update_a");" in a loop.
Re: A syntax error that I can't see whats causing it.
Posted: June 20th, 2011, 6:03 pm
by F |Madness| U
Ah nice, that will come in handy for me, thanks. Just wondering, on the issue of server stress:
-would an infinite loop (on every player) looping every second, cause considerably more server stress than an infinite loop (on the level) looping every second?
-would an infinite loop (on the level) looping every 3 seconds or so cause much server stress (looping from start to end of game)?
Edit: Also do you know if there is a built in function (for cod4 atleast) to get the person with the 5th highest score, or lowest score, etc?
Re: A syntax error that I can't see whats causing it.
Posted: June 20th, 2011, 8:02 pm
by IzNoGoD
cba to edit the function i wrote for this:
Code: Select all
rank_players(number)
{
players=getentarray("player","classname");
if(number>players.size)
number=players.size;
score=[];
//for(i=0;i<number;i++)
// score[i]=undefined;
for(j=0;j<number;j++)
{
for(i=0;i<players.size;i++)
{
if(!isdefined(score[j])||!isdefined(score[j].score)||players[i].score>score[j].score)
score[j]=players[i];
}
if(j!=number-1)
players=removefromarray(score[j],players);
}
return score;
}
removefromarray(ent,array)
{
if(!isdefined(ent))
return array;
temp=[];
for(i=0;i<array.size;i++)
{
if(ent!=array[i])
temp[temp.size]=array[i];
}
return temp;
}
Call it as:
Code: Select all
top_players=rank_players(5);
if(top_players.size>4)
player_five=top_players[4];
remember that top_players[0] will be the first, and top_players[1] will be the second player etc
Re: A syntax error that I can't see whats causing it.
Posted: June 20th, 2011, 11:15 pm
by F |Madness| U
Works great, thankyou IzNoGod! I have ALOT to learn about arrays, they're still very confusing for me =/
Also Drofder about your recommendation to use notifies and waittills rather than infinite loop, there is one which I cannot get to work. I can't seem to be able to notify ("update_a") when somebody disconnects.
I tried something like this:
Code: Select all
OnDisconnect()
{
self waittill("disconnect");
level notify("update_a");
}
However it says that `disconnect` is unknown or something like that (however in many other functions there are `self endon("disconnect")` but I have yet to see a self waittill("disconnect")).
Re: A syntax error that I can't see whats causing it.
Posted: June 20th, 2011, 11:57 pm
by Drofder2004
Code: Select all
onPlayerDisconnect()
{
self waittill("disconnect");
self notify("end_healthregen");
}
That is taken straight from the raw files.
It is also used to remove claymores from those who disconnect, so it definitly works.
Re: A syntax error that I can't see whats causing it.
Posted: June 21st, 2011, 12:13 am
by IzNoGoD
F |Madness| U wrote:Works great, thankyou IzNoGod! I have ALOT to learn about arrays, they're still very confusing for me =/
Also Drofder about your recommendation to use notifies and waittills rather than infinite loop, there is one which I cannot get to work. I can't seem to be able to notify ("update_a") when somebody disconnects.
I tried something like this:
Code: Select all
OnDisconnect()
{
    self waittill("disconnect");
    level notify("update_a");
}
However it says that `disconnect` is unknown or something like that (however in many other functions there are `self endon("disconnect")` but I have yet to see a self waittill("disconnect")).
Most likeley, self is not defined.
I wrote a tut about this:
http://modsonline.com/Forums-top-134675.html
Re: A syntax error that I can't see whats causing it.
Posted: June 21st, 2011, 6:04 pm
by F |Madness| U
Ye I've already read your tut a few times it helped me alot
But I definitely did define self, but I may have made an error somewhere else (the code I posted above wasn't my original code, just something along lines of code I used), i'll try again later anyway.
Also if one of you could answer this question about server stress I would be grateful:
-would an infinite loop (on every player) looping every second, cause considerably more server stress than an infinite loop (on the level) looping every second?
-would an infinite loop (on the level) looping every 5 seconds or so cause much server stress (looping from start to end of game)?
I understand that infinite loops inevitably cause server stress, but it would be interesting for me to know wether a long wait time in between (such as 5 seconds) causes much server stress.
Re: A syntax error that I can't see whats causing it.
Posted: June 21st, 2011, 7:06 pm
by IzNoGoD
Loops generally dont cause any serverstress, unless you are using a lot of vars+calculations
Then again, a distance() function is already a lot of stress, as is a bullettrace() function, and, as long as you dont use them, you cant cause any real server stress (maybe cod4 has some more commands im not aware of)