Jump to content
iV - Ghost

Delete value from array after selectRandom

Recommended Posts

Hi everyone,

 

I try to delete a value from a array after using selectRandom.

 

 

_task1 = {execVM "scripts\taskmanager\tasks\Girna.sqf";};
_task2 = {execVM "scripts\taskmanager\tasks\Mike-26.sqf";};
_task3 = {execVM "scripts\taskmanager\tasks\Airbase.sqf";};
_task4 = {execVM "scripts\taskmanager\tasks\Kamino.sqf";};
_task5 = {execVM "scripts\taskmanager\tasks\CampRogain.sqf";};
_task6 = {execVM "scripts\taskmanager\tasks\AgiaMarina.sqf";};



// SELECT RANDOM TASK
_randomTask = selectRandom [
    _task1,
    _task2,
    _task3,
    _task4,
    _task5,
    _task6
];



// DELETE TASK IF ASSIGNED
if (taskStateNumber_Girna > 0) then {
    _randomTask deleteAt (_randomTask find _task1);
};

if (taskStateNumber_Mike26 > 0) then {
    _randomTask deleteAt (_randomTask find _task2);
};

if (taskStateNumber_Airbase > 0) then {
    _randomTask deleteAt (_randomTask find _task3);
};

if (taskStateNumber_Kamino > 0) then {
    _randomTask deleteAt (_randomTask find _task4);
};

if (taskStateNumber_CampRogain > 0) then {
    _randomTask deleteAt (_randomTask find _task5);
};

if (taskStateNumber_AgiaMarina > 0) then {
    _randomTask deleteAt (_randomTask find _task6);
};

 

The taskStateNumber_xxx is given by loading the xxx.sqf.

But this will not work.

Share this post


Link to post
Share on other sites

Your _randomTask isn't  an array, just a task (or a script, to be exact).

Share this post


Link to post
Share on other sites

And is it possible to delete every _randomTask after he is get chosen?

Share this post


Link to post
Share on other sites

Use a forEach loop.

{
	// code
} forEach
[
	_task1,
	_task2,
	_task3,
	_task4,
	_task5,
	_task6
];

https://community.bistudio.com/wiki/removeSimpleTask

Why would you need to delete it? You can just set it as completed/cancelled/whatever.

https://community.bistudio.com/wiki/setTaskState

I personally use the BIS function for creating and handling tasks.

https://community.bistudio.com/wiki/BIS_fnc_taskCreate

If you are going to use the above and really want to delete it, use this:

https://community.bistudio.com/wiki/BIS_fnc_deleteTask

Share this post


Link to post
Share on other sites
// SELECT RANDOM TASK

_tasks = [_task1,_task2,_task3,_task4,_task5,_task6];
_randomTask = _tasks deleteAt floor random (count _tasks);

 

Share this post


Link to post
Share on other sites
1 minute ago, Lucullus said:

// SELECT RANDOM TASK

_tasks = [_task1,_task2,_task3,_task4,_task5,_task6];
_randomTask = _tasks deleteAt floor random (count _tasks);

 

I don't see the point in doing this, it won't actually delete the task, only from the array.

Share this post


Link to post
Share on other sites

Ah, misunderstood, I thought he wanted to erase it from the array.

Share this post


Link to post
Share on other sites

@Lucullus - He does, I think, but, I don't think he knows that it won't delete the task, maybe is misunderstood. Unless he wants a task not to be selected again?

Share this post


Link to post
Share on other sites

But if that is the case. Why not just select a random task and not add the others?

Share this post


Link to post
Share on other sites

The task will assigned and the taskState setted in the ..\tasks\XXX.sqf.

Everything I need is the function to delete the value (_task1,_task2,_task3,_task4,_task5,_task6) after one time selected.

The task should be only one time available in the array.

 

I will try this:


// SELECT RANDOM TASK
_tasks = [_task1,_task2,_task3,_task4,_task5,_task6];
_randomTask = _tasks deleteAt floor random (count _tasks);

 

Share this post


Link to post
Share on other sites

Tested but it doesn't delete the 1x selected _taskX.

Only give out a random value from _tasks.

I get nor error massage.

Share this post


Link to post
Share on other sites

Deleted. See below.

Share this post


Link to post
Share on other sites

EDIT: This works fine but I don't like it, feel like I have complicated it lol, I don't usually use deleteAt and it only takes number. Unless just removing usually but this command is almost 60x faster according to a comment on the Wiki.

[] spawn
{
	_task1 = "task1";
	_task2 = "task2";
	_task3 = "task3";
	_task4 = "task4";
	_task5 = "task5";
	_task6 = "task6";
	_tasks = [_task1, _task2, _task3, _task4, _task5, _task6];
	_randomTask = selectRandom _tasks;
	_taskToDelete = ((_tasks select {(_x isEqualTo _randomTask)}) select 0);
	hintSilent format ["Random selected task: %1 || Task to delete: %2", _randomTask, (call compile ((_taskToDelete splitString "task") select 0))];
	_tasks deleteAt ((call compile ((_taskToDelete splitString "task") select 0)) - 1);
	systemChat format ["Deleted task: %1", _taskToDelete];
	systemChat format ["Tasks array: %1", _tasks];
};

Tested in debug console.

  • Like 1

Share this post


Link to post
Share on other sites

if your tasks already exist:

