I have
playerIds : Array[0..500] of string;
function isPlayerMob(check : string): boolean;
var
i : integer;
begin
for i := 0 to 500 do
begin
if ((playerIds[i] <> '') and (playerIds[i] = check)) then
begin
result := true;
end;
end;
result := false;
end;
I get the Warning
Hint: Value assigned to 'isPlayerMob' never used
Can somebody tell me how to fix this? The error is for the
result := true;
As others have told you, the value your loop is assigning to Result is being discarded because you are not exiting the function before the final assignment to Result, so it does not matter what the loop assigns.
You could assign the Result with an initial value and then reassign it as needed, or you could simply Exit after you have assigned your desired value:
function isPlayerMob(check : string): boolean;
var
i : integer;
begin
for i := 0 to 500 do
begin
if ((playerIds[i] <> '') and (playerIds[i] = check)) then
begin
Result := True;
Exit; // <-- add this
end;
end;
Result := False; // <-- only performed if the loop does not find a match
end;
Or, if you are using a recent Delphi version:
function isPlayerMob(check : string): boolean;
var
i : integer;
begin
for i := 0 to 500 do
begin
if ((playerIds[i] <> '') and (playerIds[i] = check)) then
Exit(True); // <-- sets Result and exits at the same time
end;
Result := False; // <-- only performed if the loop does not find a match
end;
This hint is because your always are assigning the false value to the function. no matter if the value if found in the loop.
try this
function isPlayerMob(const check : string): boolean;
var
i : integer;
begin
result := false;
for i := 0 to 500 do
if ((playerIds[i] <> '') and (playerIds[i] = check)) then
begin
result := true;
break;
end;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With