Author Topic: To be true or not to be true, that is the question..  (Read 2140 times)

0 Members and 1 Guest are viewing this topic.

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
To be true or not to be true, that is the question..
« on: September 15, 2010, 03:25:12 pm »
Hola, error detected.

• I am doing a loop, something like that
Code: (pascal) [Select]
var
variable1: array of boolean;

procedure OnActivateServer();
var i: byte;
begin
 SetArrayLength(variable1,{Some info loaded in a .ini file(it works)}+1);
end;

{An action is done (through a procedure) and changes one by one variable1[x] to true }
Procedure iWantThemTrue();
var
i : byte;
begin
 for i := 1 to ArrayHigh(variable1) do begin
    if variable1[i] = true then begin // HERE IS THE ERROR **
     variable1[i] := false;
    end;
 end;
end;

• My error is that on the
Code: [Select]
if variable1[i] = true then begin it doesn't check for ALL the the numbers. It only checks if one of them is true
And when it thinks only one if true, it only checks for one so it does some weirds errors.

• How could i do something that should check if ALL my variables are true, not only ONE (i tried without the .ini var, did the same thing, my .ini var is alright)

I've been trying to debug it for a few weeks and no results, that would be nice to help me. Thanks!
« Last Edit: September 15, 2010, 03:51:47 pm by VinceBros »

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: To be true or not to be true, that is the question..
« Reply #1 on: September 15, 2010, 03:51:30 pm »
Quote
if variable1 = true then begin
you don't have to use '= true', if variable then is enough

Quote
How could i do something that should check if ALL my variables are true, not only ONE (i tried without the .ini var, did the same thing, my .ini var is alright)

Code: (pascal) [Select]
var b: boolean;

b := false;
for i := 1 to ArrayHigh(variable1) do begin 
   if variable1[i] then begin // HERE IS THE ERROR ** 
    variable1[i] := false;
    b := true;
   end; 
end;

if not b then begin
  // if b is false then all vars are true
end;

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: To be true or not to be true, that is the question..
« Reply #2 on: September 15, 2010, 05:37:00 pm »
Sorry, but your option doesn't work. I shall try something else..

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: To be true or not to be true, that is the question..
« Reply #3 on: September 15, 2010, 11:05:44 pm »
Code: [Select]
var
bools: array of boolean;

function CheckBoolArray(): boolean;
var i: byte;
begin
result := true;
for i := 0 to getarraylength(bools)-1 do if not bools[i] then begin
result := false;
break;
end;
end;


Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: To be true or not to be true, that is the question..
« Reply #4 on: September 16, 2010, 08:00:08 am »
Having "= true" is unnecessary (except when comparing a variant value, such as those returned by GetPlayerStat for example).

Judging by what your comment says ("An action is done (through a procedure) and changes one by one variable1[x] to true"), the procedure you wrote should do the opposite of what you want (that is, set all elements of variable1 to false).

Code: [Select]
if (some_var = true) then
  some_var := false;
is equivalent to simply:
Code: [Select]
some_var := false;since they do the same thing
(assuming some_var is a boolean)
Again, the if statement does not need the "= true" in there.

Dynamic arrays (var somevar: array of ____) index starting at zero. That is why he did "for i := 0"
I personally had issues with ArrayHigh and dynamic arrays at one point in time (that was a long time ago, it could have simply been a silly mistake) and now always use GetArrayLength, and never ArrayHigh.

Random note: "if (a = false) then" is equivalent to "if (not a) then" (although if a is a variant, the second will not work afaik).

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: To be true or not to be true, that is the question..
« Reply #5 on: September 16, 2010, 08:37:17 am »
Quote
except when comparing a variant value, such as those returned by GetPlayerStat for example.
(...)
although if a is a variant, the second will not work afaik.
It will work, but some people say it causes access violations and other errors (never happened to me though)

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: To be true or not to be true, that is the question..
« Reply #6 on: September 16, 2010, 05:30:23 pm »
My first post was just an exemple, i never use ArrayHigh. And i don't use ArrayLength anyways..

