Jump to content

Recommended Posts

 

The following does not work and I can't figure out why:

if (_deathSound != "NONE") then {
	_soundPath = [(str missionConfigFile), 0, -15] call BIS_fnc_trimString;
	_deathSoundPath = _soundPath + "Sound\" + _deathSound + ".ogg";
} else {
	_deathSoundPath = "NONE";
};

systemChat "Path:";
systemChat _deathSoundPath;

SystemChat prints just the string, the _deathSoundPath is not defined. The inside of the condition works just fine when alone and I guess I could just leave it since playSound3D doesn't return an error when given a non-existing file but it still bugs me.

The following works:

if (_deathSound != "NONE") then {
	_soundPath = [(str missionConfigFile), 0, -15] call BIS_fnc_trimString;
	deathSoundPath = _soundPath + "Sound\" + _deathSound + ".ogg";
} else {
	deathSoundPath = "NONE";
};

_deathSoundPath = deathSoundPath;

systemChat "Path:";
systemChat _deathSoundPath;

which would imply scope problems but conditions shouldn't create their own scopes, no?

Share this post


Link to post
Share on other sites

First thing I would do would be to private/param the local variables, as you've indicated towards. Looks like that's why you're getting an undefined variable error.

 

See "Ceebs" comment, it's the first one:

https://community.bistudio.com/wiki/if

  • Like 1

Share this post


Link to post
Share on other sites

If you define the local variable inside the if scope, it will only exist in it, not outside of it afterwards.
So define it before:

_deathSoundPath = "";

if (_deathSound != "NONE") then { .....

 

  • Like 2

Share this post


Link to post
Share on other sites
4 minutes ago, beno_83au said:

First thing I would do would be to private/param the local variables, as you've indicated towards. Looks like that's why you're getting an undefined variable error.

 

See "Ceebs" comment, it's the first one:

https://community.bistudio.com/wiki/if

Thanks, incredible that I've been ignorant of this until now.

2 minutes ago, Greenfist said:

If you define the local variable inside the if scope, it will only exist in it, not outside of it afterwards.
So define it before:

_deathSoundPath = "";

if (_deathSound != "NONE") then { .....

 

Also, there's a global var there too: deathSoundPath

Also thanks. The global was there in the second example just for testing.

  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, theend3r said:

No dramas (I buggered up the post edit........)

Share this post


Link to post
Share on other sites

As other have said your _deathSoundPath is defined inside the IF statement so either

private, param or define the variable before hand so its available outside of the IF statement

or as Then is a command that takes an IF Type for the left hand operand and Code as the right and returns the result of the code, you can use this return from the IF statement to define the variable

_deathSoundPath = if (_deathSound != "NONE") then {
    _soundPath = [(str missionConfigFile), 0, -15] call BIS_fnc_trimString;
    format[ "%1Sound\%2.ogg", _soundPath, _deathSound ]
} else {
    "NONE"
};

systemChat "Path:";
systemChat _deathSoundPath;

Share this post


Link to post
Share on other sites

Also, way to get mission root path:

private _missionRoot = str missionConfigFile call {_this select [0, count _this - 15]};

 

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

×