Forum

> > CS2D > Scripts > [Solved]A table key for =1,#table do
Forums overviewCS2D overview Scripts overviewLog in to reply

English [Solved]A table key for =1,#table do

6 replies
To the start Previous 1 Next To the start

old [Solved]A table key for =1,#table do

Mora
User Off Offline

Quote
Hello guys!)
I am from the phone, excuse me if there any mistakes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
addhook ("movetile","_movetile")
function _movetile (id, x, y)
 for i=1, #Houses do
  if Houses[i].dx then
  xx, yy = Houses[i].dx, Houses[i].dy
if x==xx and y==yy then
 if Player[id].Houses ~=i then
msg("Test. You are on "..Houses[i].Name)
Player[id]InHouse=i
msg("Test. You on "..Player[id].InHouse)
 end
end
  break
  end
 end
end
It's works only for first house from House table. When step on another tiles(of Houses dx and dy) nothing happens. No any errors.
Btw if there any solutions about identical problem you may give mejust a link. Bcs I can't load search function with this sucks stolen WiFi. Thank you!
edited 1×, last 26.08.16 08:35:10 pm

old Re: [Solved]A table key for =1,#table do

Yates
Reviewer Off Offline

Quote
Please code self-explanatory.
xx
and
yy
do not tell anyone what they are used for. Yes, we can all see they are clearly the house
dx
and
dy
but what the fuck are those and if you keep coding like this everywhere no one will be able to understand what it does.

And no - I didn't come here to provide a solution. I came here to tell you to clean up your code so people will want to help you faster.

old Re: [Solved]A table key for =1,#table do

Bowlinghead
User Off Offline

Quote
Line 9 has a syntax error but you told. But if the code is different than your original then you dont need to post it I guess...

What I see is that you use "Houses[i]" in line 4 but "Player[id].Houses" in line 7.

old Re: [Solved]A table key for =1,#table do

GeoB99
Moderator Off Offline

Quote
I also have the same perception like user Cebra's. Adding a
break
statement inside at the end of a loop specific will cause the loop execution to cease immediately and the conditional code inside the block as well afterwards - basically the loop will just exit. Not sure what was your motive to add this statement however I can say for sure this is the source of the problem.

old Re: [Solved]A table key for =1,#table do

Dousea
User Off Offline

Quote
It's because
break
without doubt. Maybe you're trying to break the loop after you found the house where the player is inside it. Try this. Protip: Don't use
for i = a, b, c do
then use
t[i]
to get a table's value, use
ipairs
or
pairs
instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
addhook("movetile","_movetile")
function _movetile(id, x, y)
	for i, House in ipairs(Houses) do
		if House.dx then
			xx, yy = House.dx, House.dy
			if x==xx and y==yy then
				if Player[id].Houses ~=i then
					msg("Test. You are on "..House.Name)
					Player[id].InHouse=i
					msg("Test. You on "..Player[id].InHouse)
					
					break
				end
			end
		end
	end
end

old Re: [Solved]A table key for =1,#table do

Mora
User Off Offline

Quote
Yates, I just forget to include more of code to make the problem understandable.
I have no net right now and all time I should outside my house to use WiFi. + I can't write the code here bcs it very hard. but I think Ortimh's jobs would work.I hope it would work for each house but not for one.
Thank you everyone. I'll try this next day and come here when I can.

Yeah, this works fine. Ortimh, where you get that from? I'd like to see the source to teach it by myself.
edited 1×, last 26.08.16 08:32:23 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview