HTOTM: FUSION
local function getUser(msg)
	local user = ""
	local userEnd = msg:find(">")
	local closeBr = msg:find(")")
	
	if closeBr == nil or closeBr > userEnd then
		return msg:sub(5, userEnd-1)
	else
		return msg:sub(closeBr+1, userEnd-1)
	end
end

add_hook("console", "", function(msg, t)
	--  8 => public msg from someone else
	-- 16 => public msg from you
	if t == 8 or t == 16 then
		echo("user: "..getUser(msg))
	else
		echo(t)
	end
end)
Also, use [code] tags to display code.
Signature temporarily out of order.
Thank you very much, just needed to change
local closeBr = msg:find(")")
to
local closeBr = msg:find(")") or msg:find("%]")
I have no idea why i made it get the start and end so many times.

Also, do you know if they recently made some changes for lua in toribash? Drawing seems to be much faster now at least.

Edit: Now if a user without an unofficial clan write ) it still works
local function getUser(msg)
	local user = ""
	local userEnd = msg:find(">")
	msgName = msg:sub(0,userEnd)
	local closeBr = msgName:find(")") or msgName:find("%]")
	
	if closeBr == nil or closeBr > userEnd then
		return msg:sub(5, userEnd-1)
	else
		return msg:sub(closeBr+1, userEnd-1)
	end
end

add_hook("console", "", function(msg, t)
	--  8 => public msg from someone else
	-- 16 => public msg from you
	if t == 8 or t == 16 then
		echo("user: "..getUser(msg))
	else
		echo(t)
	end
end)
Last edited by IceColor; Oct 21, 2014 at 06:02 PM.
Fukin spice and wolf
Originally Posted by psycore View Post
Also a quick tip: You can write 'a:find("%[")' instead of 'string.find(a, "%[")'. Note the colon though, it's necessary. Works only for variables so '"asdf":find(...)' won't work.

Actually, "asd":find won't work because of the parser, if you do ("asd"):find it will work, another thing, string.find has a 4th parameter that let's you pass non-pattern strings, like this:
lua code:
("asd()"):find("()", 1, true)
Originally Posted by IceColor View Post
Also, do you know if they recently made some changes for lua in toribash? Drawing seems to be much faster now at least.

No idea. The lua API (the part we get to used) hasn't changed. Of course there could have been improvements on the parser.

Originally Posted by RickyRicon View Post
Actually, "asd":find won't work because of the parser, if you do ("asd"):find it will work, another thing, string.find has a 4th parameter that let's you pass non-pattern strings, like this:
lua code:
("asd()"):find("()", 1, true)

I didn't know the parser thing. That seems very odd to me. Doesn't that imply that "asd" and ("asd") are different language constructs? Shouldn't they both be equal in every aspect? Is this a general problem of Lua or did the parser used by TB just get that wrong?

I've read about the 3rd argument of find() but it's useful so rarely that I keep forgetting about it.
Signature temporarily out of order.
Originally Posted by psycore View Post
I didn't know the parser thing. That seems very odd to me. Doesn't that imply that "asd" and ("asd") are different language constructs? Shouldn't they both be equal in every aspect? Is this a general problem of Lua or did the parser used by TB just get that wrong?

This isn't a problem with Lua nor is it a problem with the parser used by TB (afaik, they shouldn't be using a custom parser but rather implementing the native Lua parser through C/C++).

My guess is that the difference between "asd":find("blah") and ("asd"):find("blah") is the order which things are evaluated. In the case of "asd":find() you are attempting to call the find function before the string has actually interpreted as such. In the case of ("asdf"):find(), Lua first interprets "asdf" as a string and then calls the find function on that. Parentheses place priority on whatever is inside of the parentheses: without them though, the function will take the first priority.
Last edited by motota; Oct 22, 2014 at 06:19 AM.
Running frames
Hi Toribash developers,

I'm trying to be able to run_frames() in a loop but it is just stepping one time and at the last loop round .

I'm using this code :

Lua code:

for k=0,10 do
run_frames(k)
end


I need this to implement it in my lua Script http://forum.toribash.com/showthread.php?t=484109 to be able to load a complete replay in one time for both player.

