Jump to content
Sign in to follow this  
dmarkwick

Simple code example doesn't work?

Recommended Posts

Maybe it's me, but I cannot get a simple bit of code like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">X = X + Y;

to work. I've got to pad it out like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">Z = X + Y;

X = Z;

No biggie, but it gets me every time. Why would this be like it is?

Share this post


Link to post
Share on other sites

I use that equation all the time, I assume you've doublechecked with a format-hint or some other manner, that you're actually trying to add two numbers together? tounge2.gif usually my errors with simple things like that not working is either an uninitialized or badly assigned variable...

if you can't figure it out, you could always leak your code...

Share this post


Link to post
Share on other sites
I use that equation all the time, I assume you've doublechecked with a format-hint or some other manner, that you're actually trying to add two numbers together? tounge2.gif usually my errors with simple things like that not working is either an uninitialized or badly assigned variable...

if you can't figure it out, you could always leak your code...

I had a hint showing the time/brightness left for a light object as it slowly burns out, but it never decreased. I've seen this problem before elsewhere too, but I expect you're right about proper initialised variables. I just sort of assumed that a variable gets assigned/initialised the first time you use it, as long as you're not using it in a formula or something.

Share this post


Link to post
Share on other sites

it's a bit of an oddball, some command situations will automatically initialize a variable and some won't, it seems...

for example,

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_var = 5;

works flawless

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

if (true) then {_var = 5};

doesn't evaluate good (for me) tounge2.gif although it reports no errors. But put private ["_var"] above that if statement, and then it will work.

did you try a hint format ["%1 %2",light brightness,light brightness change] to make sure it's got the right data to work with? I think I'm just throwing the obvious out there, but sometimes the obvious gets overlooked crazy_o.gif

Share this post


Link to post
Share on other sites

Homer's problem there is caused by variable scope.

In his second code example, _var is local to the then code, meaning that it's not accessible outside the then code. In fact, it should be deleted once the then code is finished executing.

Share this post


Link to post
Share on other sites
Homer's problem there is caused by variable scope.

In his second code example, _var is local to the then code, meaning that it's not accessible outside the then code. In fact, it should be deleted once the then code is finished executing.

Wait - so any variable declared in a loop is local only to that loop?

Share this post


Link to post
Share on other sites

No.

You have to initialize a variable before an operation is performed on it.

--Ben

Share this post


Link to post
Share on other sites

I think the _ made it local to whatever scope it was created in... without the _ it would have worked in the whole script (without the Private command).

I think

Thanks Sanders for saying that; I'm pretty sure it clarified me up a bit on usage of the private cmd.

Share this post


Link to post
Share on other sites
Thanks Sanders for saying that; I'm pretty sure it clarified me up a bit on usage of the private cmd.

Actually, I assure you that it didn't clear up anything on the private command.

Declaring a variable without an underscore makes it global to the whole mission. Always.

Declaring a variable with an underscore makes it accessible only to the current scope and any scopes inside the current scope.

Using the private command allows you to declare a variable in the current scope, without regards to variables in a higher scope with the same name.

Take this code for example:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_x = 10;

if (true) then

{

private ["_x"];

_x = 5;

player sideChat format ["%1", _x];

};

player sideChat format ["%1", _x];

The first sidechat (innermost) returns 5. The second sidechat (outermost) returns 10. Note that if you try to declare a variable without an underscore (meaning it's global) with the private command, it will cause an error. Specifically: "Error Local variable in global space".

In this code:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

if (true) then

{

_x = 5;

};

player sideChat format ["%1", _x];

The sidechat will return boolean scaler junk.

In this code:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

if (true) then

{

private ["_x"];

_x = 5;

};

player sideChat format ["%1", _x];

The sidechat will return boolean scaler junk. This code is exactly the same as the code sample just above it, and in this case, the private is completely redundant. It serves no purpose.

I know that the whole scope concept is confusing to many of you guys with no structured coding experience prior to sqf. It's not that hard to get a grasp on though. If you want to be sure to avoid it all together, there's an easy way to do just that.

All you've got to do is declare all your variables in the script's outermost scope, such as at the top of the script. Example:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_x = 0;

_i = 0;

_j = 0;

while {_i < 5} do

{

while {_j < 5} do

{

_x = _x + 1;

_j = _j + 1;

};

_i = _i + 1;

};

player sideChat format["_x: %1, _i: %2, _j: %3", _x, _i, _j];

Returns:

"_x: 5, _i: 5, _j: 5"

That look wrong, but it isn't wink_o.gif. The reason is that I never reset _j to 0.

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  

×