geekm0nkey

Proper use of Call within a While do

5 posts in this topic

Ok, need to know if this can be done,or is even proper to do... (FYI, don't want multiple files... so bare with me)

//code for the call

["stackname", "onPlayerConnected", {[_id,_name,_uid] call FN_mycall}] call BIS_fnc_addStackedEventHandler;

while {true} do
{
  {
  	// do work!
  } forEach allPlayers;

  FN_mycall = {
    _id = _this select 0;
    _name = _this select 1;
    _uid = _this select 2;
  	// do something here
  };
  FN_myothercall = {
    // do something else
  };
};

 

Edited by geekm0nkey

Share this post


Link to post
Share on other sites

The code you posted does the following:

  1. Creates an onPlayerConnected EH with stackname and will call FN_mycall with those parameters. 
  2. Infinitely do the following:
    1. Loop through allPlayers
    2. Assign some code to a global variable named FN_mycall
    3. Assign some code to a global variable named FN_myothercall

I am confused on exactly what you are trying to do, but based on the title of the topic, all you want to do is call a function from a loop?

// Always define functions before you plan on using them.
FN_mycall = 
{
    _id = _this select 0;
    _name = _this select 1;
    _uid = _this select 2;
  	// do something here
};

FN_myothercall = 
{
    // do something else
};

// Make sure to change "stackname" to something a little more... unique. 
["stackname", "onPlayerConnected", {[_id,_name,_uid] call FN_mycall}] call BIS_fnc_addStackedEventHandler;

// In an unscheduled environment, this will only loop 10,000 times before exiting. 
// In a scheduled environment, this will loop infinitely.
while {true} do
{
	{
  		// do work!
  	} 
	forEach allPlayers;

	// You cannot call FN_mycall from here unless you define it's parameters

	// Call function FN_myothercall
	call FN_myothercall;
};

 

  • Like 1

Share this post


Link to post
Share on other sites
Advertisement

Cool exactly what I wanted to know. Yes, my example was VERY simplistic just to get the idea across, the main while do loop is on a 5 minute uisleep delay between loops.  Now you mentioned that my function variables are global is that because i didn't precede them with '_' such as _FN_myfunction or because i didn't do a private["","",""]; line defining all the variables? in the grand scheme of things is that bad? its simple enough to rename them or declare them. I'm pretty sure I want to contain them with in the scripts scope.

Edit: purpose... perform the actions within the loop, but also look for the onconnected event and do that while the loop is asleep.

 

And thank you.

Edited by geekm0nkey

Share this post


Link to post
Share on other sites

Yes, they are global because you did not have an underscore in front of the variable name. 

GlobalVariable
_privateVariable
private _localVariable

Would be comparable to:

Public, Class level variable
Private, Class level variable
Local Method variable

If you work in other programming languages. 

There are a few differences between _privateVariable and private _localVariable. The one you will run into is that if you have a bunch of inline functions defined, like you do in your examples, you will end up sharing variables across those functions; essentially those variables being used at the script level instead of locally. Using private or private ["_localVariable"], will ensure that the variable is local to that scope. 
https://community.bistudio.com/wiki/private

If you do not plan on using a function outside a script, I suggest declaring them as private local variables. If you plan on using them outside this script, you will have to define them as global. Especially in the case of the EH, as that FN_mycall will be called at a later date outside the scope of your script. 


Also, if you are feeling adventurous, I'd suggest looking into the threading system within Exile instead of using while loops. This is not required though. 
You can do a search across the files for examples on how to use it. 
ExileClient_system_thread_addTask
ExileServer_system_thread_addTask 

Share this post


Link to post
Share on other sites

Again thank you for the information.. Here is a copy of the actual script i'm working with. Any improvements will be appreciated. Its an updated version of the pvpmarkers script i released earlier.. I needed to add the ability to capture players who connect and disconnect faster than the 5 minute sleep allowed.. So the ideal flow of this script is as follows.. I am certain this needs optimizing and correction.

  1. launch
  2. check for any players, create markers as defined in script
  3. sleep
  4. while sleeping have the ability to add or remove players markers if new player connects/disconnects
  5. on loop, update existing markers as defined.
  6. repeat.

Update: I updated the code below, I read up on the use of private declares. I think I have it right? first two are for the entire script, the ones for each function and loop have been declared to prevent bleed over from the other areas of the script? correct. The function variables are not declared as I need them global for the EH to be able to reference them.. Are there any security concerns i should have?

All has been resolved, and moved to the release page. Thank you for the guidance..

 

Edited by geekm0nkey

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.