Jump to content
Sign in to follow this  
Splicer

Unit's init lines are run before init.sqf ?

Recommended Posts

Hi all,

During a recent discussion (see thread http://forums.bistudio.com/showthread.php?t=91696) it was mentioned the engine would read first the code in a unit's init field vs the mission's init.sqf.

The significance of this is that a function created by a user in the init.sqf would not be executed when such function is requested in a unit's init field (i.e., call or spawn commands) when the mission is started.

I'm new to coding in Arma2, so this topic is fundamental to gain a better understanding on writing and using scripts for the game.

Could any of you experts share your wisdom on this? :pray:

Thanks much in advance. Splicer.

Share this post


Link to post
Share on other sites

mission file -> (description and briefing) -> init.sqf -> whatever you start from init.sqf

I think. The mission file contain the init. Briefing may come after the init. Whatever it is, yes the mission unit init is always done before the init.sqf.

Edited by Murklor

Share this post


Link to post
Share on other sites

@ shk,

Thanks for that pointer: Units --> init.sqf at the Briefing of the mission

Question: How can then one execute compiled scripts or functions in a unit's init field (via either call or spawn command)?

Would adding a Game Logic with an instruction to compile a script do the trick?

Thanks. Best, Splicer.

Share this post


Link to post
Share on other sites

Instead of calling the function, either spawn code or execVM a script that waits until the function is ready (waitUntil {!isNil "fnc_name"};) and then does its thing. Init lines do get run before init.sqf, but the scripts that are spawned by them do not. In any case, it's always a good idea to not rely on the order in which things run, and instead write your code in a way that will work regardless of the order in which things run (by using waitUntil and/or only calling the script when you actually want it to run).

Share this post


Link to post
Share on other sites

Hi,

Well, I need to revisit this because it is not working for me.

In the AI's init field I've entered the following code:

waitUntil {!isNull PatrolScript}; Team1a = group this; nul=[this,"Zone1", "move", "delete:",600] spawn PatrolScript;

And ArmA2 gives me the follow error message (from the arma2.RPT file):

Suspending not allowed in this context

Error in expression <waitUntil {!isNull PatrolScript}; Team1a = >

Error position: <!isNull PatrolScript}; Team1a = >

Error Generic error in expression

I've also placed the waitUntil {!isNull PatrolScript}; code after Team1a = group this; and I still get an error message.

And to cover my bases, I also used !isNil instead of isNull and got the same error message. :banghead:

Could anybody help me understand what I am doing wrong? :pray:

Thanks much in advance.

Best, Splicer.

Share this post


Link to post
Share on other sites

Splicer,

The error message is as simple as it says:

You simply cannot use waitUntil in an init line. waitUntil is only valid in scripts. This is at least what I can tell from personal experience. Trying to use waitUntil in the init line appears to throw this error message.

Also: why are you using spawn? and why do you need to waitUntil PatrolScript is not null? Does the script not work if you just use execVM?

Share this post


Link to post
Share on other sites

and... if you do what xpaveway said wich is correct in the script you need to do it like this: Team1a = group @unit name in editor@; nul=[Team1a,"Zone1", "move", "delete:",600] spawn PatrolScript;

scripts dont like :D "this" you need to tell them what "this" is

Share this post


Link to post
Share on other sites
and... if you do what xpaveway said wich is correct in the script you need to do it like this: Team1a = group @unit name in editor@; nul=[Team1a,"Zone1", "move", "delete:",600] spawn PatrolScript;

scripts dont like :D "this" you need to tell them what "this" is

This is not correct. There is nothing wrong with using this in parameters.

How else would the famous this exec "camera.sqs" work?

@Splicer:

If you need to use sleep or waitUntil in the init line you can use spawn:

nil = this spawn {waitUntil {!isNull PatrolScript}; Team1a = group _this; nil=[_this,"Zone1", "move", "delete:",600] spawn PatrolScript};

Edited by Deadfast

Share this post


Link to post
Share on other sites

What Deadfast said is probably the most simple workaround, however remember that "this" is not defined in the spawned code, so you will either need to use hNil = [this] spawn {...} where (_this select 0) would refer to the first parameter of the spawned code, aka this, or just use the actual unit's name.

Also, I thought briefing.sqf does not get executed automatically?

Share this post


Link to post
Share on other sites
...however remember that "this" is not defined in the spawned code...

Ah, yes, thanks for reminding me, rewriting the code.

Share this post


Link to post
Share on other sites

At the risk of derailing this, if there is an init eventhandler in the item's config, when is it run in relation to the editor init line?

My hunch is that the config is run first, but I am not sure.

Share this post


Link to post
Share on other sites

deadfast do you mean you can refer to the caller of the script inside a script with "this", like:

this domove p1;

ho is "this"? does he know that this is the unit that calls the script?

sorry if said something wrong i just dint know you can do that

______________________________________________________________

Originally Posted by galzohar

...however remember that "this" is not defined in the spawned code...

Ah, yes, thanks for reminding me, rewriting the code.

:| i missed this

Edited by lucilk

Share this post


Link to post
Share on other sites

this is not defined in a script, only in a unit's init line, and then again not in anything spawned from it. In a script you have _this which refers to the parameter (usually array) that was sent to the script via hNil = [parameters] execVM "script.sqf"

Share this post


Link to post
Share on other sites

Hi guys,

Thanks for all the help!

I tried the code, but there is a problem with {!isNull PatrolScript}

The reason is that PatrolScript is an array and isNull works with objects, controls, display, group or locations.

Any thoughts about how to check that a script (e.g., PatrolScript) has actually been compiled and it's in memory?

Thanks. -Splicer.

Share this post


Link to post
Share on other sites

Splicer.

To check variables with isNil you need to enclose them with ""

!isNil "PatrolScript"

About "this"

It's a programming reference which will take a while to explain.

But basically if you put "this" in a units init-line it will always refer to the object(unit) itself.

e.g.

A soldiers init-line

MyDude = this;

MyDude will be: B1 1-1-1

MyGroup = (group this)

MyGroup will be: B1

And so on.

hope that shed some light on the subject.

Edited by Taurus

Share this post


Link to post
Share on other sites

Actually afaik for checking if a variable is defined or not you need to use isNil "varname". isNull should check if the actual object the variable pointing at is null or not.

Share this post


Link to post
Share on other sites

Yes you are correct, I didn't check carefully what command was being used, I assumed it was isNil.

So I just "copy pasted" it and added the quotation marks.

It's not that I didn't write the correct command on the first line.

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  

×