Jump to content




Convert string to function or send function


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

#1 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 18 July 2012 - 09:15 AM

Just quick query, can you convert a string to a function so you can end a string over rednet and then execute it as a function? I know you can do this:

local id,cmd,dist=rednet.receive()
turtle[cmd]()

and then if you send the string "forward" it moves forward but this is then limited to anything starting with turtle.

another problem is that while you can assign a function to a variable (eg. local me=turtle.forward and then call me()) you cannot then include that in a table and serialize it as it is trying to combine a function and a string, I know you can convert things with tonumber() and tostring(), can you convert a string to a function?

#2 1lann

  • Members
  • 516 posts
  • LocationSeattle

Posted 18 July 2012 - 11:36 AM

View PostKaoS, on 18 July 2012 - 09:15 AM, said:

Just quick query, can you convert a string to a function so you can end a string over rednet and then execute it as a function? I know you can do this:

local id,cmd,dist=rednet.receive()
turtle[cmd]()

and then if you send the string "forward" it moves forward but this is then limited to anything starting with turtle.

another problem is that while you can assign a function to a variable (eg. local me=turtle.forward and then call me()) you cannot then include that in a table and serialize it as it is trying to combine a function and a string, I know you can convert things with tonumber() and tostring(), can you convert a string to a function?
It's possible. This is how the lua program does it
--In this, the variable "s" is the string function you want to execute

local nForcePrint = 0
local func, e = loadstring( s, "lua" )
local func2, e2 = loadstring( "return "..s, "lua" )
if not func then
  if func2 then
   func = func2
   e = nil
   nForcePrint = 1
  end
else
  if func2 then
   func = func2
  end
end

if func then
		setfenv( func, tEnv )
		local tResults = { pcall( function() return func() end ) }
		if tResults[1] then
		 local n = 1
		 while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
		  print( tostring( tResults[n + 1] ) )
		  n = n + 1
		 end
		else
		 print( tResults[2] )
		end
	else
	 print( e )
	end


#3 Pinkishu

  • Members
  • 484 posts

Posted 18 July 2012 - 11:59 AM

uhm why not just
func = loadstring(s)
func()


#4 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 18 July 2012 - 12:41 PM

AHAH! perfection, thanks

#5 1lann

  • Members
  • 516 posts
  • LocationSeattle

Posted 18 July 2012 - 01:42 PM

View PostPinkishu, on 18 July 2012 - 11:59 AM, said:

uhm why not just
func = loadstring(s)
func()
Ohhhh. I actually didnt understand how the lua program works XD new thing I learnt! thanks

#6 Pinkishu

  • Members
  • 484 posts

Posted 18 July 2012 - 01:52 PM

Might be able to even to like loadstring(s)()

#7 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 18 July 2012 - 01:58 PM

you should, that was my plan, along with a few other mean and nasty things I plan on implementing, no-one will be able to pirate my computers ever again :P/> although I am developing an automatic pirating system to overwrite and modify all computers within range of my base that are not in my accepted list of IDs... cc is the most awesome part of mc by far

#8 Pinkishu

  • Members
  • 484 posts

Posted 18 July 2012 - 02:04 PM

I don't see how that would work but if you say so :P/>

#9 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 19 July 2012 - 02:03 PM

Here is what you do:

you set up a modified gps satellite, it accepts any message as a gps request and does not send anything back to the requester, it sends it to your PC which checks its ID to see if it is yours, if it isn't then you send out a turtle to reprogram it (which is a whole other story 8o )

#10 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 19 July 2012 - 02:13 PM

that way you get its exact location and there is a process in which you can place a disk drive next to the pc, insert one and restart the pc, the disk's startup then rewrites the pc, the turtle then removes the disk drive and the apparatus used to place it and returns to your base, you can then use the pirated pc as another modified gps satellite to extend the reach of your overwriting system or you can use it as a signal disruptor etc

#11 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 19 July 2012 - 02:19 PM