any help is welcome
Last edited by DisD; Oct 26, 2014 at 01:24 PM.
I was bored.
So... I tried to make a script that echos if tori/uke got dismembered/decaped. The script which I made, only works one time when called after tori/uke got dismembered.

I wanted to make it toggle, like after doing /ls name.lua, whenever tori/uke got dismembered, it would echo (One time). Any help appreciated ?

lua code:
Tori=0
Uke =1
joint = 0

tori_dp = get_joint_dismember(Tori,joint)
if tori_dp then
echo("Tori got Decapped")
end

for i=1,19 do
tori_dm = get_joint_dismember(Tori,i)
if tori_dm then
echo ("Tori got dismembered in no."..i.." joint")
end
end

uke_dp = get_joint_dismember(Uke,joint)
if uke_dp then
echo("Uke got Decapped")
end

for i=1,19 do
uke_dm = get_joint_dismember(Uke,i)
if tori_dm then
echo ("Uke got dismembered in no."..i.." joint")
end
end


P.S :- First time trying lua.
Last edited by eElectro; Oct 26, 2014 at 01:53 PM.
Dengue is a wizard.
[Nitro] | (KnC) | [Monk] | Gamer's Inc. | ViperTech and Cheshyre fanboy
Originally Posted by DisD View Post
Hi Toribash developers,

I'm trying to be able to run_frames() in a loop but it is just stepping one time and at the last loop round .

I'm using this code :

lua code:

for k=0,10 do
run_frames(k)
end

It seems that the frames step after your script has run. I don't know if that's a bug or intended behaviour but instead of

lua code:

for k=s, e do
run_frames(k)
end


you could do

lua code:

run_frames(- (s - e - 1) * (s + e) / 2)


Keep in mind though that the frames won't run until after your script pauses. If you want to pause execution until after the frames were run you need to register an enter_freeze hook and continue there.


Originally Posted by Eelectro View Post
So... I tried to make a script that echos if tori/uke got dismembered/decaped. The script which I made, only works one time when called after tori/uke got dismembered.

I wanted to make it toggle, like after doing /ls name.lua, whenever tori/uke got dismembered, it would echo (One time). Any help appreciated ?

[... code ...]

P.S :- First time trying lua.

I'm going to assume you want to learn Lua and not just need that one script so I'm not going to give you a finished solution but just some hints so you can figure it out yourself.

The script you have written will just run once, in the frame in that you execute the "/ls yourScript.lua" command.

However, you need to check every frame if someone was decapitated. There are so called "hooks" that let call a function in response to some event happening. The complete list of hooks can be found in startup.lua in your script folder. The event you need is "enter_frame".

So for example this code will echo "entered a frame" every beginning of a frame:

lua code:
-- The function that is called when a frame is entered.
-- The functions name can be whatever you want

local function onEnterFrame()
echo("entered a frame")
end

-- register the hook
-- first argument is the event name
-- second argument can be ignored for now
-- third argument is the function you want to call.

add_hook("enter_frame", "", onEnterFrame)


Take a look at the new and old Lua tutorials. They are a great place to start coding. Also there's a "sdk" folder in your script folder which contains small examples for a lot of script functions.
Last edited by psycore; Oct 26, 2014 at 06:08 PM.
Signature temporarily out of order.
Thanks and another brick in the wall
Hi ,

first thanks for your answer psycore ! It was very helpful!

But while programming i got a small run_frames() bug.

It seems that when entering the run_frames() command in a loop with the "enter_freeze" hook it is running run_frames(x+1) instead of run_frames(x) without doing that the first round of the loop.

I manage to solve this for run_frames(x) with x=x-1 after the first round of the loop.

but not for x=1! because then x=0. when x=0 run_frames suddenly accept 0 as 0 and not as 0+1 while looping!

I've got no problems with run_frames(1) while stepping to the next frame with a key being pressed.


Thanks and any help is welcome!
re up!

No one got the answer or a solution?

if needed the code is there : http://forum.toribash.com/showthread.php?t=484109

I've uploaded the press a key for next round solution and asking for help for the problem posted above.