[selectRandom ["task1","task2","task3","task4","task5","task6"] ] call BIS_fnc_deleteTask;

 

The question is : are "task1", "task2".... some valid task ids from your script ?

Share this post


Link to post
Share on other sites

@pierremgi - I already asked about what you said. Read up. Unless you are thinking of returning task ID with function then passing that to the deleteAt command? That would obviously require a match up.

Share this post


Link to post
Share on other sites
5 minutes ago, HazJ said:

@pierremgi - I already asked about what you said. Read up. Unless you are thinking of returning task ID with function then passing that to the deleteAt command? That would obviously require a match up.

Just trying to help with a working script to delete a task at random. You can "deleteAt" all task id you want, that will not delete the task.

Share this post


Link to post
Share on other sites

@pierremgi - He doesn't want to delete the task. Think he already got that. At least from what I understood, he wants to delete it from the array.

Share this post


Link to post
Share on other sites

If i've understood well, you simply want to randomselect a Task and remove it from the task list.

 

This should do the trick :

_tasks = [
    _task1,
    _task2,
    _task3,
    _task4,
    _task5,
    _task6
];

_randomTask = selectRandom _tasks;
_tasks deleteAt (_tasks find _randomTask);

 

  • Like 2

Share this post


Link to post
Share on other sites
37 minutes ago, Plasmasuka said:

If i've understood well, you simply want to randomselect a Task and remove it from the task list.

 

This should do the trick :


_tasks = [
    _task1,
    _task2,
    _task3,
    _task4,
    _task5,
    _task6
];

_randomTask = selectRandom _tasks;
_tasks deleteAt (_tasks find _randomTask);

 

I knew I was way overcomplicating it lol.

Share this post


Link to post
Share on other sites

Will not deleted the 1x selected _taskX.

 

 

taskManager.sqf

// CHECK SERVER
if (!isServer) exitWith {};



// VARIABLES
private ["_tasks", "_randomTask", "_caller"];

_caller = _this select 1;

_task1 = {execVM "scripts\taskmanager\tasks\Girna.sqf";};
_task2 = {execVM "scripts\taskmanager\tasks\Mike-26.sqf";};
_task3 = {execVM "scripts\taskmanager\tasks\Airbase.sqf";};
_task4 = {execVM "scripts\taskmanager\tasks\Kamino.sqf";};
_task5 = {execVM "scripts\taskmanager\tasks\CampRogain.sqf";};
_task6 = {execVM "scripts\taskmanager\tasks\AgiaMarina.sqf";};



// IF NO GROUP-LEADER
if ((leader group _caller) != _caller) exitWith {
	hint localize "STR_iV_OnlyGroupLeader";
};



// SELECT RANDOM TASK
_tasks = [
    _task1,
    _task2,
    _task3,
    _task4,
    _task5,
    _task6
];

_randomTask = selectRandom _tasks;
_tasks deleteAt (_tasks find _randomTask);



// CHECK IF A TASK IS ASSIGNED
if (isNil "assignedTask" || {!assignedTask}) then {
    call _randomTask;
} else {
    hint "A Task is assigned!";
};

 

 

And the mission-scripts looks like the follow one. Is only for testing.

Later I will create the tasks and triggers.

 

Kamino.sqf

hint "create Task Kamino";
sleep 3;


// SET VALUES
assignedTask = true;
publicVariable "assignedTask";

if (assignedTask) then {

    hint "assignedTask Kamino";
    sleep 8;
	
    // RESETING VALUES
    assignedTask = false;
    publicVariable "assignedTask";
	
};



// CHECK IF VALUES RESETTED
if (isNil "assignedTask" || {!assignedTask}) then {
    hint "NOT assignedTask Kamino";
};

 

Share this post


Link to post
Share on other sites

Instead of copy/paste your non comprehensible code, you should explain what you intend to do in plain language.

Something like: "My server is dedicated. I'd like to create tasks via script, for one side of the players (2 playing sides). These tasks must have some conditions met (edited triggers), then must be randomized. I'd like one different task for each player of the concerned side...."

Share this post


Link to post
Share on other sites

What I will do:

I will that the teamleader(s) can interact via addaction on a laptop and getting tasks by random from a pool (54 tasks on 21 locations on altis).

Only 1 Task should be active at the same time. Never more than one.

Every location get his own XXX.sqf which should be selected by random. Every location can be selected only 1x.

After this location (E.G. _task1 for Girna on Stratis) was selected it should be deleted from the array so it is not possible to selected it again.

 

 

What I need:

A function to delete the value from the array so that the selected _tasks can't selected again.

 

 

What I not need:

A script what spawn the contents (tasks, triggers, ai, objects, ...) for me.

 

 

 

I hope my awful english is sufficient to explain that.

cheers

Share this post


Link to post
Share on other sites
21 minutes ago, iV - Ghost said:

What I need:

A function to delete the value from the array so that the selected _tasks can't selected again.

 

That is exactly what people gave you and you said it's wrong.

Problem though is that you overwrite the _tasks list everytime so even if you remove it you always add it back to the list. Ofc that's not gonna work. Make it a global variable and only set it once (init.sqf). Then use the deleteAt way that 2 or 3 people recommended to you.

  • Like 1

Share this post


Link to post
Share on other sites

No. I've said that it won't work with this code and I hope somebody can tell me why.

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×