I have a script that is meant to get data from a webpage
[code]
function LoadDeed(ply)
function http_reply(contents)
data_reply = string.Explode("|", contents)
xCord = data_reply[2]
yCord = data_reply[3]
zCord = data_reply[4]
end
http.Get("http://127.0.0.1/realwar/retrievedata.php?auth=datanet_687_PI&con=mapdata&map=" .. tostring(game.GetMap()), "", http_reply)
return xCord, yCord, zCord
end
[/code]
It variables it is set to return aren't, but they can be seen inside the http_reply function, how do I get the variables to be accessed outside of the function?
I wouldn't declare a function inside of a function, it's weird.
Try making two separate functions.
Declare them outside of the function:
[lua]
function LoadDeed(ply)
local xCord, yCord, zCord
function http_reply(contents)
data_reply = string.Explode("|", contents)
xCord = data_reply[2]
yCord = data_reply[3]
zCord = data_reply[4]
end
http.Get("http://127.0.0.1/realwar/retrievedata.php?auth=datanet_687_PI&con=mapdata&map=" .. tostring(game.GetMap()), "", http_reply)
return xCord, yCord, zCord
end
[/lua]
[QUOTE=_nonSENSE;23307097]Declare them outside of the function:
[lua]
function LoadDeed(ply)
local xCord, yCord, zCord
function http_reply(contents)
data_reply = string.Explode("|", contents)
xCord = data_reply[2]
yCord = data_reply[3]
zCord = data_reply[4]
end
http.Get("http://127.0.0.1/realwar/retrievedata.php?auth=datanet_687_PI&con=mapdata&map=" .. tostring(game.GetMap()), "", http_reply)
return xCord, yCord, zCord
end
[/lua][/QUOTE]
[lua]-- Or the more reliable way:
function LoadDeed(ply)
return http.Get("http://127.0.0.1/realwar/retrievedata.php?auth=datanet_687_PI&con=mapdata&map=" .. tostring(game.GetMap()), "", function(contents)
local data_reply = string.Explode("|", contents)
local xCord = data_reply[2]
local yCord = data_reply[3]
local zCord = data_reply[4]
return xCord , yCord , zCord
end )
end
[/lua]
That [b]should[/b] work, if http.Get returns the return of the callback (Which it really should)
Otherwise use locals like above.
It returns an HTTPConnection object:
[url]http://wiki.garrysmod.com/?title=HTTPConnection[/url]
[QUOTE=_nonSENSE;23307097]Declare them outside of the function:
[lua]
function LoadDeed(ply)
local xCord, yCord, zCord
function http_reply(contents)
data_reply = string.Explode("|", contents)
xCord = data_reply[2]
yCord = data_reply[3]
zCord = data_reply[4]
end
http.Get("http://127.0.0.1/realwar/retrievedata.php?auth=datanet_687_PI&con=mapdata&map=" .. tostring(game.GetMap()), "", http_reply)
return xCord, yCord, zCord
end
[/lua][/QUOTE]
That doesn't work, I can't seem to get the variables if they are outside the function, but can if they are outside.
Sorry for the bump, but I still can't get this to work.
-snip- Wrong thread.
It takes some time for it to get the info from the website, but you instantly return the variables. There is a reason it takes a function as an argument.
AKA you'll have to do whatever you need to do with those values inside the http_reply function.
Sorry, you need to Log In to post a reply to this thread.