Jump to content




loadstring has problems with tables...


  • You cannot reply to this topic
27 replies to this topic

#1 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 06 August 2015 - 06:14 PM

tabl={}

anotherTabl={}
function anotherTabl:printIt()
print("Tabl!")
end

tabl[1]=anotherTabl

local updateString=""
for pos, value in pairs(tabls) do
updateString=updateString.."tabl"..tostring( pos ).."="..tostring( value ).."\ntabl"..tostring(pos)..":printIt()\n"
end
print(updateString)
local func, err = loadstring(updateString)
if func then
print(updateString)
func()
else
error( err )
end

Edited by LeDark Lua, 06 August 2015 - 06:17 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 06 August 2015 - 07:17 PM

Well, the problem here is you're giving it a table pointer, not the table. tostring( value ) is giving you an address (table: 0x5dfe8 or similar), and the later loadstring is, of course, going to error saying "hey, I didn't expect random numbers and letters like that".

Try running this normally, and you'll see the similarities in the error:

thing = table: 0x5dfe8 


#3 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 06 August 2015 - 07:21 PM

Soo how to fix it?

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 06 August 2015 - 07:31 PM

Normally, I'd say use textutils.serialize to get the string code to replicate the table. In this case, I'll ask: what are you trying to do. Because what you're doing is almost entirely useless.

#5 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 06 August 2015 - 07:32 PM

Well I'm trying to make an some sort of API that updates all of the table functions at once.
Well better instance update code.

Edited by LeDark Lua, 06 August 2015 - 07:34 PM.


#6 ElvishJerricco

  • Members
  • 803 posts

Posted 06 August 2015 - 09:55 PM

Using loadstring like this is a really dirty hack that I'd avoid at all costs. Never, EVER use loadstring except for the purpose of loading arbitrary code. Don't try to build functions like that when you can just actually build a function. What exactly are you trying to do, so that we can help you find a more appropriate solution?

#7 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 07 August 2015 - 06:45 AM

I want to run code at once. I have a table where I store my objects and every object has an update function that needs to be updated. So how to update thoose functions without looping trough the table?

#8 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 07 August 2015 - 07:44 AM

Unless those functions are assigned to different variables you'll need a loop, but I don't really ser why that's bad to you. Why cannot you use a loop?

#9 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 07 August 2015 - 07:55 AM

objects are assigned from 1-Infinity

So if I have 100 object and I update them in a loop, 20fps is the max so it will finnish the full update in 5full frames I suppose.

Edited by LeDark Lua, 07 August 2015 - 08:01 AM.


#10 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 07 August 2015 - 08:27 AM

Ehm, textutils.serialize does not support functons and another tables :/

#11 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 07 August 2015 - 11:38 AM

View PostLeDark Lua, on 07 August 2015 - 07:55 AM, said:

objects are assigned from 1-Infinity

So if I have 100 object and I update them in a loop, 20fps is the max so it will finnish the full update in 5full frames I suppose.

Where did you get that number? Loops will work as fast as your computer can handle, meaning a few million iterations per second with small loops.

Edited by MKlegoman357, 07 August 2015 - 11:38 AM.


#12 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 08 August 2015 - 03:18 PM

It depends on the VM like java is make in some language and lua is made in java soo there is a delay and the lua depends on java which depends on the lsnguage java is made in.

#13 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 08 August 2015 - 03:40 PM

View PostLeDark Lua, on 08 August 2015 - 03:18 PM, said:

It depends on the VM like java is make in some language and lua is made in java soo there is a delay and the lua depends on java which depends on the lsnguage java is made in.

The Java VM that your computer will use JITs the Java bytecode. This means it gets translated into machine code - what your CPU actually runs. Whilst Java is still slower than writing in machine code, the overhead is still significantly smaller than you make out.

The Lua VM however is interpreted. I've experimented with compiling to Java bytcode (which then gets converted to machine code) and it works mostly, have a read about performance differences but in the end it only amounts to about 2-5x performance increase.

However, despite Lua is massively slower than machine code, it isn't as slow as 20Hz (20 instructions per second). You are probably getting confused with the Minecraft update speed - which is 20Hz. Some actions - such as sleep(0) and some peripheral calls are 'synchronised' with the server tick - and so stop your code running until the next server tick.

TLDR Use loops, this 20fps stuff is nonsense.

Edited by SquidDev, 08 August 2015 - 03:40 PM.


#14 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 08 August 2015 - 04:25 PM

but what if I have a low end pc. that's why I need performance ;)

#15 biggest yikes

  • Members
  • 573 posts

Posted 08 August 2015 - 06:11 PM

View PostLeDark Lua, on 08 August 2015 - 04:25 PM, said:

but what if I have a low end pc. that's why I need performance ;)
If your PC seriously lags from lua code, you've bought your computer from the wrong person.

#16 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 09 August 2015 - 05:29 AM

It doesn't lag it jus doesn't like big performance.

#17 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 09 August 2015 - 06:16 AM

View PostLeDark Lua, on 09 August 2015 - 05:29 AM, said:

It doesn't lag it jus doesn't like big performance.

If you have a graphics card and CPU made in the last 5 years, it should be able to run MC at 30 FPS (MINIMUM), and executing Lua code (especially through CC) shouldn't have any impact.
(Note that using a lot of mods can impact FPS, even with a good PC)

#18 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 09 August 2015 - 07:04 AM

On emulators I get 62FPS on MC 15/20-30

#19 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 August 2015 - 07:57 AM

If you're talking about your MineCraft framerate, that's also unrelated to the number of loop iterations ComputerCraft can complete in a second.

#20 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 09 August 2015 - 08:11 AM

If you want proof, just run this code:

for i = 1, 100000 do end

It will run almost instantly.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users