Jump to content
Sign in to follow this  
Impavido

Distance not being calculated on local variables?

Recommended Posts

I am working on my own recruitment script that is run by a trigger when West approaches it. It should addaction when the player is close enough and then remove all the actions when the player is farther.

I cannot get a simple distance calculation working for two local variables. I know the rest of the script works--it gets stuck running back and forth in the loop that checks distance. It runs that indefinitely because it is not calculating distance or identifying the variables correctly. What am I missing?

HERE IS THE TRIGGER THAT DETECTS THE PLAYER, which should be array _x. _item is the barracks.

_trg=createTrigger["EmptyDetector",getPos _item];
_trg setTriggerActivation ["WEST", "PRESENT", true]
_trg setTriggerArea [10, 10, 0, false] 
_trg setTriggerStatements ["this", "[{[_x,_item] exec ""buildings\barracksrecruit.sqs""} foreach thislist]", ""]

THIS IS THE RECRUITING SCRIPT, WITH THE PROBLEM AREA HIGHLIGHTED.

_person is the player detected by the above trigger.

_obj is the barracks

general is a global variable name for a player position.

_person = _this select 0
_obj = _this select 1

?!(alive _obj): exit

R1=(General addaction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj]])
R2=(General addaction ["RECRUIT Recon Grenadier ($1000)","recruit\wgen\gren.sqs", [_obj]])
R3=(General addaction ["RECRUIT Recon Medic ($1000)","recruit\wgen\medic.sqs", [_obj]])
R4=(General addaction ["RECRUIT Recon M-Gunner ($1500)","recruit\wgen\mgun.sqs", [_obj]])
R5=(General addaction ["RECRUIT Recon Demo Spec. ($1000)","recruit\wgen\demo.sqs", [_obj]])
R6=(General addaction ["RECRUIT Recon Marksman ($1500)","recruit\wgen\mark.sqs", [_obj]])
R7=(General addaction ["RECRUIT Recon Sniper ($2000)","recruit\wgen\sniper.sqs", [_obj]])
R8=(General addaction ["RECRUIT Recon Sniper-50 Cal ($3000)","recruit\wgen\sniper50.sqs", [_obj]])

?!(alive _obj): goto "deactivate"

[color="Red"][b]#check
~.5

?((_person distance _obj)>10): goto "deactivate"

goto "check"
[/b][/color]
#deactivate

general removeaction R1
General removeaction R2
General removeaction R3
General removeaction R4
General removeaction R5
General removeaction R6
General removeaction R7
General removeaction R8

exit

Edited by Impavido

Share this post


Link to post
Share on other sites

Just a thought?

If you want to add the actions when someone enters the trigger, why not

On Act: [MyBarrack] exec "myaddaction.sqs"

On Deact: [MyBarrack] exec "myremaction.sqs"

As the trigger will measure the distance, if you walk outside it'll get deactivated.

If you chose not to.

Are you sure _obj and _person actually contains the player and barrack?

_trg setTriggerStatements ["this", "[{[_x,_item] exec ""buildings\barracksrecruit.sqs""} foreach thislist]", ""]

Those things are strings and won't convert _item to the variable or object.

There are probably more clever ways of doing this, but here's mine :)

_onActstr = format["{[%1] exec ""buildings\barracksrecruit.sqs""} foreach thislist",vehicleVarName _item];
_trg setTriggerStatements ["this", _onActstr, ""];

And wont the sqs script be executed by every unit in the trigger?

Say if three walks into it, you'd get three scripts running?

Share this post


Link to post
Share on other sites

#check
~.5

?((_person distance _obj)>10): goto "deactivate"

goto "check"

have you tried distance without the brackets ???

? _person distance _obj > 10

Share this post


Link to post
Share on other sites

I'm getting some great suggestions, I planon trying them out.

But the problem is related to the "_x foreach thislist" is not carrying over with the varname of the player.

I'll repost the activation trigger created:

_trg=createTrigger["EmptyDetector",getPos _item];
_trg setTriggerActivation ["WEST", "PRESENT", true]
_trg setTriggerArea [10, 10, 0, false] 
_trg setTriggerStatements ["this", "[color="Red"][{[_x,_item] exec ""buildings\barracksrecruit.sqs""} foreach thislist][/color]", ""]

I've tested the second script to have it produce the name of the player, but it comes blank.

So it makes be believe that the unit entering the trigger is not being properly inserted into _x. :confused:

---------- Post added at 04:15 AM ---------- Previous post was at 04:03 AM ----------

I'll amend that. The barracks (_item in the trigger activation) is not being inserted either. I changed the script to use the actual ID for my character and it still didnt work.

So _obj in the second script is not working properly.

---------- Post added at 04:26 AM ---------- Previous post was at 04:15 AM ----------

Another possibility would be to completely forget adding/removing the actions every time and just add them once and give them a condition.

Full syntax for addAction

I'm attempting this syntax, and it does not appear to work. The action does not appear at all.

Here is how I have it typed:

