A syntax error that I can't see whats causing it.

Have a question about modding, modelling or skinning? Have a tutorial to post? Post here!

Moderator: Core Staff

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

A syntax error that I can't see whats causing it.

Post by F |Madness| U » June 19th, 2011, 10:08 pm

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!
-

User avatar
iCYsoldier
CJ Worshipper
CJ Worshipper
Posts: 289
Joined: December 5th, 2009, 7:12 am
Location: Australia

Re: A syntax error that I can't see whats causing it.

Post by iCYsoldier » June 19th, 2011, 10:20 pm

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.

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: A syntax error that I can't see whats causing it.

Post by F |Madness| U » June 19th, 2011, 10:35 pm

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 :P

Edit: Infact I missed out many } lol, I must be tired or something :S
-

User avatar
iCYsoldier
CJ Worshipper
CJ Worshipper
Posts: 289
Joined: December 5th, 2009, 7:12 am
Location: Australia

Re: A syntax error that I can't see whats causing it.

Post by iCYsoldier » June 19th, 2011, 10:40 pm

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 :P I just looked at it again.

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: A syntax error that I can't see whats causing it.

Post by F |Madness| U » June 19th, 2011, 10:56 pm

Haha, well it works great now anyway so thanks for help :)
-

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: A syntax error that I can't see whats causing it.

Post by Drofder2004 » June 19th, 2011, 11:44 pm

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.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: A syntax error that I can't see whats causing it.

Post by F |Madness| U » June 20th, 2011, 1:02 am

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?
-

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: A syntax error that I can't see whats causing it.

Post by Drofder2004 » June 20th, 2011, 3:15 am

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.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: A syntax error that I can't see whats causing it.

Post by F |Madness| U » June 20th, 2011, 6:03 pm

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?
-

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: A syntax error that I can't see whats causing it.

Post by IzNoGoD » June 20th, 2011, 8:02 pm

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
LMGTFY!

Its not a glitch... Its the future!

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: A syntax error that I can't see whats causing it.

Post by F |Madness| U » June 20th, 2011, 11:15 pm

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")).
-

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: A syntax error that I can't see whats causing it.

Post by Drofder2004 » June 20th, 2011, 11:57 pm

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.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: A syntax error that I can't see whats causing it.

Post by IzNoGoD » June 21st, 2011, 12:13 am

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
LMGTFY!

Its not a glitch... Its the future!

F |Madness| U
CJ G0D!
CJ G0D!
Posts: 1575
Joined: June 3rd, 2009, 9:02 pm
Location: Cardiff University, UK

Re: A syntax error that I can't see whats causing it.

Post by F |Madness| U » June 21st, 2011, 6:04 pm

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.
-

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: A syntax error that I can't see whats causing it.

Post by IzNoGoD » June 21st, 2011, 7:06 pm

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)
LMGTFY!

Its not a glitch... Its the future!

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests