Jump to content
twistking

object randomization question

Recommended Posts

Hello,

i have some objects semi randomly scattered in my mission (empty marker - random spawn). These are called cache_a, cache_b, cache_c, etc.

 

To them i've attached (attachto) objects (FMradios): radioplayer_a, rabioplayer_b, radioplayer_c etc.). Those radios come with two triggers each (getpos, setpos) for interaction, mission task, custom music etc.. So i can't change the name of the radios, otherwise the triggers don't know where to go.

 

Everything works fine, with the cache objects spawning (semi) randomly and the radios + triggers getting moved to them, but now i would also like to randomly change which radio objects gets attached to which cache object.

I attach manually with:

radioplayer_a attachTo [cache_a, [-0.25, 0.25, 0.51]];
radioplayer_a setVectorDirAndUp [[-0.5,0.5,0],[0,0,1]];
radioplayer_b attachTo [cache_b, [-0.2, -0.4, 0.4]];
radioplayer_b setVectorDirAndUp [[-0.5,0.5,0],[0,0,1]];

etc. (the attach to parameters are different with every cache object)

 

I want to do two things. First change the code so instead of the name of the object (radioplayer_a) the code for cache a holds a placeholder variable like radioplayer_xa and the code for cache b holds a placeholder radioplayer_xb.

Then i could maybe have a script in the init.sqf that randomly matches all variables radioplayer_xa ... radioplayer_xe to the actual object names radioplayer_e ... radioplayer_e.

 

I don't know how to do either o those tasks.

  1. How can i write code where instead of "radioplayer_a" i write the name of a variable that would contain the name of the object?? I tried variations of "" () [] {}, but ocould not get it to work and such "basic" things are difficult to look up in the wiki, if you don't know what you are looking for (i'm still a script noob after over 15 years in the armaverse...)
  2. How can i (in init-sqf) randomly but evenly assign all random variables (radioplayer_xa ... radioplayer_xe) to the actual object names (radioplayer_a ... radioplayer_e).

 

Any help greatly appreciated!

Share this post


Link to post
Share on other sites

Not tested, and not sure I correctly understand what you're seeking, but might could try setVariable in each radio an array of it's parameters:

_radio setVariable ["data",[_triggerA,_triggerB,_taskName,_music, etc., etc. . . . .]];

Create an array listing the radios, and while selecting the radios randomly, remove them from the array as they get selected and assigned to each cache.

Then setVariable in each cache its selected radio object:

_cache setVariable ["radio",_radio];

 

then use getVariable with params or param to define them for use.

_data = ((_cache getVariable "radio") getVariable ["data",[objNull,objNull,"",configNull]]) params 
]
	["_triggerA",objNull,[objNull]],
	["_triggerB",objNull,[objNull]],
	["_taskName","",[""]],
	["_music",configNull,[configNull]]
];

if !(_data) then 
{
	// data verification to help identify errors
};

 

 

  • Thanks 1

Share this post


Link to post
Share on other sites

thanks for the reply.

i think my request is simpler than you've read into it. by trial and error i got my first point of my question figured out. I did not realize that arma doe not care if something in an expression is an object name or a variable.

So what i do is:

cache_random_a = cache_a

and then work with cache_random_a for everythign that follows. I thought that would be more complex already.

 

So my only problem left is, how to randomize which random variable (cache_random_a, cache_random_b, cache_random_c ...) gets assigned which value (cache_a, cache_b, cache_c...)

 

So i have a number of variables and i want to randomly assign values from a list which holds the same number of entries as there are variables.

In my case, i have 5 variables (cache_random_a to cache_ramdom_e) and these should be assigned a random entry from the array (cache_a, cache_b, cache_c, cache_d, cache_e) and no entry should be in two variables. So one variable should get a value assigned and then this value shoudl get removed from the list.

Also this should be done -if possible - before editor objects init fields get initialized, because i would prefer having the code for moving and attachng the objects in the objects init fields in eden editor. for me that makes it easier to work with...

