Jump to content
Sign in to follow this  
Gil Galvanti

Return Value From .SQF File?

Recommended Posts

Hey,

Is there any way to return a value from a call to a .sqf file? I would just use a global variable, but the problem is when the function is called by 2 different objects in a row, and they are both modifying the same global variable at the same time, so you never know what the final value will be.

Share this post


Link to post
Share on other sites

Yes. Whatever your script evaluates to will be returned. Or to make things simple, just put the variable you want to return at the end of your script without a semicolon. Ex:

_param1 = _this select 0;
_param2 = _this select 1;

_sum = _param1 + _param2;

// this will return the value of _sum
_sum

Share this post


Link to post
Share on other sites

Hmm...I'm confused on how you access it's return value though? If you do this:

_test = [] execVM "script.sqf";

_test isn't the return value, it's a script variable.

Share this post


Link to post
Share on other sites

True (afaik), but call can provide a return. I've never used execVM to return something though, so I have no clue if that is possible. Might be, check it out.

Edited by CarlGustaffa

Share this post


Link to post
Share on other sites

Right so basically I would suggest you to do funcs.

A function looks like this

calculator = {

private ["_a", "_b"];

_a = _this select 0;

_b = _this select 1;

_sum = _a + _b;

_sum

};

You place it somewhere in your mission and []execVM it

to call it you had to do

_anothervariable = [3, 4] call calculator;

_anothervariable = the return value = _sum

Share this post


Link to post
Share on other sites

Or you just compile your script into a function and use 'call' with it.

like:

YEAH_fnc_whatever = compile preprocessFile "whateveryourscriptsnameis.sqf";

_returnvalue = [parameters] call YEAH_fnc_whatever;

That's basically the same as Joshii proposed above, with the difference that you don't need to re-write your script to match the structure of a function (not that that would be a lot of work, but still...).

D.

Share this post


Link to post
Share on other sites
Please take note that functions started with call can't handle suspend code like waituntil or sleep. If your function needs such suspending, use spawn instead.

Depends on from where you call it. If you call a function inside a spawn/execVM'ed script, you can call functions with sleeps and waituntils ... it's bad style (IMHO), though.

Share this post


Link to post
Share on other sites

Ok, thanks for the help. Could someone explain to me the difference in all the function call commands? The documentation makes them seem like the same thing, there's call, call compile, spawn, execVM, preprocessFile, and loadFile.

Share this post


Link to post
Share on other sites

call means starts the code which can return a result -> Do this for me and tell me the result I'll wait for it.

spawn means start this code and run it in parallel. I'll continue doing whatever I was going to do.

loadFile reads a file and makes it a string; a piece of text.

preProcessFile reads a file and removes comment, applies preprocessor rules, and then return a string; the resulting text.

compile takes some string and turns it into code.

execVM is the equivalent of "spawn compile preProcessFile"

If you have comments or preprocessor definition/macros (// or /* og eg. #define blabla blablabla) then you need to preprocess it using either preProcessFile or preProcessFileLineNumbers. Why? Well loadFile just reads the text as it is and compile for example doesn't understand comments.

Yes. Whatever your script evaluates to will be returned. Or to make things simple, just put the variable you want to return at the end of your script without a semicolon. Ex:

You can use a semicolon if you want.

Edited by Muzzleflash

Share this post


Link to post
Share on other sites
You can use a semicolon if you want.

Yea, I suppose. I guess it's just a convention to not use one though (to indicate you're intentionally returning something).

Share this post


Link to post
Share on other sites

Isn't semicolon just a command to separate items in the same scope? Meaning that it can always be omitted on the last item in a block? I've seen others go this way, I guess it "cleans up the code" a little. But I found the pitfall to be that I always end up with stupid scripting errors ( missing ; ) due to copy and paste and forgetting to put the ; at the no longer last line. So I'm not doing it anymore, except for functions, as Big Dawg KS says, just as a convention.

Share this post


Link to post
Share on other sites

I always put a semicolon after statements. Even in "then" statements in multiline-inits and spawn commands. It makes it clear to me this statement is done and that I haven't forgot anything.

I guess it applies to the return statement too; causing me to think, if I look at it after some time, "yes I really want to return this value, it's not like I forgot the rest". I haven't found any indication on that it is a convention besides that most people do it. By popping open some random CBA and ACE files it seems like they prefer returning with a ; too. Don't know if they follow a specific "convention" or that it is author-dependent (I only opened 3 files).

Well guess it doesn't matter much in the end for readability anyway for this particular case. You just use what you want Gil (and everybody else) :).

Share this post


Link to post
Share on other sites
Even in "then" statements in multiline-inits and spawn commands. It makes it clear to me this statement is done and that I haven't forgot anything.

Don't try that in any other language..

Share this post


Link to post
Share on other sites

The lack of a trailing semicolon, at least to me, should indicate there is no more code to follow within that scope. After all, the point of a semicolon is to seperate expressions within a scope. Thus, your returned expression (having to be the last one in your function) does not need a semicolon, as nothing is following it. That's the convention I use. Same reason I don't put semicolons in single-line code blocks. Ex:

How I do it

if(true)then{hint "Hello"};

How I've seen it done

if(true)then{hint "Hello";};

The one exception for me is when I expand a code block to multiple lines:

if(true)then{
      hint "Hello";
      player sideChat "Hello";
};

And the reason for that is just because it looks nicer. Unless of course that block is returning something.

Share this post


Link to post
Share on other sites
Don't try that in any other language..

You got to be kidding?

You have to do that in C/C++, Java, Lua (I believe), and a bunch of others. In most languages that have a statement-separator you have to use it after ALL statements including the last in a if-then block.

Maybe I should have called it a "then" statement, sorry. Maybe I caused you to believe I was meaning something else with that?

Edited by Muzzleflash

Share this post


Link to post
Share on other sites
You got to be kidding?

You have to do that in C/C++, Java, Lua (I believe), and a bunch of others. In most languages that have a statement-separator you have to use it after ALL statements including the last in a if-then block.

Maybe I should have called it a "then" statement, sorry. Maybe I caused you to believe I was meaning something else with that?

Actually in LUA you are not forced to use any ; at all.

But yeah seems like I got you wrong..

SQF is like the only thing I know where it gives you an error if you don't do it like this

if() then{} else{};

So you gotta put a ; behind the } of the else.

That wouldn't be a good thing to do in C++ if am not completly wrong here.

At first I thought you were talking about that but seems like you mean statements in the condition so yeah nevermind me.

Share this post


Link to post
Share on other sites

It's not the else part that is being separated, but the whole if condition. If () then {}; without the else block also needs to be terminated like that. Same with:

switch (condition) do {

case 0 : {code block};

case 1 : {code block};

default {code block}; //No colon

}; //Ends the whole condition

Edited by CarlGustaffa

Share this post


Link to post
Share on other sites

I am aware of that.. I am just pointing out that SQF is one of few languages with does that afaik.

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  

×