Jump to content
Sign in to follow this  
bangabob

Send a condition into script

Recommended Posts

Hello guys.

Got a small problem with my script. I am trying to create a system that manipulates markers and part of this involves passing a condition into the script.

For example

null=["mkr","!alive obj1",[]] execVM "script.sqf";

Script.sqf

_mkr=(_this select 0);
_task=(_this select 1);

while {true} do
{
if (_task) then {//code here};
sleep 0.5;
};

Btw i know i can send the code using {} brackets. But then this doesn't work with "if" statements.

So my question is how can i retrieve code from my script call. I have looked into string manipulation to remove the "" but this is all a little above me.

Thanks for reading

Share this post


Link to post
Share on other sites

null = ["mkr",{if (!alive obj1) then {true} else {false};},[]] execVM "script.sqf";

Perhaps something like that.

Alternative would be to sort out the bool beforehand and contain it in a variable...

private ["_condition"];

_condition = false;

if (!alive obj1) then {
_condition = true;
};

null = ["mkr",_condition,[]] execVM "script.sqf";

Edited by MDCCLXXVI

Share this post


Link to post
Share on other sites
null = ["mkr",{if (!alive obj1) then {true} else {false};},[]] execVM "script.sqf";

Perhaps something like that.

Alternative would be to sort out the bool beforehand and contain it in a variable...

private ["_condition"];

_condition = false;

if (!alive obj1) then {
_condition = true;
};

null = ["mkr",_condition,[]] execVM "script.sqf";

This is not really what i am looking for because i already use a very similar method as a workaround.

Specifically i want to extract the code from the string by removing the "". Is this even possible?

Share this post


Link to post
Share on other sites

compile sounds like what you're looking for

So:

if (compile _task) then {//code here};

Nevermind, that outputs code (aka {}).

Edited by SilentSpike

Share this post


Link to post
Share on other sites

Did you try to return something from the code and then execute it?

null=["mkr",{_cond = !alive obj1; _cond},[]] execVM "script.sqf";


_mkr=(_this select 0);
_task=(_this select 1);

while {true} do
{
if (call _task) then {//code here};
sleep 0.5;
};

Share this post


Link to post
Share on other sites
Did you try to return something from the code and then execute it?

null=["mkr",{!alive obj1; _cond},[]] execVM "script.sqf";


_mkr=(_this select 0);
_task=(_this select 1);

while {true} do
{
if (call _task) then {//code here};
sleep 0.5;
};

Cool, that works. Im half asleep so i am probably missing something obvious but is there a way to have the {!alive obj1;_cond} bit inside the sqf file rather than the script call

Something like

null=["mkr",{_cond = !alive obj1;},[]] execVM "script.sqf";

_mkr=(_this select 0);
_task=(_this select 1);

// special way here

while {true} do
{
if (call _task) then {//code here};
sleep 0.5;
};

Edited by BangaBob

Share this post


Link to post
Share on other sites

Call compile can turn string to code without the brackets.

call compile format ["_obj = %1",_varname];

is the same as

_obj = _varname;

Edited by Fight9

Share this post


Link to post
Share on other sites
Call compile can turn string to code without the brackets.

call compile format ["_obj = %1",_varname];

is the same as

_obj = _varname;

You have to call it though because it is a code snippet (aka, has brackets). So it doesn't work in an if statement unless it's returning a boolean

Share this post


Link to post
Share on other sites

Ive got it. Simple really.

null=["mkr","!alive obj1"] execVM "script.sqf";

Script.sqf

_mkr=(_this select 0);
_task=(_this select 1);

while {true} do
{
if (call compile _task) then {//code here};
sleep 0.5;
};

I used call compile in the if statement.

Turns a string into code.

Share this post


Link to post
Share on other sites

Just remove the "" from around !alive obj1.

null=["mkr",!alive obj1,[]] execVM "script.sqf";

Share this post


Link to post
Share on other sites
Just remove the "" from around !alive obj1.

Surprised none of us even noticed this sooner :p

Share this post


Link to post
Share on other sites
Ive got it. Simple really.

null=["mkr","!alive obj1"] execVM "script.sqf";

Script.sqf

_mkr=(_this select 0);
_task=(_this select 1);

while {true} do
{
if (call compile _task) then {//code here};
sleep 0.5;
};

I used call compile in the if statement.

Turns a string into code.

You could optimise it a bit by compiling it one time and then just calling it:

_mkr=(_this select 0);
_task=(_this select 1);
_cond = compile _task;

while {true} do
{
if (call _cond) then {//code here};
sleep 0.5;
};  

It's a bit cleaner since it doesn't force the engine to recompile it on each iteration. Or you could pass it already as code {} as someone mentioned before.

---------- Post added at 07:09 PM ---------- Previous post was at 07:07 PM ----------

Removing the "" will just result in a one-time-only evaluation which is not what he was looking for if he runs the condition stuff in a loop I guess. But I might be wrong, didn't test it.

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
Sign in to follow this  

×