Returning a value of a function inside of a function

Okay so I absolutely HATE lua at this point. -_- so I have a question…

I’ve got this function


function getPlayerData( ply, row)
	tmysql.query("SELECT " .. row .. " FROM players WHERE SteamID = '" .. ply:SteamID() .. "'", function(Res, stat, Err)
		if err ~= nil or !stat then
			print("MySQL Error: " .. err)
			return
		end
		
		if Res[1][1] ~= nil then
			print(Res[1][1])
			return Res[1][1]
		else
			print("nil data");
			return 0
		end
	end)
end

I’m trying to return a value of the callbackfunc for tmysql but it’s not as retardly simple as I thought it would be. Can someone please tutor me how to solve this issue?

I’d like Lua if it had an official class implementation, events & delegates. But I guess it would be pretty much Python at that point.

It might help others if you posted errors.

True. True. And I’m getting nil and unset value errors, because the return values aren’t outputting any information. I’ll try to explain.

I have this function…



function meta:setPoints()
	self:SetNetworkedInt( "points", getPlayerData( self , "Points", 0 ))
	self:savePoints()
end

and im using getPlayerData() to get information from the database and setting the players information upon joining.


function getPlayerData( ply, row)
	tmysql.query("SELECT " .. row .. " FROM players WHERE SteamID = '" .. ply:SteamID() .. "'", function(Res, stat, Err)
		if err ~= nil or !stat then
			print("MySQL Error: " .. err)
			return
		end
		
		if Res[1][1] ~= nil then
			print(Res[1][1])
			return Res[1][1]
		else
			print("nil data");
			return 0
		end
	end)
end

the getPlayerData() function isn’t returning anything. it’s giving me an error that nothing is set/returned. but I added print statements to make sure Res[1][1] wasn’t nil or anything, and it’s not it returns 1000 like it should be but when I return Res[1][1] it doesn’t actually return a value…

On another note, I completely agree lua needs some Object Orientation. The language doesn’t even have much information as practically a lost cause… like visual basics 6

You can’t return a value in a MySQL onSuccess function to the parent function to be returned and the reason is because the parent function finishes before the query has been executed fully. This means that by the time the return is called, the pointer is elsewhere.

CallBack / onSuccess is supposed to handle everything or call a hook, or whatever, in order to achieve the goal.

Don’t rely on instant responses for everything you code. The same will occur for http fetch, mysql query, and a few other things.

On a side note, objects can be achieved with Lua by using Metatables. You create a base / parent object, and then when you create a :New of that object, you have a copy of it that you can do x y and z with without changing the original. You can do this to as many subs as you want; here’s an example using cards: https://dl.dropboxusercontent.com/u/26074909/tutoring/_metatable_objects__classes.lua

The “class” is written in a way that the values of cards can be passed in without creating all of the cards; 0-51 = cards. 0-13 are the values of cards per suit, and 0-3 are suit values. This demo was created to show you could either go all out, create everything up front as constants, and copy them when you create a new “deck of cards” in this instance, or you can pass a value in to have the same effect without creating new objects.

What I would do it just load the data when the player joins, then save the modified data when they leave. This way you’re not relying on mysql calls each time you need to change their points.
Also don’t use Set/GetNetworkedInt

Kinda new to this language, umm okay so basically what I was doing is when the player logged in it set their points, and when the log out it saves their changes, not everytime they gain points. Also what should I use besided networked ints and how exactly will this fix the issue?

Read the side note from my previous post; I updated it regarding classes.

OnJoin, load their information to a table on the server; sync data across clients, or to the connecting client when necessary. Update data in the tables. OnDisconnect / better yet hook into game function player_disconnect, update the mysql.

While db is fast, it should be no replacement to managing data via tables. You can do a cache and save the updated data every 5, 10 minutes to ensure no data is lost, or the max that is lost is 5 to 10 minutes worth of data, and save that cache file on disconnect in batch format so that everyone gets saved at the same time.

Right, I’ve done cache maps before in java. That’s not too hard Thanks for the idea, but either way I feel like at somepoint I’m going to need to be able to return data from a mysql database so this problem isn’t necessarily solved yet :confused:

If done correctly, you won’t need to. Your onSuccess function should be able to handle it, it should be your “return” as it will manipulate the data which is passed to it; you’ll need to parse the rows of returned data if it’s a SELECT, etc… There are ways around needing to return right away…

Design helper functions that return what you need, then design your mysql stuff so that it doesn’t need to rely on returning. Set it up so it networks, or populates a table, and onSuccess do a hook.Call, and within that hook continue with processing where the return would’ve. Etc…