Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:29:10+00:00 2026-06-07T03:29:10+00:00

I am trying to return a value of a function to tell me if

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:29:11+00:00Added an answer on June 7, 2026 at 3:29 am

    Like @SertacAkyuz said, the problem is your use of IndexOfName(). You need to use IndexOf() instead.

    There is another bug in your CheckLocation() function. You are ignoring all of the input parameters other than canfly. If canwater or canlava were True, your function would return False on any water/lava location. You need to check if the location actually matches the Player’s capabilities. If canwater is true, there is no need to check if the location is water. Likewise with lava.

    Try this instead:

    canwater := (FMyPlayers.Player[i].Values['water'] = 'yes');
    canlava := (FMyPlayers.Player[i].Values['Lava'] = 'yes');
    canfly := (FMyPlayers.Player[i].Values['fly'] = 'yes'); 
    cansnow := true; 
    if not CheckLocation(position, cansnow, canlava, canwater, canfly) then 
      Exit; 
    

    .

    function TBaseGameForm.CheckLocation(position: TPoint; cansnow, canlava, canwater, canfly: Boolean): Boolean; 
    var
      loc: String;
    begin 
      Result := True;
      if (not canfly) and ((not canwater) or (not canlava)) then
      begin
        loc := Format('x%d%d', [Position.X, Position.Y]);
    
        if (not canwater) and (FGamePlay.waterLocations.IndexOf(loc) <> -1) then
        begin
          Showmessage('Cant move there due to water'); 
          Result := False;
          Exit;
        end;
    
        if (not canlava) and (FGamePlay.LavaLocations.IndexOf(loc) <> -1) then
        begin
          Showmessage('Cant move there due to lava'); 
          Result := False;
          Exit;
        end;
      end; 
    end; 
    

    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:

    [0]  x4y7
    [1]  x5y8
    
    loc := Format('x%dy%d', [Position.X, Position.Y]);
    

    Personally, I wouldn’t use a TStrings to hold integer values like this at all. I would use a TList of TPoint instances instead, eg:

    waterLocations: TList;
    
    function FindLocation(List: TList; Position: TPoint): Integer;
    begin
      for Result := 0 to List.Coun-1 do
      begin
        with PPoint(List[Result])^ do
        begin
          if (X = Position X) and (Y = Position.Y) then Exit;
        end;
      end;
      Result := -1;
    end;
    

    .

    if (not canwater) and (FindLocation(FGamePlay.waterLocations, Position) <> -1) then
    begin
      Showmessage('Cant move there due to water'); 
      Result := False;
      Exit;
    end;
    

    Or, if you are using a modern Delphi version that supports Generics, a TList<TPoint>:

    waterLocations: TList<TPoint>;
    
    function FindLocation(List: TList<TPoint>; Position: TPoint): Integer;
    begin
      for Result := 0 to List.Coun-1 do
      begin
        with List[Result] do
        begin
          if (X = Position X) and (Y = Position.Y) then Exit;
        end;
      end;
      Result := -1;
    end;
    

    .

    if (not canwater) and (FindLocation(FGamePlay.waterLocations, Position) <> -1) then
    begin
      Showmessage('Cant move there due to water'); 
      Result := False;
      Exit;
    end;
    

    Or even a single TDictionary that stores the type of location that any given X/Y coordinate specifies:

    type
      locType = (locLand, locWater, locLava, locSnow);
    
    locations: TDictionary<TPoint, locType>;
    

    .

    function TBaseGameForm.CheckLocation(position: TPoint; cansnow, canlava, canwater, canfly: Boolean): Boolean; 
    var
      loc: locType;
    begin 
      Result := True;
      if not canfly then
      begin
        locations.TryGetValue(Position, loc);
        case loc of
          locWater: begin
            if not canwater then
            begin
              Showmessage('Cant move there due to water'); 
              Result := False;
            end;
          end;
          locLava: begin
            if not canlava then
            begin
              Showmessage('Cant move there due to lava'); 
              Result := False;
              Exit;
            end;
          end;
        end;
      end; 
    end; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to return the value that a $ajax call returns, from a function
I am trying to return the value by using unix_timestamp function but it behaves
I'm trying to return a value from (transport) to the calling function, but can't
I am trying to make a function that can return the prime factors of
I am trying to fix outofMemory in my application that has a function as
I am trying to return a value (ID), matching another field (Type) in the
declaration of textfield1 <td nowrap=nowrap><input type=text name=textfield1 id=textfield1/></td> trying to return the value entered
I am trying to use the return value(response) from the callback outside the jQuery.post
I am trying to do 2 things: get a return value from a C
I am trying to return two json sets from java which each contain key/value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.