chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 23 Aug 2015 12:01 PM |
Must support all number representations.
1 1.2 -2 2e4 3e+7 0x0111 etc etc
The numbers will be seperated with a single space character.
|
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 23 Aug 2015 12:10 PM |
StringToVector3 = function(s) local X,Y,Z for word in s:gmatch("[^%s]+") do if not X then X = tonumber(word) elseif not Y then Y = tonumber(word) elseif not Z then Z = tonumber(word) end end if X and Y and Z then return Vector3.new(X,Y,Z) end return nil end |
|
|
| Report Abuse |
|
|
lupine
|
  |
| Joined: 24 Jun 2008 |
| Total Posts: 3561 |
|
|
| 23 Aug 2015 12:14 PM |
| Not difficult, just too much work. I thought about how I'd make it, I just didn't do any typing. |
|
|
| Report Abuse |
|
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 23 Aug 2015 12:18 PM |
| What about commas? The axes of all the Vector3s I see in Roblox are separated by commas. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 23 Aug 2015 12:23 PM |
...
THE STRING NUMBERS ARE SEPERATED BY A SINGLE SPACE.
WTF is so hard to understand?
|
|
|
| Report Abuse |
|
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 23 Aug 2015 12:42 PM |
That would be easier. Here's one that actually works with real Vector3s.
prp='-179.879, 85.094, 179.895' local cells={} for i=1,2 do local comma=string.find(prp,',') local cell=string.sub(prp,1,tonumber(comma-1)) prp=string.sub(prp,comma+1) table.insert(cells,cell) end prp=tonumber(prp) table.insert(cells,prp) prp=Vector3.new(unpack(cells)) print(prp)
Crude, yes, but yours doesn't even work with your imaginary, non-comma-having Vector3s. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 23 Aug 2015 12:50 PM |
Let me try to explain the error of your ways.
1. Please wait 3-5 years before coming onto forums.
2. How you encode a Vector3 into a string doesn't matter at all, the challenge is to convert strings where numbers are seperated by a space back into Vector3. |
|
|
| Report Abuse |
|
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 23 Aug 2015 12:56 PM |
Okay, I take that back, it does work. ' didn't copy the whole function and got the return inside the loop.
Here
StringToVector3 = function(s) local X,Y,Z for word in s:gmatch("[^%s^,]+") do print(word) X = tonumber(word) Y = tonumber(word) Z = tonumber(word) end if X and Y and Z then return Vector3.new(X,Y,Z) end return nil end
now it works in the real world. |
|
|
| Report Abuse |
|
|
chimmmihc
|
  |
| Joined: 24 Jul 2014 |
| Total Posts: 2420 |
|
|
| 23 Aug 2015 12:59 PM |
I like how chimm asks a hard question nobody answers the question for ten minutes and as soon as he gives an answer everyone is like "OH I KNEW THAT I JUST DIDN'T WANT TO DO IT"
Deflate your ego guys. |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2015 01:04 PM |
function stringToVector(s) local allNums = {} for num in s:gmatch("[^%s]+") do allNums[#allNums + 1] = num end return Vector3.new(unpack(allNums)) end
print(stringToVector("3e7 2e+2 -.4"))
-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::] |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2015 01:04 PM |
"chimmmihc" totally not an alt
~Upload code to codepad-dot-org with Lua syntax highlighting to preserve indentation and make it easier to read!~ |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
| |
|
chimmmihc
|
  |
| Joined: 24 Jul 2014 |
| Total Posts: 2420 |
|
|
| 23 Aug 2015 01:09 PM |
I'm garybyte
http://www.roblox.com/Forum/ShowPost.aspx?PostID=171821726
It a joke name change |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2015 01:11 PM |
local stv3=function(s) local a={} for i in s:gmatch('[^%s]+') do a[#a+1]=tonumber(i) if #a>3 then break end end return Vector3.new(unpack(a)) end |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2015 01:15 PM |
local function StringToVector3(str) local x, y, z = str:match("(%S+)%s+(%S+)%s+(%S+)") return Vector3.new(tonumber(x), tonumber(y), tonumber(z)) end
Yay I win
~Upload code to codepad-dot-org with Lua syntax highlighting to preserve indentation and make it easier to read!~ |
|
|
| Report Abuse |
|
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 23 Aug 2015 01:27 PM |
| Ego? What about his ego? You can't assume that everyone sees a post as soon as it goes up. It's not like we have events in our brains that fire as soon as chimmihc clicks Post. I saw his response the first time I clicked on the thread, asked a simple question, and look at his response to it. |
|
|
| Report Abuse |
|
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 23 Aug 2015 01:55 PM |
| Anyway, I'll go ahead and apologize. I was angered by his response to me and, in haste, said something not true and posted something that doesn't work. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Aug 2015 03:27 PM |
local x, y, z = str:match("([^,]+), ([^,]+), ([^,]+)"); return Vector3.new(tonumber(x), tonumber(y), tonumber(z)); |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 Aug 2015 03:35 PM |
just an fyi, Vector3 will automatically convert your strings to numbers
local str = "1e, 1e, 1e" local x, y, z = str:match("([^,]+), ([^,]+), ([^,]+)");
print(Vector3.new(x,y,z) == Vector3.new()); |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Aug 2015 03:37 PM |
| Yeah but your example code doesn't make sense lol |
|
|
| Report Abuse |
|
|
|
| 23 Aug 2015 03:40 PM |
Vector3.new("0","0","0") == Vector3.new(0,0,0)*
|
|
|
| Report Abuse |
|
|
|
| 23 Aug 2015 04:21 PM |
local Vector3String='1e3 -38.380 0x0111' local X,Y,Z=Vector3String:match'(.-) (.-) (.-)' or error'Invalid string' local FinalVector3=Vector3.new(X,Y,Z) |
|
|
| Report Abuse |
|
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 23 Aug 2015 05:17 PM |
Can this get any shorter?
stringtovalue=function(s) local cells={} for cell in s:gmatch("[^%s^,^{^}]+") do table.insert(cells,cell) end if #cells==3 then return Vector3.new(unpack(cells)) elseif #cells==2 then return Vector2.new(unpack(cells)) elseif #cells==4 then print('uDim') return UDim2.new(unpack(cells)) elseif #cells==6 then local v1=Vector3.new(cells[1],cells[2],cells[3]) local v2=Vector3.new(cells[4],cells[5],cells[6]) return Ray.new(v1,v2) elseif #cells==12 then return CFrame.new(unpack(cells)) end return nil end |
|
|
| Report Abuse |
|
|
|
| 24 Aug 2015 06:35 AM |
stringtovalue=function(s) local cells={} local t={[3]=Vector3.new,[2]=Vector2.new,[4]=UDim2.new,[12]=CFrame.new} for cell in s:gmatch("[^%s^,^{^}]+") do cells[#cells+1]=cell end return t[#cells](unpack(cells)) or nil end
Try this. Not 100% sure if it will work. |
|
|
| Report Abuse |
|
|