Forum

> > CS2D > Scripts > Check if tile is water
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Check if tile is water

9 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Check if tile is water

Mami Tomoe
User Off Offline

Zitieren
How does one check if a tile is wet (water)?
I know of
tile(tx, ty, 'property') == 14
but that doesn't work properly when using dynamic floors.
This only checks for the tile below the dynamic floor.

So how do I check if it's water by either a tile or a dynamic floor?

alt Re: Check if tile is water

Yunuu
User Off Offline

Zitieren
1
2
3
4
5
6
7
8
9
10
11
12
local tiles = {
	[14] = true
}

function isWater(x, y)
	if entity(x, y, "exists") and entity(x, y, "type") == 71 then
		if tiles[entity(x, y, "int0")] then
			return true
		end
	end
	return false
end

Specify water tile IDs in tiles table it works only for entities you still need use
tile(tx, ty, 'property')
for normal tiles with water property.

alt Re: Check if tile is water

DC
Admin Off Offline

Zitieren
What user Yunuu wrote looked quite right. Are you sure that it isn't working?
Make sure not to just put 14 into the array. You have to add the tile FRAMES (!) of water tiles there. So the 14 is very misleading in that sample code.

It's a bit stupid that you manually have to define water tiles. I will add the Lua command cs2d lua cmd tileproperty which allows you to get the property value of a tile frame (instead of a tile position).

alt Re: Check if tile is water

Mami Tomoe
User Off Offline

Zitieren
IMG:https://i.imgur.com/nTSi1Dg.png



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- List of tiles that are... Wet...?
hc.water_tiles = {
	[42] = true,
	[52] = true,
	[53] = true
}

function hc.util.is_water(tx, ty)
     if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          if (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
				
               return true
          end
     end
	 
     return false
end

This returns 'false' on both water and the road, I'm trying to make a bridge.

The bridge is made out of dynamic walls.

alt Re: Check if tile is water

DC
Admin Off Offline

Zitieren
Like user Yunuu said you STILL also have to do your
tile(tx, ty, 'property') == 14
-check! In fact you should do your check first and only if that returns false you should try the entity/dynamic object checks.

If the dynamic object part isn't working I would try to print the values to console/chat/wherever to see if anything returns weird/unexpected things.

alt Re: Check if tile is water

Mami Tomoe
User Off Offline

Zitieren
Using:

1
2
3
4
5
6
7
8
9
10
function hc.util.is_water(tx, ty)
     if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          if (tile(tx, ty, 'property') == 14) or (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
				
               return true
          end
     end
	 
     return false
end

or

1
2
3
4
5
6
7
8
9
10
function hc.util.is_water(tx, ty)
     if (tile(tx, ty, 'property') == 14) or entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          if (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
				
               return true
          end
     end
	 
     return false
end

Still false everywhere.

alt Re: Check if tile is water

DC
Admin Off Offline

Zitieren
The nesting is bad and hard to understand and read. You always do entity checks. Even for tiles which are simply water tiles without an entity.

Simple code with more IFs and less and/or is always better (easier to understand, less error prone)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function hc.util.is_water(tx, ty)
	-- water! no need to check entities!
	if (tile(tx, ty, 'property') == 14) then
		return true
	end

	-- dynamic object check
	if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
		if (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
			return true
		end
	end
      
	return false
end

to debug however you should try something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function hc.util.is_water(tx, ty)
	-- water! no need to check entities!
	if (tile(tx, ty, 'property') == 14) then
		return true
	end

	-- dynamic object check
	if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
		msg("tile is dynamic object")
		if (not entity(tx, ty, 'state') then
			msg("dyn obj state is false")
		end
		if hc.water_tiles[entity(tx, ty, 'int0')]) then
			msg("dyn obj tile is water")
			return true
		end
	end
      
	return false
end

by checking the messages you get you can see where it fails.

alt Re: Check if tile is water

Mami Tomoe
User Off Offline

Zitieren
Mehr >


I've made it work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function hc.util.is_water(tx, ty)
	-- Dynamic Tile
	if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 and (not entity(tx, ty, 'state')) then
		if hc.water_tiles[entity(tx, ty, 'int0')] then
			return true
		else
			return false
		end
	end
	
	-- Actual Tile
	if tile(tx, ty, 'property') == 14 then
		return true
	end
	
	return false
end

Thanks.
1× editiert, zuletzt 05.06.19 01:47:34

alt Re: Check if tile is water

DC
Admin Off Offline

Zitieren
Ah, sorry, my bad! Didn't consider that the tile is still a water tile if it's covered by the entity. Of course my suggestion to check that first and return true in that case was nonsense then!

Glad to see that you managed to solve it! √
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht