Jump to content
lukasaurus

What is wrong with this code (beginner)

Recommended Posts

I'm just learning, going through some video tutorials. I'm familiar with Python and C#.

Some of the syntax in Arma Script makes no sense to me

for example, the following should work in my head, but does nothing in game. I cannot figure out why

random_task_text = ["pink_task","green_task","yellow_task"];//name of task
random_task_target = [pink_task,green_task,yellow_task];//target of task
//_random_task = random_task_text call BIS_fnc_selectRandom;
//hint _random_task //this works
_x = floor (random count random_task_text); //get a random task index
//hint str _x //this also works
hint random_task_text[_x]; //display the task name

On lines 3 and 4, that will select a random string and display it. Line 5 is also working (you can see my code to test it commented out). But line 7 does nothing, when in my mind, it should take the random int generated from line 5 and then display the string in that index. It doesn't. Any help?

This function is called random_task.sqf and the init.sqf files runs it. I can get it to do the stuff that I have commented out, but am confused as to why I can't get it to work the way I want.

Share this post


Link to post
Share on other sites

the reason why your hint wont pup up or why it wont select random there is because you are using _x, _x is for other stuff like where you use the foreach command. so rename the _x var to something else and it should work

Share this post


Link to post
Share on other sites

hint random_task_text[_x]; is nonsense. What you want to do is:

_randomText = random_task_text select (floor random (count random_task_text)); 
hint str _randomText; 

select

Also, as noted above, don't use _x as a variable, since it is the magic variable used to reference the current element in a forEach statement, for example:

{hint str _x; sleep 1;} forEach ["pink_task","green_task","yellow_task"];

... where each string would be displayed in a hint every 1 second.

And if you're not using it already, enable showScriptErrors via the startup parameters.

Edited by 2nd Ranger

Share this post


Link to post
Share on other sites
hint random_task_text[_x]; is nonsense. What you want to do is:

What I am wanting to do is get the index value and then use it for multiple arrays. How would I do that? I understand what you did, but that won't give me the index of the string, so I can't use it for other arrays...

Share this post


Link to post
Share on other sites

Oh, I see. Well in that case, just what you had originally, but not using _x as the variable.

_int = (floor random (count random_task_text));

And then to use that to select a string and then the corresponding target:

_text = random_task_text select _int;
_target = random_task_target select _int;
hint str _text; //---display the text

Edited by 2nd Ranger

Share this post


Link to post
Share on other sites

Either (as 2nd Ranger has shown above) by getting a random index then using this to select from both lists/arrays.

task_texts = [ "pink_task", "green_task", "yellow_task" ];
task_targets = [ pink_task, green_task, yellow_task ];

_random_index = floor ( random ( count task_texts ));
_random_task_text = task_texts select _random_index;
_random_task_target = task_targets select _random_index;

hint format [ "Selected Task\n%1\n\nTasks Target\n%2", _random_task_text, _random_task_target ];

Or if you already have a selected index and you need to find the corresponding index from another list/array use FIND

//name of tasks
task_texts = [ "pink_task", "green_task", "yellow_task" ];
//target of tasks
task_targets = [ pink_task, green_task, yellow_task ];

//Get a random task text
_random_task_text = task_texts call BIS_fnc_selectRandom;

//Find index of random task test
_random_task_index = task_texts find _random_task_text;

//Select corresponding task target of select random task text
_random_task_target = task_targets select _random_task_index;


hint format [ "Selected Task\n%1\n\nTasks Target\n%2", _random_task_text, _random_task_target ];

Edited by Larrow
corrected missspelt variable

Share this post


Link to post
Share on other sites
Oh, I see. Well in that case, just what you had originally, but not using _x as the variable.

_int = (floor random (count random_task_text));

And then to use that to select a string and then the corresponding target:

_text = random_task_text select _int;
_target = random_task_target select _int;
hint str _text; //---display the text

Alright, I get it, cheers. That's completely unlike any other programming language I've used before with accessing array indexes, which is why I was getting so confused.

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

×