0


This script line isn't working but I don't see any problems, I'm a beginner so I don't know anything about CS2D LUA scripting, it's not the same as all them xD

parse ("setpos "..id.." "..(x,"610")..","..(y,"60"))

flag offensive
asked 2010-08-08 03:15:50.957084
1
add comment
5 Answers:
0


parse("setpos "..tostring(id).." 610 60")

An easier way of doing this is via the format function

parse(string.format("setpos %s 610 60",id)
permanent link | flag offensive
answered 2010-08-08 04:10:55.955060
41
add comment
0


what advantages are there to using string.format?

permanent link | flag offensive
answered 2010-08-16 22:49:05.950959
1
add comment
0


The advantage of using format.string is that you can supply numbers and they are automatically converted to string for you.

Using the previous example:

parse(string.format("setpos %s %s %s",id,posX,posY)

Or something like that. Personally I don't really like using that method, but this way you don't have to add a tostring() function for each of the variables, or use "..id.." ect. It basically just reduces the code needed. ;)

permanent link | flag offensive
answered 2010-08-17 19:29:34.092707
5
add comment
0


meh i guess it's just preference notice that lee's second line (with format) is longer than his first lol

permanent link | flag offensive
answered 2010-08-17 22:37:18.125474
1
add comment
0


It's just convenience for me, coming over from the wonderful printf in a world filled with arcane programming hell that is the soul of C. (Why anyone still uses it is beyond me)

Anyways, CS2D's native output functions (print, msg, msg2) only takes in strings... which means that I usually have to explicitly cast the variable into a string. (seriously, type casting in the most dynamic language ever? Blasphemy!) Hence, to forgo the mind boggling internal debate of whether I should just use my python-to-lua extension (which is ironically written in C) instead of enduring the anti-lua, I just pretend that string.format is the next best shit since sliced bread.

Also, since I rarely work with consts, I usually just do the following: some_str:format(a,b,c)

This can be further extended, for example, if we want to join a string together with "%s=%s;", we can just do the following:

delimiter = "%s=%s;"
field = {'a',1,'b',2,...}
return delimiter:rep(#field/2):format(unpack(field))

Which for me is more concise than

str=""
field = {'a',1,'b',2,...}
for i=1,#field,2 do
    str = str .. field[i] .. "=" .. field[i+1] .. ";" -- // Ahhhh, C/C++ doesn't support str concatenation.... damn those languages are backwards T_T
end
return str

Lately though, I've been too lazy to actually bother with the whole :format thing so I just add

local gm = debug.getmetatable("")

gm.__mod=function(self, other)
        if type(other) ~= "table" then other = {other} end
        for i,v in ipairs(other) do other[i] = tostring(v) end
        return self:format(unpack(other))
end

and use

"%s %s %s"%{"a","b","c"}

instead xD

permanent link | flag offensive
answered 2010-08-18 02:40:40.536536
41
add comment
Your answer:
You are now not logged in but you can answer first and then login
toggle preview



Tags:

× 2
× 1
× 1

Asked: 1 year, 6 months ago

Seen: 1,008 times

Last updated: 1 year, 5 months ago

Made with Django.