This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MarioBG's profile picture

Displaying text from a table in the precise moment

Started by MarioBG, 02 January 2014 - 08:27 AM
MarioBG #1
Posted 02 January 2014 - 09:27 AM
Hello, people!
I've recently been working on my driving simulator for a custom map, which I shall post here too, of course, when I tripped over a problem. I've never actually understood how to properly work with tables, and I found that I didn't quite know how to overcome this.

I've got a serialised table in a separate file, which has a string associated to a number, and that string is to be shown in the chat via command blocks, but only when the user surpasses the required distance, which is the previously mentioned number.

I think I could do it up to this point, but, obviously, once the user has surpassed this distance, I do not wish the message to be shown forever after this. Also, there is a precision problem, because the distance is updated every 0.05 seconds, and it is almost impossible for the distance to be exactly the required one in this point.

So, we've got imprecise distance measurement that has to be higher than the value of the needed chat line, but we need that, after it has been shown, it will never do so again, with support for as many lines as I need to implement changing only the external file (which would be as simple as

f=fs.open("chat/dialog","r")
jander=f.readAll()
f.close()
table=textutils.unserialize(jander)
that, although probably the variable names would change). ¡Thanks for your help!
Edited on 02 January 2014 - 08:27 AM
Bomb Bloke #2
Posted 02 January 2014 - 10:02 AM
Let's say you defined your table like this:

message = {
  {[10] = "That's ten blocks!",
  [20] = "Twenty, keep going!",
  [50] = "Fifty!"
  [100] = "One hundred! Well done!"}

Now say you've ascertained the players distance via whatever means, and stored that value in a variable called "distance". You could then do something like this:

if message[math.floor(distance)] then  -- If an index exists in the table for the value of "distance" rounded down...
  chat(message[math.floor(distance)])        -- Chat that message (this assumes you set up a function that chats things)
  message[math.floor(distance)] = nil        -- Remove that index from the table, so it can't be found again.
end
MarioBG #3
Posted 02 January 2014 - 11:21 AM
Let's say you defined your table like this:

message = {
  {[10] = "That's ten blocks!",
  [20] = "Twenty, keep going!",
  [50] = "Fifty!"
  [100] = "One hundred! Well done!"}

Now say you've ascertained the players distance via whatever means, and stored that value in a variable called "distance". You could then do something like this:

if message[math.floor(distance)] then  -- If an index exists in the table for the value of "distance" rounded down...
  chat(message[math.floor(distance)])        -- Chat that message (this assumes you set up a function that chats things)
  message[math.floor(distance)] = nil        -- Remove that index from the table, so it can't be found again.
end
Thanks, but that is only part of what I need, because it is not the player who moves, but the variable instead, because it is via monitors (virtual car) how the feeling of driving is created. So the variable of distance could be more than one unit higher than in the previous check, and could skip one chat line. So, what I am going to do is just checking if it is HIGHER than the value, instead of EQUAL, and using the rest of your idea nonetheless. Thanks, you got me out of my mindblock!