Jump to content
Sign in to follow this  
Craig_VG

Formatting ExecVM

Recommended Posts

Hi! I am trying to use formatting, like in hint/chat/text, for the execVM command.

I found this thread: http://forums.bistudio.com/showthread.php?t=102745

It got me to this:

call compile format [execVM "spawn%1.sqf", wave];

However, it doesn't work. No script errors, but it gives me this message: Script Spawn%1.sqf not found

Does this mean that what I am trying to do is impossible?

thanks

bobtom

Share this post


Link to post
Share on other sites

Very ugly coding. Don't do it.

Proper way:

_waves = ["wave1.sqf","wave2.sqf",..];
_wave = _waves select (round (random ((count _waves) - 1)));

[] execVM _wave;

Share this post


Link to post
Share on other sites

Just a guess as I cannot test at home at the moment, but if you want to press ahead with your current method, try this:

call compile format ["execVM ""spawn%1.sqf""", wave];

The first value in the format array must be a string. Use double-quotes around spawn%1.sqf to preserve its own quote marks.

Share this post


Link to post
Share on other sites
Very ugly coding. Don't do it.

Proper way:

_waves = ["wave1.sqf","wave2.sqf",..];
_wave = _waves select (round (random ((count _waves) - 1)));

[] execVM _wave;

It works, but it spawns random waves, correct?

I want it to exec a certain sqf depending on what wave the mission is on.

ie: on wave one, spawn1.sqf is exec. On wave 2, spawn2.sqf is exec. I have a variable, wave, to tell what wave I am on.

Just a guess as I cannot test at home at the moment, but if you want to press ahead with your current method, try this:

call compile format ["execVM ""spawn%1.sqf""", wave];

The first value in the format array must be a string. Use double-quotes around spawn%1.sqf to preserve its own quote marks.

That works great! That solves it! Thank you

Edited by bobtom

Share this post


Link to post
Share on other sites

Try this:

_wave = _waves select (_yourVariable - 1);//-1 as array starts at 0

@ fatty86

Don't teach people bad coding habit and style and performance. ;)

Share this post


Link to post
Share on other sites

Because the script name is a string, you could just format it like so:

[] execVM format ["spawn%1.sqf",wave];

Share this post


Link to post
Share on other sites
@ fatty86

Don't teach people bad coding habit and style and performance.

fatty86's advice was helpful for the question asked, just because you don't agree with the OP's algorithm doesn't mean you should criticize those who help him implement it. Just because something isn't, in your opinion, the "best" way to code something, doesn't mean it wouldn't be extremely beneficial for a novice scripter (like most of us here) to learn how to do it anyway, to understand the technique, its advantages and limitations. Of course you are welcome to criticize, but it would be nice if you went into detail about what those limitations are without casting a blanket judgment upon them.

That said, I do think Celery's implementation of said "bad" algorithm is slightly more elegant ;p

Oh, and doing a select (round (random ((count _arr) - 1)) seems like it might make more sense as select (floor (random (count _arr))). Just to save on parentheses and math, two of my least favorite subjects.

Edited by dwringer

Share this post


Link to post
Share on other sites

@ dwringer

Do you have smileys blocked?

Share this post


Link to post
Share on other sites
@ dwringer

Do you have smileys blocked?

It still comes out as impolite unless you give real reasons why the code is bad. If you ask me, adding single-use local variables doesn't make it proper or elegant.

Oh, and doing a select (round (random ((count _arr) - 1)) seems like it might make more sense as select (floor (random (count _arr))). Just to save on parentheses and math, two of my least favorite subjects.

The parentheses are all unnecessary because the code is executed in that order anyway. array select floor random count array works all the same.

Share this post


Link to post
Share on other sites

derp :P

I've been doing everything in emacs lately so its paren-matching has gotten me into some pretty horrible coding practices, although I need to look into whatever editor that is where there's a color coding configuration based around .sqf syntax, can't remember off the top of my head...

But thank you, I had never really gotten around to figuring out how exactly things get evaluated. Guessing a list of function names just gets evaluated right to left then. haha.

Share this post


Link to post
Share on other sites

Would be much better for you in the long run if you just have 1 spawning script and pass the number as a parameter for the script (or just have the script itself pick it at random without passing any parameters). Writing a lot of scripts is kinda silly imo. But in any case Celery's suggestion is probably the most simple for how you're trying to do it.

Share this post


Link to post
Share on other sites
fatty86's advice was helpful for the question asked, just because you don't agree with the OP's algorithm doesn't mean you should criticize those who help him implement it. Just because something isn't, in your opinion, the "best" way to code something, doesn't mean it wouldn't be extremely beneficial for a novice scripter (like most of us here) to learn how to do it anyway, to understand the technique, its advantages and limitations. Of course you are welcome to criticize, but it would be nice if you went into detail about what those limitations are without casting a blanket judgment upon them.

Thanks dwringer.

Share this post


Link to post
Share on other sites

(round (random ((count _array) - 1))

Shows the clear logic behind it rather to hide it and therefore your suggestion would make the code less readable.

@ Celery

The search function will tell you the reasons. Gaia (BI dev), Sickboy and Xeno explained it many many times.

To use parentheses makes the intend of code clear, avoids errors and is general good practice.

As non programmer you probably don't care about such "minor details". New guys should learn to do it better though.

Edited by .kju [PvPscene]

Share this post


Link to post
Share on other sites

Well count _array will return the length of the array, so generating a random with (count _array) - 1 as the argument will generate a random number from 0 to the last index of the array, non inclusive. The round will round some values (but not quite half of an integer's worth) up to the last index. Since the random number generator in ArmA2 is dubious at best this is acceptable, but it is not more logically sound.

(round (random ((count _array) - 1))

Shows the clear logic behind it rather to hide it and therefore your suggestion would make the code less readable.

Share this post


Link to post
Share on other sites

I think he meant that using floor instead of round with a -1 is clearer.

Share this post


Link to post
Share on other sites
I think he meant that using floor instead of round with a -1 is clearer.

That's not what it seemed like, since that would mean we are in agreement. The floor is both clearer and more logically correct.

I certainly feel bad for arguing a point like this when it's something merely stylistic, but the more I think about it the logic is simply flawed without using the "floor" statement.

Ex. We have an array of length 10, with indices 0 thru 9.

Selecting (random (count - 1)) would give us a number between 0 and 8.9999 (dunno how many significant places are used here by the engine).

Rounding that would mean any number between 0 and 0.49 gives us 0, anything between 0.5 and 1.49 gives us 1, and so on, to where a number between 7.5 and 8.49 gives 8 and only 8.5 thru 8.99 yields 9. That means the chances of getting a 0 or a 9 COMBINED only equal the chance of getting any other specific number; thus the "random" index is twice as likely not to be the first or last element of the array.

Using the floor method (while not "clearer" to some, apparently) circumvents this problem, simply truncates the number past the decimal place returned by random, and consequently just gives us a random integer corresponding to one (any) of the array indices.

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  

×