I am trying to return a value of a function to tell me if a location has Lava or Water at that location.
The issue is Even if it has lava or water there, it still returns true.
The values for water,lava,and fly are from a players creature’s list. It is just getting if that creature has the ability of water,lava,or can fly. And i just mark them as true /false accordingly.. Position is a TPOINT. It loads all this into a function and as long as the function CheckLocation returns true then its ok location
if FMyPlayers.Player[i].Values['water']='yes' then
canwater := true;
if FMyPlayers.Player[i].Values['Lava']='yes' then
canlava := true;
if FMyPlayers.Player[i].Values['fly']='yes' then
canfly := true;
cansnow := true;
if checkLocation(position,cansnow,canlava,canwater,canfly) = False then
exit;
function TBaseGameForm.checkLocation(position :Tpoint;Cansnow,canlava,canwater,canfly:bool):Bool;
begin
RESULT := True;
if canfly = true then
RESULT := true
else begin
if FGamePlay.waterLocations.IndexOfName('x'+inttostr(Position.x)+inttostr(Position.Y)) <> -1 then begin //Check location
Showmessage('Cant move there due to water');
RESULT := FALSE;
end;
if FGamePlay.LavaLocations.IndexOfName('x'+inttostr(Position.x)+inttostr(Position.Y)) <> -1 then begin //Check location
Showmessage('Cant move there due to lava');
RESULT := False;
end;
end;
end;
when running i check the values in the WaterLocations
[0] x47
[1] y47
[2] x58
[3] y58
The value of Position.x and Position.y are (4,7) Thus it should return false cause x47 is on the list.
Like @SertacAkyuz said, the problem is your use of
IndexOfName(). You need to useIndexOf()instead.There is another bug in your
CheckLocation()function. You are ignoring all of the input parameters other thancanfly. Ifcanwaterorcanlavawere True, your function would return False on any water/lava location. You need to check if the location actually matches the Player’s capabilities. Ifcanwateris true, there is no need to check if the location is water. Likewise with lava.Try this instead:
.
With that said, I agree with @sarnold that your coordinate system needs some tweaking. It is fine only as long as your x/y coordinates are both single digits. But if they are multiple digit, it will not work. At the very least, you should prefix the Y coordinates, eg:
Personally, I wouldn’t use a
TStringsto hold integer values like this at all. I would use aTListofTPointinstances instead, eg:.
Or, if you are using a modern Delphi version that supports Generics, a
TList<TPoint>:.
Or even a single
TDictionarythat stores the type of location that any given X/Y coordinate specifies:.