Error: On Activation: Select: Type: Nothing expected, Array,String,Config entry when running specified code in Arma 3 - sqf

So, my issue is whenever I run this code block within a trigger with condition any player,
private _all_freedom_units = ["B_FRD_Freedomer_Patrolman_01","B_FRD_Freedomer_Sniper_01", "B_FRD_Freedomer_Exo_1", "B_FRD_Freedomer_Warrior_01", "B_FRD_Freedomer_SEVA_01", "B_FRD_Freedomer_Gaurdian_01", "B_FRD_Freedomer_Seva_II_01"];
private _final_pos = [0,0,0];
private _position_player = [0, 0, 0];
_direction = getDir player;
if (_direction > 0 and _direction < 90) then {cardinal_direction = "North";};
if (_direction > 90 and _direction < 180) then {cardinal_direction = "South";};
if (_direction > 180 and _direction < 250) then{cardinal_direction = "South";};
if (_direction > 250 and _direction < 359) then{cardinal_direction = "North";};
_position_player = getPos player;
_position_player_x = _position_player select 0;
_position_player_y = _position_player select 1;
if (cardinal_direction == "North") then{
_random_x = [250, 500] call BIS_fnc_randomInt;
_random_y = [250, 500] call BIS_fnc_randomInt;
_final_pos set [0 , _position_player_x + 200 + _random_x];
_final_pos set [1 , _position_player_y + 200 + _random_y];
};
if (cardinal_direction == "South") then{
_random_x = [-250, -500] call BIS_fnc_randomInt;
_random_y = [-250, -500] call BIS_fnc_randomInt;
_final_pos set [0 , _position_player_x + _random_x];
_final_pos set [1 , _position_player_y + _random_y];
};
_position = _final_pos;
_group_freedom_patrol = createGroup [west, true];
hint str _final_pos select 0;
_random_number = floor random 5;
The editor returns with,
On Activation: Select: Type Nothing, expected Array,String,Config entry
I have a suspicion from similar reports that it's centered around these statements
_position_player = getPos player;
_position_player_x = _position_player select 0;
_position_player_y = _position_player select 1;
Because of the "select" aspect of the error code, but I just started Arma 3 scripting
A similar post recommended dealing with the Condition portion of the Trigger, but the fixes their didn't work. I'm not quite sure what the issue is at this point, as I'm new to Arma 3 scripted.
The big issue here is I'm 90% sure it's an issue with syntax, because if it was logic the trigger would allow me to use the code in the first place. In this case, the built in checks are barring me from running the code, and syntax errors are harder than logic for me.
The code is intended to run properly, grab the first player who enters the trigger, and using the position data and angle it should (this part of the code isn't included as I'm testing in chunks and this chunk isn't working) spawn units in front of the player, min 250 out, heading towards them,
But as I explained previously, the Arma 3 debugger is yelling at me before I can even test the code.

Take this with a grain of salt, I haven't scripted for Arma 3 for quite the time.
I think if the errors comes up in the editor, then it may be because the player may not be defined (makes sense in the editor). You should check that. Either way - to make it multiplayer compatible - you should use the the "thisList" variable to access the list of the objects which are in the trigger area. If this only triggers for players, then you can use "thisList select 0".
See the passed variables for On Activation in the documentation

Related

uiresume (gcbf) worked in the function, but failed when the function being called

I want to let the user select multiple ROI's on a picture. I used a while loop. I paused and let the user select an ROI. After that the while loop would continue unless the user clicked another button on the toolbar to terminate the while loop. The code worked on single pictures. I made the code a function. When I looped and called the function in another script, it failed to proceed. I pressed Ctrl + C and it showed that "Operation terminated by user during uiwait". Apparently the uiresume didn't work.
Please let me know where the problems are. Thanks! My code:
% Below was basically a copy of the example given in R2014a.
% It created an interactive toolbar with 2 pushbuttons.
fh = figure('ToolBar', 'none'); hold on;
h_im = imshow(rgb2gray (I));
tbh = uitoolbar(fh);
[X_OK, map_OK] = imread(fullfile(matlabroot,...
'toolbox','matlab','icons','greenarrowicon.gif'));
[X_rect, map_rect] = imread(fullfile(matlabroot,...
'toolbox','matlab','icons','tool_rectangle.gif'));
% Convert indexed image and colormap to truecolor
icon_OK = ind2rgb(X_OK, map_OK);
icon_rect = ind2rgb(X_rect, map_rect);
% Create 2 uipushtools in the toolbar
% I introduced 2 variables, leave and draw, to facilitate the control of the
% later while-loop.
pth_OK = uipushtool(tbh, 'CData',icon_OK,...
'TooltipString','Toolbar push button',...
'ClickedCallback',...
'leave = 1; uiresume (gcbf)');
pth_rect = uipushtool(tbh, 'CData',icon_rect, 'Separator','on',...
'TooltipString','Toolbar push button',...
'ClickedCallback',...
'draw = 1; uiresume (gcbf)');
% The loop for ROI selection.
ii = 1;
% Maximum the use can select 30 ROI's.
while ii < 31;
draw = 0;
uiwait;
if leave == 1;
% If the user pressed "OK" button, leave will be 1.
break;
end;
if draw == 1;
% If the user pressed the "rect" button, draw will be 1.
h = imrect;
wait (h);
gui_Mask = createMask(h, h_im);
greyImg (~gui_Mask) = 255;
ii = ii + 1;
continue;
end;
end;
I saw someone used set/waitfor instead.
I tried to change to set/waitfor. Now it works both in the function and when the function being called.
But I still want to know why uiresume didn't work.

Ruby on Rails optimalization of some code

I have some simple code that uses the minmax algoritm to locate birds. Everything works but I find my programming not good and I believe there is a better solution. I'm not that experienced in RoR but if somebody knows a better way to achieve the same solution then I'm greatful ;).
There are two parts I hate, the 4 lists I had to create to determine the max or min value for the different combinations (the core of the min-max algorithm) and the very ugly SQL hack.
Thanks!
def index
# fetch all our birds
#birds = Bird.all
# Loop over the birds
#birds.each do |bird|
#fixed = Node.where("d7type = 'f'")
xminmax = []
xmaxmin = []
yminmax = []
ymaxmin = []
#fixed.each do |fixed|
rss = Log.find_by_sql("SELECT logs.fixed_mac, AVG(logs.blinker_rss) AS avg_rss FROM logs
WHERE logs.blinker_mac = '#{bird.d7_mac}' AND logs.fixed_mac = '#{fixed.d7_mac}' ORDER BY logs.id DESC LIMIT 30")
converted_rss = calculate_distance_rss(rss[0].attributes["avg_rss"])
xminmax.push(fixed.xpos + converted_rss)
xmaxmin.push(fixed.xpos - converted_rss)
yminmax.push(fixed.ypos + converted_rss)
ymaxmin.push(fixed.ypos - converted_rss)
end
pos = {x: (xminmax.min + xmaxmin.max) / 2, y: (yminmax.min + ymaxmin.max) / 2}
puts pos
end
end
2 things you could do to start with is (assuming Birds could be a large table) Change Bird.all to
Bird.find_each do |bird|
... code ...
end
It's a more efficient way to loop over many table records.
2nd: take #fixed = Node.where("d7type = 'f'") out of the each loop since it doesn't need any variables for its query. Put it above the loop so it doesn't execute each time.
3rd (Not so much of an optimization but just safer code): Your Log.find_by_sql looks simple enough to use active_record, you can change it to:
Log.select('fixed_mac, AVG(logs.blinker_rss) AS avg_rss, blinker_mac').
where(blinker_mac: bird.d7_mac, fixed_mac: fixed.d7_mac).
order('id DESC').limit(30)
converted_rss = calculate_distance_rss(rss.first.avg_rss)
Everything else looks fine.

How do I set a function to a variable in MATLAB

As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points

Tips for function inside while loop and i=i+1, Matlab

I have a problem with a function in matlab. This specific function is for filtering light signals. As you can see below I added the coding I’ve used in the function and in the while loop itself. The code is written for a NXT Lego robot.
Is there any tip how to get the count variable ( i = i + 1 ) to work in the function, so we can plot Light(i)? Because we’re getting a bunch of error messages when we try different codes to make it work.
function [light] = filter_func( i)
lightI(i) = GetLight(SENSOR_3);
if i==1
light(i)=lightI(i)
elseif i==2
light(i) = 0.55*lightI(i) + 0.45*lightI(i-1)
else
light(i) = 0.4*lightI(i) + 0.3*lightI(i-1) + 0.3*lightI(i-2);
end
end
i=1
while true
lightI(i) = GetLight(SENSOR_3); % Get’s a lightvalue between 0 and 1024.
if i>2
light =filter_func(i)
light=round(light);
else
light(i) = GetLight(SENSOR_3);;
end
i=1+i
plot(light(end-90:end), 'r-');
title('Lightvalue')
axis([0 100 0 1023]) ;
end
You probably mainly get errors because you are not allowed to mix script and functions like this in MATLAB (like you are in Python).
Your filter function is only used when i>2 so why are you doing the first 2 tests? It seems like you want lightI as a global variable, but that is not what you have done. The lightI inside the function is not the same as the one in the while loop.
Since your while loop runs forever, maybe you don't need to worry about updating the plot the first two times. In that case you can do this:
filter = [0.4 0.3 0.3]';
latest_filtered_light = nan(90,1);
lightI = [];
p = plot(latest_filtered_light, 'r-');
title('Lightvalue')
axis([0 100 0 1023]) ;
while True
lightI(end+1,1) = rand*1024; % Get’s a lightvalue between 0 and 1024.
if i>=3
new_val = lightI(end-2:end,1)'*filter;
latest_filtered_light = [latest_filtered_light(2:end);...
new_val];
set(p, 'ydata', latest_filtered_light)
drawnow
end
end
I think it is an important point to not call plot every time - at least if you are the least concerned about performance.

Function callback ('StopFcn' , 'TimerFcn' )for audiorecorder object in MATLAB? [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
How can I use function callback ('StopFcn' , 'TimerFcn' )for audiorecorder object in MATLAB?
So I try to this code.
% assume fs,winsize,winshift is given.
T = 0.1; % in seconds
samples = cell{100,1};
r = audiorecorder(fs,16,1);
k=1;
r.TimerPeriod = 0.1;
r.StopFcn = 'samples{k} = getaudiodata(r);';
r.TimerFcn = {#get_pitch,samples{k},winsize,winshift};
while 1
record(r,T);
k=k+1;
end
I want to execute the function 'get_pitch(samples,fs,winsize,winshift)' while during recording through audiorecorder object.
But following exception occurs during execution.
1) after record(r,T) is executed. (StopFcn is now called) ??? Error using ==> eval Undefined function or variable 'r'.
2) after StopFcn is called (TimerFcn is now called) In this phase, get_pitch function have totally wrong parameters. For example, parameter in the position samples{k} change to 'audiorecorder object'.
It seems that I do not know exact use of 'StopFcn' & 'TimerFcn'.
Is there anyone who can give me some advice? I really appreciate all of your comments.
Looking at the example in the documentation I would recommend trying to call getaudiodata(r) in your loop rather than with the CallBack. So something like this:
% assume fs,winsize,winshift is given.
T = 0.1; % in seconds
samples = cell{100,1};
r = audiorecorder(fs,16,1);
k=1;
r.TimerPeriod = 0.1;
r.StopFcn = 'disp(''Completed sample '', k)';
r.TimerFcn = {#get_pitch,samples{k - 1},winsize,winshift};
while 1
record(r,T);
samples{k} = getaudiodata(r);
k=k+1;
end
Note I changed the r.TimerFcn to use samples{k - 1} instead of k because k will increment before the timerfcn gets called. So this might give you issues with your first sample, you'll have to tweak it a little. Also this is an infinite loop, which I'm sure you'll want to address.