PDA

View Full Version : [LUA] io.read()



rafek1241
02-16-2015, 07:46 PM
I need a lua function like std::cin in C++ or Console.WriteLine(); in C#
Why?
Because it will be much easier to do auto-config scripts or something like that. I give you an example:



...

Config:SendOrangeMessage("Config creator", "Name of script.")
answer1 = io.read()
File:write("--" .. answer1 .. "--")
Config:SendOrangeMessage("Config creator", "Tell me your main bp.")
answer2 = io.read()
File:write("Mainbp = \"" .. answer2 .. "\"")
Config:SendOrangeMessage("Config creator", "Tell me your Loot bp.")
answer3 = io.read()
File:write("Lootbp = \"" .. answer2 .. "\"")

...

slaweksor
02-16-2015, 09:11 PM
There is already Channel.New()
By it you can edit any config.lua as much as you want.

rafek1241
02-16-2015, 09:32 PM
Ohh... I really wanted to write this more specifically but it doesn't work i see..

I don't need Channel.New() function. I need function which will be waiting for my typing and didn't check it what i type. I think that this simple function can make some scripts very much easier to write. So why not?

Channel.New() can that but only if i do something like that:



...
if (talkstate == 0) then
...

Config:SendOrangeMessage("Config creator", "Name of script.")
talkstate = 1

elseif ((talkstate == 1) and (answer == "config")) then
File:write("--" .. answer .. "--")
Config:SendOrangeMessage("Config creator", "Tell me your main bp.")
talkstate = 2
elseif ((talkstate == 2) and (answer == "backpack")) then
File:write("--" .. answer .. "--")
Config:SendOrangeMessage("Config creator", "Tell me your Loot bp.")
talkstate = 3
elseif ((talkstate == 3) and (answer == "beach backpack")) then
...



And my answer can't be random.

shadowart
02-17-2015, 02:38 PM
You can write a sort of bastardized version that waits a fixed amount of time for the user to write something and then return whatever was written. Alas the real version that waits for the user to type and then returns instantly (with optional timeout) would require preemptive multitasking which isn't supported by Lua and would be one hell of a hassle to implement. I really doubt that this is something that the devs would like to spend time on.

drakylucas
02-17-2015, 02:41 PM
local talkstate = 0

function onSpeak(channel, message)
--Send Messages
if message == "walker" then
talkstate = 1
return true
elseif message == "looter" then
talkstate = 2
return true
elseif message == "target" then
talkstate = 3
return true
end
if(talkstate == 1) then
File:write("WPT: " .. message .. "--")
channel:SendOrangeMessage('', message .. 'Added as the answer.')
elseif (talkstate == 2) then
File:write("LOOTER: " .. message .. "--")
channel:SendOrangeMessage('', message .. 'Added as the answer.')
end

end


function onClose(channel)
print('Draky Casino closed. Go to Scripter and open it again if you want to use me.')
end

local canal = Channel.Open('canal', onSpeak, onClose)
if talkstate == 0 then
canal:SendOrangeMessage('canal', 'Options: Walker, Looter, Target')
end




this check the talkstate and write in correct lin

Spectrus
02-17-2015, 02:46 PM
When you create a custom channel in Xenobot you pass the constructor three parameters: channelName, textCallback, closeCallback.

The first is a string and is the name given to the channel you create. The last is a function that is called when the channel is closed.

The second, which is relevant to what you're looking for, is called any time any text is sent in the channel. It looks something like this:


function OnSpeak(channel, message)
if (message == "ping") then
channel:SendOrangeMessage("Channel", "Pong!")
end
end

function OnClose() end --empty

local newChannel = Channel.New("Channel", OnSpeak, OnClose)

newChannel:SendOrangeMessage("Channel", "Hi!")

shadowart
02-17-2015, 03:03 PM
When you create a custom channel in Xenobot you pass the constructor three parameters: channelName, textCallback, closeCallback.

The first is a string and is the name given to the channel you create. The last is a function that is called when the channel is closed.

The second, which is relevant to what you're looking for, is called any time any text is sent in the channel. It looks something like this:


function OnSpeak(channel, message)
if (message == "ping") then
channel:SendOrangeMessage("Channel", "Pong!")
end
end

function OnClose() end --empty

local newChannel = Channel.New("Channel", OnSpeak, OnClose)

newChannel:SendOrangeMessage("Channel", "Hi!")
Considering his code examples I'm pretty sure he already knows that but finds it cumbersome to use as you have to turn your callback function into a finite-state machine.