Jump to content
thecoolsideofthepillow

Defined a Variable in one line, use it in the next; says undefined?

Recommended Posts

_droppedIntel = "Intel_File1_F" createVehicle (_this select 1);
systemChat str _droppedIntel;​

 

These are lines 4 and 5 of my script. Line 5 throws up an error saying that the "_droppedIntel" variable is undefined, when I am literally defining it in the previous line. What gives?!

Share this post


Link to post
Share on other sites

Well, the vehicle may not be actually created, it depends on what "(_this select 1)" is, so a little more context on your script would help.

Share this post


Link to post
Share on other sites

Are you using it in the editor? If so, then the editor will only accept global variables.

Also, as the above had said, its not really wise to put select there. Try this perhaps in your script:

_location = (_this select 0);
_droppedIntel = "Intel_File1_F" createVehicle [getPos _location];
systemChat str _droppedIntel;​
And since I am not on a PC at the moment, I don't want users braining me if my code is wrong or something...

Share this post


Link to post
Share on other sites

I see now... The vehicle isn't being created. No wonder.

 

How do I properly pass a created unit's name to a script through an event handler?

 

What seems to be happening is when I pass "this" to my script played on a unit's death, "this" is an array with the unit's name and some other name. Even if I select 1 (the unit's name), the selection still comes out to the same array as just "this." If I use the 0 selection, it gives me an error saying it was given an array and not a unit. I figured when it stopped giving me errors for creating the vehicle it was working.

 

What I am trying to do is have my random groups have one unit in them to carry some intel. That unit gets an event handler that, when killed, is *supposed* to drop a thing of documents at his corpse. I've been able to get the documents to drop once; but it was dropping at the same unit for every guy who held the documents because I was naming them without the underscore, so it would just make it appear on the last unit the random selection thing landed on after giving them the event handler.\

 

Here are my scripts as I currently have them written:

 

taskSetup.sqf
_grpSelect = enemyGroupTypes call BIS_fnc_selectRandom;
 
if (intelCount >= 3) then
{
 {
  _resgrp1 = [getMarkerPos _x, EAST, (_grpSelect)] call BIS_fnc_spawngroup;
  _holder = (units _resgrp1) call bis_fnc_selectRandom;
  _holderPos = getPos _holder;
  _holder addEventHandler ["killed", {_nil = [_holderPos] execVM "IntelStuff.sqf"}];
 } forEach selectedStart;
} else
{
 
};
{
 _tskName = format ["taskObj_%1", _x];
 _tsk = player createSimpleTask [_tskName];
 _tsk setSimpleTaskDescription ["Find and collect the intelligence in the area. Be prepared for hostile contact.", "Gather Intel", "Search Area for Intel"];
 _tsk setSimpleTaskDestination (getMarkerPos _x);
 _tsk setTaskState "Assigned";
  startTasks pushBack _tsk;
} forEach selectedStart;
 
IntelStuff.sqf
systemChat str _this;
_droppedIntel = "Intel_File1_F" createVehicle getPos _this;
_droppedIntel addAction ["Gather Intelligence",
 {
  deleteVehicle _droppedIntel;
  gatheredIntel = gatheredIntel + 1;
  if (gatheredIntel <= intelCount) then
  {
   player sideChat "SPEARHEAD to PHALANX. We have neutralized an insurgent team and gathered some documents, over.";
   sleep 3;
   PHALANX sideChat "Excellent work, SPEARHEAD. Proceed to locate the rest of the files. PHALANX out."
  } else
  {
   if (gatheredIntel >= intelCount) then
   {
    player sideChat "SPEARHEAD to PHALANX. We have collected all the files. Sending details now. How copy? Over.";
    sleep 10;
    PHALANX sideChat "Roger, SPEARHEAD. After going over the documents, we've discovered a Molatian officer we need you to arrest.";
    sleep 3;
    PHALANX sideChat "Do not kill the officer. Repeat DO NOT KILL THE MOLATIAN OFFICER. We need to interrogate him to find out what he may know.";
    sleep 3;
    PHALANX sideChat "How copy, SPEARHEAD? Over.";
    sleep 3;
    player sideChat "Copy, PHALANX. Capture, not kill, the HVT. SPEARHEAD out.";
    _tsk = player createSimpleTask ["HVT"];
    _tsk setSimpleTaskDescription ["Capture the officer for interrogation in Bolabongo. Beforewarned, Captain, the Molatians are swarming all over the city, and the target is without doubt accompanied by guards.", "Arrest HVTs", "Capture the Officer"];
    _tsk setSimpleTaskDestination (getMarkerPos "mkrHvt");
    _tsk setTaskState "Assigned";
   };
  };
 }];​

​

Share this post


Link to post
Share on other sites

Hey, I'd like to help but I have no idea what you're actually trying to achieve. A good write-up on asking programming/scripting questions:

http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question

 

It *sounds* as if you're adding the killed event handler to a unit and want the event handler code to create an intel object at the units position.

 

You can see that the killed event handler has two arguments: https://community.bistudio.com/wiki/Arma_3:_Event_Handlers#Killed

 

Arguments are passed as array _this. The first one is the dead unit:

_this select 0 // arrays use zero based indexes
The syntax of the createVehicle command you're using requires a classname and a position (https://community.bistudio.com/wiki/createVehicle). You have the classname already, but you're passing it an object rather than a position. To get the position of an object you can use getPos (https://community.bistudio.com/wiki/getPos)

Share this post


Link to post
Share on other sites
_droppedIntel = "Intel_File1_F" createVehicle (_this select 1);
systemChat str _droppedIntel;​

If this code is executed by an eventHandler, then _this select 1 would return a unit. However, createVehicle expects a position.

 

Please correct my if I got it wrong.

Share this post


Link to post
Share on other sites

The snippets you've shared provide some useful context. Ignoring the missing sections and assuming that they're all correct, this is your problem:

 

 

  _holderPos = getPos _holder;
  _holder addEventHandler ["killed", {_nil = [_holderPos] execVM "IntelStuff.sqf"}];
Edit:

Okay...not cool. So the forum deleted the second half of my post where I explained what the problem is and how you can approach fixing it.

This is way less useful to you but I don't have it in me to type that all out again:

_holder addEventHandler ["Killed", { _this execVM "IntelStuff.sqf"}];
Combine that with my previous post to fix your problem. Edited by SilentSpike

Share this post


Link to post
Share on other sites

What would I change there? I mean, I've tried having no _holderPos and just sending this (and _this; not at the same time) as an argument for execVM, I've also tried no arguments for execVM. I've tried every combination of this/_this select 0/1. They all have similar problems with either undefined variables or expecting the wrong thing for the position of the createVehicle command for the intel.

 

EDIT:

 

OMG. I don't know why that worked, Silent, but it did. I thought arguments for execVM had to be in [] brackets. Is this just a quirk of the event handler thing, or have I been using it wrong the whole time and only by coincidence has my stuff worked?

 

EDIT 2: Well, creating the intel works... Picking it up doesn't work 100%. It adds to your total, sends the radio message, but does not delete the thing, citing an undefined variable in _droppedIntel again.

 

EDIT 3: Ironically, it was the same issue as before. Got it working now. *whew*

Share this post


Link to post
Share on other sites

_this is the _holderpos in the executed script (not the [unit,killer] because it's not passed to it as argument).

Problem is that event handler code is a separate scope,  _holderpos is not defined there.

Share this post


Link to post
Share on other sites

Yeah, so this basically what I had originally explained in my post before the forum software decided it wasn't worth keeping  <_<

 

I thought arguments for execVM had to be in [] brackets. Is this just a quirk of the event handler thing, or have I been using it wrong the whole time and only by coincidence has my stuff worked?

 

Try thinking in terms of datatypes rather than syntax. The execVM command only has two parameters:

1 execVM 2
  1. Any type of data. The reason you commonly see square brackets is because that's the syntax to denote the array datatype (which can contain multiple datatypes as elements - so it's handy for passing multiple arguments in a single datatype).
  2. A string. This parameter isn't an actual script file, it's just a string which gets read and used as a path to a script file.

Thinking in this way helps to really build an understanding of the way coding languages are structured. So you can see that by using _this as the first parameter we're simply using an array datatype and there's nothing special about 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

×