Jump to content
arcvision

Error: Undefined Variable in expression

Recommended Posts

Hello! I'm learning scripting and do simple scripts.

But I have error in my random inventory script.

Here is my script:

_navlist = ["ItemCompass", "ItemGPS", "ItemMap", "ItemRadio", "ItemWatch"];
_rnd = random 9;
if (_rnd >= 6) then {_nav = _navlist select floor random count _navlist};
player addWeapon [_nav, 1];

Here is error:

'...ndom count _navlist};
player addWeapon [|#|_nav, 1];'
Error addWeapon: Undefined Variable in expression: _nav
File ..\RndInv.sqf, line 4

Please help me.

Share this post


Link to post
Share on other sites

addItem for Items. _nav is undefined since you only define it if it's 6 or more since the incorrect addWeapon is outside the if statement.

navlist = ["ItemCompass", "ItemGPS", "ItemMap", "ItemRadio", "ItemWatch"];  
_randomRoll = random 9;
_randomItem = navList select (floor (random count navlist));
if (_randomRoll >= 6) then {
   player addItem _randomItem;
   player assignItem _randomItem;
};

Share this post


Link to post
Share on other sites

Your code is working, thank you.

But why if I use "if" with "else", I get the same error.

Variable "_nav" must be defined at all times.

_navlist = ["ItemCompass", "ItemGPS", "ItemMap", "ItemRadio", "ItemWatch"];
_rnd = random 9;
if (_rnd >= 6) then {_nav = _navlist select floor random count _navlist} else {_nav = ""};
player addItem _nav;
player assignItem _nav;

Share this post


Link to post
Share on other sites

Because _nav gets created inside the if structure. You can avoid this by putting this line at the top of your script:

private ["_nav"];

Then the rest of the script will recognize the local variable, _nav, when it's created inside a control structure like if. However you have a bigger problem because if the random number doesn't match then you're trying to add "" (nothing) as an item and you get an even bigger error.

Share this post


Link to post
Share on other sites

Okay, thank you for helping.

Now I know a little more about scripts, thanks to you.

Share this post


Link to post
Share on other sites

Great to hear! As you run into more questions, feel free to ask.

  • Like 1

Share this post


Link to post
Share on other sites

Hello, I have the same problem with this script. You can help me?

 

 

// by ALIAS
if (!isServer) exitWith {};

_check_time = this select 0;

curr_time = false;
publicVariable "curr_time";

if (_check_time>0) then 
    {
    sleep _check_time;
    curr_time = true;
    publicVariable "curr_time";
    };

 

 

the error say that the variable is not defined in the expresion:this

in the third line: _check_time = this select 0;

Share this post


Link to post
Share on other sites
3 hours ago, Hekhy said:

Hello, I have the same problem with this script. You can help me?

 

We need more context. How is the script being called? Is that the entire script? If not, please share it.

Share this post


Link to post
Share on other sites

The _ is missing from _this select 0

  • Like 1

Share this post


Link to post
Share on other sites


@Hekhy
 

Please read our rules
(see my signature for links, they can also be found under Guidelines in the Nav bar)


9) Do not cross-post

Do not post duplicate threads in more than one forum simply to get an answer quicker or to draw more attention to your post.
 

AND
 

10) Do not dig up old threads

Threads older than 4 months should not be dug up unless something significant is being added. If in doubt as to what is "significant", contact a moderator and they will give you their opinion. As always old threads will remain open or be closed at the moderator's discretion.

Your other post has been deleted. 
 

Share this post


Link to post
Share on other sites

Thank you very much mate, this fix my error easy. I am so dissapointed for don't see this error.

Thank you another time and also Harzach for take the time for answer.

 

Sorry Fallujah, this won't happen again.

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

×