View PostKaoS, on 19 July 2012 - 02:13 PM, said:

that way you get its exact location and there is a process in which you can place a disk drive next to the pc, insert one and restart the pc, the disk's startup then rewrites the pc, the turtle then removes the disk drive and the apparatus used to place it and returns to your base, you can then use the pirated pc as another modified gps satellite to extend the reach of your overwriting system or you can use it as a signal disruptor etc
The only problem is that turtles can't insert disks in the disk drives :P/>
I made a system like that, wich locates any computer that broadcasts a message. It's on the program library.

#12 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 19 July 2012 - 02:50 PM

REDPOWER!!!! it can be done lol, use deployers and transposers

#13 KingMachine

  • Members
  • 122 posts

Posted 19 July 2012 - 04:50 PM

View PostMysticT, on 19 July 2012 - 02:19 PM, said:

The only problem is that turtles can't insert disks in the disk drives :P/>
turtle selects obsidian pipe
places it next to disk drive (compared from inventory)
turtle drops disk


OR
turtle places disk drive next to a computer
turtle places obsidian pipe above that
turtle drops floppy in

And yes, obsidian pipes work with disk drives. Just tested it to verify it (it is a storage device after all)

This would of course require that mod. But most computer craft servers are tekkit servers anyways. It's highly likely to have them available. If not, there may be other methods in the future.

#14 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 July 2012 - 06:02 PM

View PostKingMachine, on 19 July 2012 - 04:50 PM, said:

View PostMysticT, on 19 July 2012 - 02:19 PM, said:

The only problem is that turtles can't insert disks in the disk drives :P/>
turtle selects obsidian pipe
places it next to disk drive (compared from inventory)
turtle drops disk


OR
turtle places disk drive next to a computer
turtle places obsidian pipe above that
turtle drops floppy in

And yes, obsidian pipes work with disk drives. Just tested it to verify it (it is a storage device after all)

This would of course require that mod. But most computer craft servers are tekkit servers anyways. It's highly likely to have them available. If not, there may be other methods in the future.

In the current version of ComputerCraft, turtles are unable to place pipes, to my knowledge. Did you test this portion of your suggestion as well?

#15 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 19 July 2012 - 06:10 PM

use transposers, I have fully tested the process and it works, you also have to use a deployer to place the disk drive but an automated overwrite is possible

#16 Pinkishu

  • Members
  • 484 posts

Posted 20 July 2012 - 06:59 PM

View PostKingMachine, on 19 July 2012 - 04:50 PM, said:

[...], obsidian pipes [...]


lol, bc :)/>

#17 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 04 August 2012 - 12:33 PM

Or alternatively, destroy the computer, bring it back to base, place it somewhere else, load it where your foe can't find it
If you try to rewrite things there are many issues:

1- what if the diskdrive on the pc is full
2-what if all perimeters of the computer are occupied
3-what if the owner sees your turtle coming and destroys it first, your enemy just gained control of your turtle and anything in it

Just an idea for a quick method, especially since pipes are slow. And no matter what happens, your bound to destroy your foe's computer.

#18 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 04 August 2012 - 01:15 PM

you can make the startup disk delete everything on the PC, eliminating the space issue (which wasn't a problem in 1.3 - when this topic was created)

the turtle can mine out a space next to the computer

with your method the owner could still see it coming

what do pipes have to do with it? use a redpower transposer

#19 bonzaii

  • New Members
  • 7 posts

Posted 22 October 2012 - 12:11 AM

Hi there.

After 5 hours I have decided to give up. I can't get it working, this loadstring stuff.

My script:
function test()
print("Test")
end
local s = "test"
func = loadstring(s)
func()

Running above 'eval' program gives error:
eval :8: attempt to call nil

Can anybody help?

#20 faubiguy

  • Members
  • 213 posts

Posted 22 October 2012 - 12:54 AM

You use loadstring on the actual body of a function, not the name. For example:
local s = "print('Loadsting test')"
func = loadstring(s)
func()






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users