Share this post


Link to post
Share on other sites

so i would ne something like:

(in pseudo-code, because i have no idea how to do it)
 

execute on server before eden placed objects get initialized

array = [cache_a, cache_b, cache_c, cache_d, cache_e]

random_cache_a = selectrandom [array]

delete picked entry from array

random_cache_b = selectrandom [array]

delete picked entry from array

random_cache_c = selectrandom [array]

delete picked entry from array

random_cache_d = selectrandom [array]

delete picked entry from array

random_cache_e = selectrandom [array]

 

Share this post


Link to post
Share on other sites
array = [cache_a, cache_b, cache_c, cache_d, cache_e];

random_cache_a = selectrandom [array];
array = array - [random_cache_a];

random_cache_b = selectrandom [array];
array = array - [random_cache_b];

random_cache_c = selectrandom [array];
array = array - [random_cache_c];

random_cache_d = selectrandom [array];
array = array - [random_cache_d];

random_cache_e = selectrandom [array];
array = array - [random_cache_e];

 

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
Spoiler

_array = [ cache_a, cache_b, cache_c, cache_d, cache_e ];

{
	missionNamespace setVariable[ _x, _array deleteAt floor random count _array ];
}forEach [ "random_cache_a", "random_cache_b", "random_cache_c", "random_cache_d", "random_cache_e" ];

 

 

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

i needed to change things up. i need to randomize the radio-objects, not the the cache-object they get assigned to.

i've now got a gamelogic with code from opusfmspol, only with changed names and shortened for testing (only 3 objects for testing)

radiolist = [radioplayer_a, radioplayer_b, radioplayer_c];  
 
random_radioplayer_a = selectrandom [radiolist];  
radiolist = radiolist - [random_radioplayer_a];  
 
random_radioplayer_b = selectrandom [radiolist];  
radiolist = radiolist - [random_radioplayer_b];  
 
random_radioplayer_c = selectrandom [radiolist];  
radiolist = radiolist - [random_radioplayer_c];

for testing i now have triggers, because i had problems with init-order by having everything in objects init fields - need to sort that out later...

first trigger fires after one second with on activation:

random_radioplayer_a attachTo [cache_a, [-0.25, 0.25, 0.51]]; 
random_radioplayer_a setVectorDirAndUp [[-0.5,0.5,0],[0,0,1]];

second trigger fires after two seconds with:

trigger_radio_a setpos (getpos RadioPlayer_a);
trigger_found_a setpos (getpos RadioPlayer_a);

but now i get an error with " type array, expected object" at the attachto command.

 

@larrow: thanks for the code. what's the benefit of using missionNamespace? and could i put this code - as is - in init.sqf?

Share this post


Link to post
Share on other sites
3 hours ago, twistking said:

what's the benefit of using missionNamespace

Nothing, missionNamespace and global variables are the same thing.

missionNamespace setVariable[ "someVarName", "Hello World" ];
if ( someVarName == "Hello World" ) then {
  systemchat "true";
};
hint someVarname;

What it does do, by using setVariable, is give you a handy way of placing the value of the deleted index from _array straight into the variable in one line, rather than repeating code multiple times for each variable.

 

3 hours ago, twistking said:

and could i put this code - as is - in init.sqf?

Depends, is this SP or MP? SP yes, MP no, as all machines would assign different values.

If MP then it would better placed into initServer.sqf and if needed publicVariable the result.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
4 hours ago, twistking said:

now i get an error with " type array, expected object" at the attachto command

use  selectRandom radiolist  instead of  selectRandom [radiolist].  radiolist is already an array.

I should have observed that before with selectRandom [array] also.

 

  • Thanks 1

Share this post


Link to post
Share on other sites

thanks to both of you. got it working reliably now (both in SP and on dedicated server).

code is still a bit messy, but i'll sort that out later... eventually...

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

×