Jump to content
daniel1060

Variable not appearing in hint

Recommended Posts

Right so I have a hint which displays a variable it works perfectly on a server that I am hosting on my machine but not on a dedicated server. The variable I am using is a global public variable. I have no idea why this is happening can anybody help?

 

Init.sqf

Spoiler

waitUntil {time > 0};
if (isDedicated) then {
cash = 0
publicVariable "cash";

};

 

atm.sqf

Spoiler

_cash = cash;
_Showcash = call {hint str format ["You have: $%1",str _cash]};

 

Any help?

 

Share this post


Link to post
Share on other sites

What does the hint show? Does it say 'any'?

Why do you str the format? Isn't that going to result in too many quotes?

Why don't you just hint the publicvariable instead?

Share this post


Link to post
Share on other sites

Sorry all the hint displays is "You have: $"

 

Hint screenshot

 

Also, I did have the public variable hinted but I changed it to check if that was causing another problem

Share this post


Link to post
Share on other sites

Probably because atm.sqf runs before the game starts (Time>0) or before cash is public. Try a waitUntil {time > 2} in this sqf.

Share this post


Link to post
Share on other sites

@daniel1060:

This:

 _Showcash = call {hint str format ["You have: $%1",str _cash]}; 

will never work, since _cash does not exist inside the call scope, which does not make sense in any way, why not just hint directly.

Some examples:

_test = "Test";
hint str _test; //will display "Test"

_test = "Test";
[] call {hint str _test};//will throw an error because _test does not exist here

_test = "Test";
_test call {hint str _this};//will hint "Test"

//Now this little gem:
_test = "Test";
_success = [] call {

_noTest = "Success";
_noTest
};

hint str _test;//will hint "Test"
hint str _noTest;//will throw an error because _noTest only exists inside the call
hint str _success;//will hint "Success" because _noTest is being returned by the call and available through the _success variable

Like I stated in another post, if you want to keep track of individual money a player has (according to your hint) you need to do it with setVariable/getVariable.

Also, what kk said.

 

Cheers

Share this post


Link to post
Share on other sites

This:

 _Showcash = call {hint str format ["You have: $%1",str _cash]}; 

will never work, since _cash does not exist inside the call scope, which does not make sense in any way, why not just hint directly.

Some examples:

 

Well it would work as _cash is equal to cash 

 

aswell KK I noticed that after I posted it here and tested it, it still didn't work.

 

The ATM.sqf script is run off an addAction from an NPC so it shouldn't need to wait until the server is older than 0

 

I'll try setting the init to waitUntil {time > 2} 

Share this post


Link to post
Share on other sites
38 minutes ago, daniel1060 said:

Well it would work as _cash is equal to cash 

It would not work.

_cash does not exist inside the call code. Reread the examples I gave above.

It's undefined. But believe what you want.

The entire line is nonsensical anyway.

There's no reason to use:

_Showcash = call {hint str format ["You have: $%1",str _cash]}; 

When you can simply do:

hint str format ["You have: $%1",str _cash]

inside the proper scope.

 

Cheers

Share this post


Link to post
Share on other sites
1 hour ago, Grumpy Old Man said:

It would not work.

_cash does not exist inside the call code. Reread the examples I gave above.

It's undefined. But believe what you want.

The entire line is nonsensical anyway.

There's no reason to use:


_Showcash = call {hint str format ["You have: $%1",str _cash]}; 

When you can simply do:


hint str format ["You have: $%1",str _cash]

inside the proper scope.

 

Cheers

 

Not exactly. You're right to say it's a waste of code calling something you can hint directly. But:

 

In fact, you can return a value with a call, in this case, because the lines are following with no delay.

Tested several times and several other circumstances:

 

place in a trigger cond: true (or simply run the code in an init field)

on act:

amount = 10;

_cash = amount;
_Showcash = call {hint str format ["You have: $%1",str _cash]};

You'll get : "you have: $10"

That works as if you passed the last local variable as argument. Don't ask me why.

 

Cheers

  • Thanks 1

Share this post


Link to post
Share on other sites
5 minutes ago, pierremgi said:

 

Not exactly. You're right to say it's a waste of code calling something you can hint directly. But:

 

In fact, you can return a value with a call, in this case, because the lines are following with no delay.

Tested several times and several other circumstances:

 

place in a trigger cond: true; (or simply run the code in an init field)

on act:

amount = 10;

_cash = amount;
_Showcash = call {hint str format ["You have: $%1",str _cash]};

You'll get : "you have: $10"

That works as if you passed the last local variable as argument. Don't ask me why.

 

Cheers

You're right, it works.

But it shouldn't. It's not even inside the right scope so it shouldn't be defined.

Something's off here.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, Grumpy Old Man said:

But it shouldn't. It's not even inside the right scope so it shouldn't be defined

Local variables are accessible in child scopes. That's why you private local variables, to un-associate them from any previous define.

h = [] spawn {
	_cash = 10;
	call {
		systemChat format[ "child non private: %1", _cash ];
	};
	call {
		private [ "_cash" ];
		systemChat format[ "child private: %1", _cash ];
		_cash = 20;
		call {
			systemChat format[ "child of private child: %1", _cash ];
		};
	};
	systemChat format[ "parent: %1", _cash ];
};

Would print...

child non private: 10

child private: any

child of private child: 20

parent: 10

 

any as the variable has been un-associated (private) from the previous definition and will throw an undefined error because it has no value (nil).

  • Like 4

Share this post


Link to post
Share on other sites

Excellent clarification,

I always assumed that private or params secures the variable in parents and child scopes, but not that call is also included.

Thanks for shedding some light on this!

 

Cheers

Share this post


Link to post
Share on other sites

@Larrow's example is bad, because it errors out in line 8 (used undefined variable used in scheduled environment).

The example would work without error if line 1 and the last line were removed.

 

>I always assumed that private or params secures the variable in parents and child scopes, but not that call is also included.

That is exactly what a child scope is. Everything that could be encapsulated in curly brackets is a scope.

 

 

Share this post


Link to post
Share on other sites
8 minutes ago, commy2 said:

@Larrow's example is bad, because it errors out in line 8 (used undefined variable used in scheduled environment).

The example would only work if line 1 and the last line were removed.

 

That was the whole point if you read the last sentence. It was done on purpose to show that the variable was totally isolated from its previous definition yet was still undefined as it had no value (was nil).

The whole thing still runs and prints as stated when run from the debugConsole, you just get an undefined warning.

  • Like 2

Share this post


Link to post
Share on other sites
On 21/04/2017 at 6:00 AM, pierremgi said:

waitUntil {time > 2}

waitUntil {!isNil {cash}};

 

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

×