R1=(General addaction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"R1","(general distance _item)<10])

What is wrong with it?

---------- Post added at 06:01 AM ---------- Previous post was at 04:26 AM ----------

Okay, I think I've narrowed it down.

The problem is that _item is being literally placed in the ONACT line of the created trigger. So the local variable _item is put in a place that a global would only work.

So how can I work around this?

Share this post


Link to post
Share on other sites

I'm sorry to ask, but did you try my suggestion regarding the trigger statement?

If not, please do.

You could also verify the trigger statement by doing

hint format ["%1",triggerStatements _trg]; //triggerStatements = Arma 2 only

Share this post


Link to post
Share on other sites
I'm getting some great suggestions, I planon trying them out.

But the problem is related to the "_x foreach thislist" is not carrying over with the varname of the player.

Almost... but not entirely true. The _x should be carrying over just fine but I doubt the "_item" will.

Variables with _ are private variables in scripts... it doesn't matter if you create the trigger withing the script, the trigger is still not part of it so the _item var will not carry over. So if you need _item to be variable, (several baracks) then put them in an public-array and only pass the index to the trigger by using "format".

Aside from that, have a look at what dawg said, addactions with conditions are a great thing and will surely make this easier.

Share this post


Link to post
Share on other sites
I'm attempting this syntax, and it does not appear to work. The action does not appear at all.

Here is how I have it typed:

R1=(General addaction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"R1","(general distance _item)<10])

What is wrong with it?

The local variable _item is not defined in the action condition space. You could try this:

(_item addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","(general distance _target)<10"]

Share this post


Link to post
Share on other sites

Thanks so much for the suggestions Taurus.

I've tried your format method in a syntax like this, for a different, but similar script in which I try to use both _x and the vehiclevarname _item like this:

_onActstr = format["{[_x,%1] exec ""buildings\WGdetect2.sqs""} foreach thislist",vehiclevarname _item];
_trg setTriggerStatements ["this", _onActstr, ""];

When I enter Preview, the hint text says:

[_x,] exec "buildings\WGdetect2.sqs"

---------- Post added at 12:00 PM ---------- Previous post was at 11:56 AM ----------

Almost... but not entirely true. The _x should be carrying over just fine but I doubt the "_item" will.

Variables with _ are private variables in scripts... it doesn't matter if you create the trigger withing the script, the trigger is still not part of it so the _item var will not carry over. So if you need _item to be variable, (several baracks) then put them in an public-array and only pass the index to the trigger by using "format".

Thanks very much for the clear explanation. However I am unfamiliar with the public-array/index method you suggested.

Do you have any examples?

Share this post


Link to post
Share on other sites
Thanks so much for the suggestions Taurus.

I've tried your format method in a syntax like this, for a different, but similar script in which I try to use both _x and the vehiclevarname _item like this:

Did you give _item a vehicleVarName?

Share this post


Link to post
Share on other sites
The local variable _item is not defined in the action condition space. You could try this:

(_item addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","(general distance _target)<10"]

Thanks man, but no dice on this end.

I've cheated and found away to set a new local variable for the barracks. Other than that this doesn't seem to work:

_barr = nearestobject [general,"USMC_warfareBbarracks"]
(_barr addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","(general distance _barr)<10"]

---------- Post added at 12:17 PM ---------- Previous post was at 12:12 PM ----------

Did you give _item a vehicleVarName?

I don't know if I can. The _item variable is from a script that is run after the creation of a new building in the Construction manager. Like so:

_item = _this select 0

?(_item iskindof "USMC_warfareBbarracks"): goto "barracks"
?(_item iskindof "USMC_WarfareBLightFactory"): goto "lgtfact"
?(_item iskindof "USMC_WarfareBFieldhHospital"): goto "hospital"
?(_item iskindof "USMC_WarfareBAntiAirRadar"): goto "radar"
?(_item iskindof "USMC_WarfareBArtilleryRadar"): goto "groundradar"


exit

#barracks
wbarrdeploy=true
publicvariable "wbarrdeploy"
hint "YOU BUILT BARRACKS"
_trg=createTrigger["EmptyDetector",getPos _item];
_trg setTriggerActivation ["WEST", "PRESENT", true]
_trg setTriggerArea [10, 10, 0, false] 
_trg setTriggerStatements ["this", "[{[_x] exec ""buildings\barracksrecruit.sqs""} foreach thislist]", ""]

Should I redefine _item in this script somehow?

Share this post


Link to post
Share on other sites
Thanks man, but no dice on this end.

I've cheated and found away to set a new local variable for the barracks. Other than that this doesn't seem to work:

_barr = nearestobject [general,"USMC_warfareBbarracks"]
(_barr addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","(general distance _barr)<10"]

Again, the same problem. The variable _barr will not work if you use it inside the action condition. You are limited to _this, _target, and global variables. The wiki explains what _this and _target are.

_barr = nearestobject [general,"USMC_warfareBbarracks"];
(_barr addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","(general distance _target)<10"]

---------- Post added at 08:19 AM ---------- Previous post was at 08:17 AM ----------

I don't know if I can. The _item variable is from a script that is run after the creation of a new building in the Construction manager. Like so

You can use setVehicleVarName to set the vehicleVarName in a script.

Share this post


Link to post
Share on other sites
Again, the same problem. The variable _barr will not work if you use it inside the action condition. You are limited to _this, _target, and global variables. The wiki explains what _this and _target are.

_barr = nearestobject [general,"USMC_warfareBbarracks"];
(_barr addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","(general distance _target)<10"]

I've just tried that exact syntax and not getting any results. :confused:

You can use setVehicleVarName to set the vehicleVarName in a script.

Wouldn't that cause conflicts, as the first script that is run for every building that is constructed will use that variable?

Edited by Impavido

Share this post


Link to post
Share on other sites
I've just tried that exact syntax and not getting any results.

Your problem is probably with nearestObject. Try confirming that _barr properly references the object you are expecting it to.

Wouldn't that cause conflicts, as the first script that is run for every building that is constructed will use that variable?

You can increment it or something each time, keeping track of it with a global variable or something.

Share this post


Link to post
Share on other sites
Your problem is probably with nearestObject. Try confirming that _barr properly references the object you are expecting it to.

I put a 10 second delay and "deletevehicle _barr" and it was deleted. So the variable seems to work.

You can increment it or something each time, keeping track of it with a global variable or something.

do you know if something like:

_item setvehiclevarname format ["BULDINGNUMBER%1",count]

will work?

Share this post


Link to post
Share on other sites

Ok now just read this please.:

the "_barr" does work. BUT ONLY inside the script.

The trigger you've created is OUTSIDE of the script, therefor it does not have access to the _barr variable.

Just for the sake of testing, remove the "_" so the variable is named just "barr" and try it again. If you need to have multiple barracks, read my other post.

ps.: have a look at this:

http://community.bistudio.com/wiki/Variables#Local_Variables

Edited by Tajin

Share this post


Link to post
Share on other sites
Ok now just read this please.:

the "_barr" does work. BUT ONLY inside the script.

The trigger you've created is OUTSIDE of the script, therefor it does not have access to the _barr variable.

Just for the sake of testing, remove the "_" so the variable is named just "barr" and try it again. If you need to have multiple barracks, read my other post.

ps.: have a look at this:

http://community.bistudio.com/wiki/Variables#Local_Variables

I appreciate your patience greatly, but frankly I still don't understand the process you are describing here:

So if you need _item to be variable, (several baracks) then put them in an public-array and only pass the index to the trigger by using "format".

I understand the basics of using format, but can you please show an example of putting _item in a public array?

Share this post


Link to post
Share on other sites

Sorry I forgot to fix some syntax errors you had.

_barr = nearestobject [general,"USMC_warfareBbarracks"];
_barr addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","_this == general && (_this distance _target)<10"];

Should work.

Share this post


Link to post
Share on other sites
Sorry I forgot to fix some syntax errors you had.

_barr = nearestobject [general,"USMC_warfareBbarracks"];
_barr addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","_this == general && (_this distance _target)<10"];

Should work.

:(

This makes me feel like a complete ass, since you've been so great about trying to help Dawg...

but that isn't working either. The action just doesn't show up.

Share this post


Link to post
Share on other sites

How recently was the barracks created? When you createVehicle stuff sometimes actions attached to it don't become visible until the player "knowsAbout" it. Try this:

_barr = nearestobject [general,"USMC_warfareBbarracks"];
_barr addAction ["RECRUIT Recon SMAW ($2000)","recruit\wgen\SMAW.sqs", [_obj],false,true,"","_this == general && (_this distance _target)<10"];
general reveal _barr;

Also, make sure you (the player) are this 'general' guy, and that you're within 10m of the barracks, and that you are looking at the barracks (your cursor needs to be over any visual part of it).

Edited by Big Dawg KS

Share this post


Link to post
Share on other sites

Edit: - the contents of this post have been flagged "obsolete" -

Edited by Tajin

Share this post


Link to post
Share on other sites

Tajin, read the Wiki entry for addAction. It says (and I have tested this to confirm it's true) that there are two reserved variables in an action condition; _this and _target. The variable _this refers to the unit who "sees" the action (ex, the player), and _target is the object/unit/whatever that the action is added to.

This also means your example won't work because in that space, _this would not be an array.

Share this post


Link to post
Share on other sites

Ok, thats new. Not very consequent, but interesting nevertheless. ;)

ps.: I must confess I still haven't used the addAction-conditions but that's just because I'm not using addactions very often anyway. ^^

Share this post


Link to post
Share on other sites

That reveal command doesn't seem to work either Dawg.

I really appreciate your help man, but I'm starting to think I need to drop this recruit thing until something more effective comes to me. Hell, maybe this is the frustration I need to force myself to learn SQF functions.

However, can you or anybody explain what Tajin is talking about when he suggests putting the local variable into a public array?

I may or may not know what he is suggesting, the problem is I don't always understand the vernacular.

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  

×