Well i've tried everything. All you've told me and it still only checks for one..

- Some of you didn't seem to understand..

Code: [Select]
if var1[i] then begin // if ALL the var1 booleans (var[1],var[2],var[3],etc.) are true then begin
var[i] := false; // make them false back again
« Last Edit: September 16, 2010, 05:31:54 pm by VinceBros »

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: To be true or not to be true, that is the question..
« Reply #7 on: September 16, 2010, 11:04:03 pm »
post the whole loop of the original code and stop making us try to diagnose cancer by a protograph

Offline Gizd

  • Flagrunner
  • ****
  • Posts: 586
  • (Re)tired
    • Eat-this! community site
Re: To be true or not to be true, that is the question..
« Reply #8 on: September 16, 2010, 11:19:28 pm »
Code: [Select]
b:= true;
for i:= 1 to n do b:= (var[i] and b);
if b then for i:= 1 to n do var[i]:= false;

DarkCrusade

  • Guest
Re: To be true or not to be true, that is the question..
« Reply #9 on: September 17, 2010, 06:43:38 am »
It will work, but some people say it causes access violations and other errors (never happened to me though)

That's what makes iMod crash so often.

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: To be true or not to be true, that is the question..
« Reply #10 on: September 18, 2010, 04:25:04 am »
are you sure about for i := 1? not 0?
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: To be true or not to be true, that is the question..
« Reply #11 on: September 18, 2010, 12:26:15 pm »
I tried it. On my exemple i am using 1 to "number" and 0 isn't used, at first i thought it could have been the error, but no.

Everythin' given hasn't worked yet.. I guess i gotta give up lol

(Happy Gizd?)
« Last Edit: September 18, 2010, 07:33:10 pm by VinceBros »

Offline Gizd

  • Flagrunner
  • ****
  • Posts: 586
  • (Re)tired
    • Eat-this! community site
Re: To be true or not to be true, that is the question..
« Reply #12 on: September 18, 2010, 02:07:32 pm »
Nothin' given hasn't worked yet..
Logically, that states that something has worked.

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: To be true or not to be true, that is the question..
« Reply #13 on: September 19, 2010, 10:13:00 am »
Well i just type failed, i bet you aren't perfect either.

I'll try to find other ways to solve it, thanks anyways guys!
_________________________________________________

Heya, i finally made it work, i just added another variable, a integer, and each time it adds var[a] = true, it adds + 1 to varint!

I got another question though, with a picture

As you can see, when the player joins the game it spams something like that... Here is the code/part:
Code: [Select]
procedure OnJoinTeam(ID, Team: byte);
begin
    if (Mode.Playing = false) and (Player[ID].speccing = true) then begin
    Command('/setteam5 ' + IntToStr(ID));
    WriteConsole(ID,'There is no player running, you can type !play and run.',Color);
 end;
 
if (Mode.Playing = true) and (Player[ID].speccing = true) then begin
         Command('/setteam5 ' + IntToStr(ID));
    WriteConsole(ID,'There is a player running, you can type !play and run when he''s done.',Color);
         Player[ID].Speccing := true;
    end;
end;

I really tried everything and we mustn't get rid of Mode.Playing, because you can only go in the game on Alpha team and THAT with a command (!play).


Thanks!
« Last Edit: September 19, 2010, 10:15:44 am by VinceBros »

DarkCrusade

  • Guest
Re: To be true or not to be true, that is the question..
« Reply #14 on: September 19, 2010, 10:36:01 am »
Everytime the procedure triggers it'll check his state and put him into team 5 again, what'll trigger the procedure again and again and again.

Try figuring the rest out yourself

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: To be true or not to be true, that is the question..
« Reply #15 on: September 19, 2010, 10:45:59 am »
K kewl, i just randomly fixed it :)