Jump to content

Route locking, Switch["Position"] does not exist -> Error ?


Herman
 Share

Recommended Posts

Hello, 

Sorry I'm here back only to understand / learning something. 

Before the question a short introduction.
In route locking I have also crosses which must be locked. And a static cross doess not have a position. ( unlike a track switch )
Solution : give in the table position 99 to such crosses, so they are handled as another switch,  and trap in the code with an if ... then. Works fine

Code 
  -- lock the switches, crossings 
  for i, Switch in ipairs(t) do
    -- a cross without position has in the list/table position 99
    if   Switch["Position"] ~= 99 then
      Switch["SwitchPoint"].state = Switch["Position"]
    end

    Switch["SwitchPoint"].variables["#TSW Locked"] = true
  end

In a project I forgot to give the crosses the 99 position, they have none as you can see in the table : Route [2]

1091950977_Routelocking.jpg.d21ed63157cf3fc42c695ebc74c5f0bf.jpg

The code is as above, but even when I looked with the analyser, it seemed there was no script error visible, and I did expect one.
And it all works fine ? Is there an error or not, or did I just not seen it ? Do I overlook something ?

As always thanks for reading, super thanks for an answer.

Kind regards, Herman

Link to comment
Share on other sites

3 minutes ago, Goetz said:

a simple crossing is not a switch. It therefor has no "state" property.

Hello @Goetz,

Yes I know that. Of course it has no state to switch. That's why I gave it in the route table position 99 as an non existent state.
The if ... then sees the 99, and there is no change of the state. That works fine.

But now I forgot to give ( in the route table ) to give this 99. I gave no position a

Link to comment
Share on other sites

Just now, Herman said:

But now I forgot to give ( in the route table ) to give this 99. I gave no position a

Sorry, interrupt.

So I forgot the = 99 position.  I wonder why all is still working. 

In     if   Switch["Position"] ~= 99 then , the Switch["Position"] does not exist in the table ( if it is a static cross )
Why is there not an error ? Sorry my question was confusing. 

Herman


      

Link to comment
Share on other sites

11 minutes ago, Herman said:

That's why I gave it in the route table position 99 as an non existent state.

Maybe someone wonders why the cross is in the table. Well it has no state indeed, but still needs locking if the train comes there.
So I wanted to handle switches and crosses in the same manner in the route table. The code then filters out the cross for the state, but still locks correct.

 

Link to comment
Share on other sites

vor 12 Minuten schrieb Herman:

Sorry my question was confusing. 

indeed, it is.

I understand why you want to include the crossing.
But your method seems strange and cumbersome. Why not have a simple "lock" boolean, which you turn on and off?

Link to comment
Share on other sites

Just now, Goetz said:

Why not have a simple "lock" boolean, which you turn on and off?

That's exactly what I do.  It is a simple lock. The switches and static crosses have a boolean in it 

code :

    Switch["SwitchPoint"].variables["#TSW Locked"] = true

Never mind. It's just I do not understand why an non existing "thing" causes no error in the if then else.
There is a misunderstanding about what I'm asking ... :(

However, thanks a lot for your time and answer.

Kind regards,
Herman

Link to comment
Share on other sites

vor 23 Minuten schrieb Herman:

It's just I do not understand why an non existing "thing" causes no error

Probably, because Lua creates variables (and table entries) when they don't exist and you assign a value to them.

At Lua level, the command Switch["SwitchPoint"].state = Switch["Position"] maybe fine.
And Lua won't know that the item in question has no state. The Lua table is merely a copy of the item's properties.

Link to comment
Share on other sites

Hello,

Mostly in this part of the forum there is asked for a solution.
Here it is the other way around. The solution is there, is not cumbersome, and works fine.
Track Switches and Static Crosses are handled the same in the route table. So I needed something to distinguish. 
I took position 99, but it could be whatever. 

But I forgot to implement it in a new project. I forgot to give the crosses a position, it does not exist in the table. And still all is working as before. Strange not ?

I leave it here, sorry I'm not more clear.

Kind regards,

Herman

Link to comment
Share on other sites

Hello @Goetz,

 

1 minute ago, Goetz said:

At Lua level, the command Switch["SwitchPoint"].state = Switch["Position"] maybe fine.

We both do not give up hé.  Nice you come still with an answer.
That's the answer I was looking for. 

Thank you very much indeed !

Nice regards,

Herman

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...