fasad 1 Posted April 29, 2007 G'day, I'm a little confused as to the purpose of the private command. Isn't a variable with a name starting with underscore "private"? What purpose is there in declaring _variables private at the beginning of a script file? Cheers, fasad Share this post Link to post Share on other sites
Big Dawg KS 6 Posted April 29, 2007 G'day, I'm a little confused as to the purpose of the private command. Isn't a variable with a name starting with underscore "private"? What purpose is there in declaring _variables private at the beginning of a script file? Cheers, Â fasad It's used in functions, so that the variables in the function don't effect similiarly named variables in the calling script/function (I think so at least). Share this post Link to post Share on other sites
Op4 BuhBye 0 Posted April 29, 2007 Im not sure about this command but I did read in the wiki that ALL variables "local, global and public" are broadcast to all machines. Maybe this stops that. Share this post Link to post Share on other sites
Big Dawg KS 6 Posted April 29, 2007 Im not sure about this command but I did read in the wiki that ALL variables "local, global and public" are broadcast to all machines. Maybe this stops that. No, it doesn't. I'm pretty sure it's for when you do something like... <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_man = _this; _function = compile "private[""_pos"",""_man""];_pos=_this;_man=nearestObject[_pos,""SoldierWB""];_man"; _man2 = (getpos _man) call _function; _man domove (getpos _man2); hint "_man != _man2"; So the _man inside _function doesn't interfere with the _man outside of it. Share this post Link to post Share on other sites
fasad 1 Posted April 29, 2007 Okay, thanks KyleSarnik, that makes sense. I had assumed that called code happened in it's own scope/space, but I think I can understand why call doesn't... I guess forEach would be similar? Is private needed for any other "different scope within the same script file" commands apart from call? (for,do, then etc)? If I'm not in the habit of declaring identically named variables, do I need to use private? I've seen many MP scripts that start with private [<every single _Variable>]. Is it just best practice to do so even if the script will be exec(VM)'d or spawned? Share this post Link to post Share on other sites
raedor 8 Posted April 29, 2007 If you call code (be it via call, forEach or whatever) and init a new var there, it won't be declared in the outer scope as well. If you're using a var that is already defined in the outer scope, it will usually overwrite it. To prevent that, there's the command private. _x in forEach etc. is handled in another way, it is not affected by the outer scope and on the other hand it won't affect the outer scope (so if you have a {_x ...} count in a {_x...} forEach it should work as you need it ) You can use private whereever you need it (not only "functions"). Share this post Link to post Share on other sites