• Print

Author Topic: Questions! Questions! Questions!  (Read 28600 times)

0 Members and 1 Guest are viewing this topic.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Questions! Questions! Questions!
« on: February 28, 2015, 01:50:24 am »
Code: Lua
  1. if#data > 0 then

What does #data mean ?
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Questions! Questions! Questions!
« Reply #1 on: February 28, 2015, 08:28:12 am »
http://www.lua.org/manual/5.1/manual.html#2.5.5 This about sums it up.

Basically, it's a very simple (and fast) count operator that only works on tables that have indexed items from 1 to n, where there are no nil values between 1 to n.
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Questions! Questions! Questions!
« Reply #2 on: February 28, 2015, 08:30:43 am »
In lua 5.1, # returns the length of variable after it.
Though, I too would like more clarity from another of our advanced lua members, because I've seen it used in ULX for many years, and in our case it normally used for variable substitution.
Like so
ulx.fancyLogAdmin( calling_ply, "#A ungagged #T", target_plys )
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Questions! Questions! Questions!
« Reply #3 on: February 28, 2015, 09:42:11 am »
The way it's used in substitution is a string operator for string formatting.
The way it's used in the first post is, like Stickly mentioned, a quick way to return a count of the table.


for example:
Code: Lua
  1. local tab = {"red", "blue", "green", "orange", "turquoise", "yellow" }
  2.  
  3. print( #tab )
  4.  

The output would be 6. Since there are 6 items in the table 'tab'

The example given by Apple in the first post is simple a conditional if statement checking to make sure that the table 'data' has information in it.

Note: This does not return anything for tables without incremental numerical keys.

Example:
Code: Lua
  1. local tab = { test = 1, test2 = 2, test3 = 3, test4 = 4 }
  2. print( #tab )
  3.  

This would return 0.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Questions! Questions! Questions!
« Reply #4 on: February 28, 2015, 02:03:58 pm »
Alright, so when I was a really nooby lua scripter, I didn't really understand how anything worked. My friend whom was really experienced in web development, and lua made a script for me that basically is a global chat system (except I didn't use sockets because I refused to do that (/index.php/topic,5516)).

I recently remade it because I've actually much better than him now, and when I was remaking it, I used some of his code as I didn't understand what it did (#), and recently it came into question on what it still does because I did not know still. Though, because this term is still new to me, I'd like some clarification on this seeing as I may still not understand it fully.

Code: Lua
  1. local Get_LastID_Query = MPGCserver:query("SELECT * FROM chat ORDER BY id DESC LIMIT 1")
  2.         Get_LastID_Query:start()
  3.        
  4.         function Get_LastID_Query:onSuccess( data )
  5.         if #data > 0 then
  6.                 lastID = data[#data].id
  7.         end
  8.  

From my extensive knowledge of mysql, I can see that it requests the highest last number from the database. Then the function gets the data, but my question is, how does this work, what does it mean:

Code: Lua
  1.         if #data > 0 then
  2.                 lastID = data[#data].id
  3.         end
  4.  

From my idea on it, it basically counts the data in the if statement, then for the lastID, it does well, I do not know, that's really where my question is at this time.
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Questions! Questions! Questions!
« Reply #5 on: February 28, 2015, 02:44:42 pm »
Gets the data, puts it in table "data".
Checks the data to make sure it's not nil/empty/broken/missing data>0
#data will always be the highest entry number
assigns that highest/last entry to table lastID as data<"data" table length number>.id

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Questions! Questions! Questions!
« Reply #6 on: February 28, 2015, 04:54:51 pm »
I love this little family we all have here on this community.
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Questions! Questions! Questions!
« Reply #7 on: February 28, 2015, 06:34:50 pm »
So new question. Is there a way to retrieve the AVATAR LINK from garry's mod?
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Questions! Questions! Questions!
« Reply #8 on: February 28, 2015, 07:02:44 pm »

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Questions! Questions! Questions!
« Reply #9 on: February 28, 2015, 07:12:02 pm »
Yes, there is.
https://developer.valvesoftware.com/wiki/Steam_Web_API

Yeah, I know that already, but it's too slow through web. I was looking for a way to do it while in game.

I've come up with a new idea, but I request assistance with http.Post/http.Fetch. As I understand MrPresident is familiar with this, and I would like to know if this is possible.

I'm looking for an ability to basically push two values to a website php file.
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Questions! Questions! Questions!
« Reply #10 on: February 28, 2015, 07:21:27 pm »
If you're looking to get their avatar in game;
http://wiki.garrysmod.com/page/Category:AvatarImage

As far as using fetch and post, what are you trying to pass to a php script and are you familiar with $_GET in php?

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Questions! Questions! Questions!
« Reply #11 on: February 28, 2015, 07:28:50 pm »
http://wiki.garrysmod.com/page/http/Post

in lua:
Code: Lua
  1. local var1 = "test1"
  2. local var2 = "test2"
  3. local tbl = { tosend1 = var1, tosend2 = var2 }
  4.  
  5. http.Post( "http://www.mydomain.com/myscript.php", tbl, function() print("Success") end, function() print("Failed") end )
  6.  


in php:
Code: PHP
  1. $var1 = htmlspecialchars($_GET["tosend1"]);
  2. $var2 = htmlspecialchars($_GET["tosend2"]);
  3.  
  4. echo $var1;
  5.  

Would print "test1" to the page.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Questions! Questions! Questions!
« Reply #12 on: February 28, 2015, 07:29:54 pm »
If you're looking to get their avatar in game;
http://wiki.garrysmod.com/page/Category:AvatarImage

As far as using fetch and post, what are you trying to pass to a php script and are you familiar with $_GET in php?

Yes I am familiar with $_GET. Though, what I'm trying to do is push SteamID64() through a php script. Then the php script will get the string. This is what I have right now, but I doesn't send the string.

Code: Lua
  1. function UpdateStuffPlz(ply)
  2. testtest = { "hello", "there", "my", "name", "is", "drakehawke" }
  3.  
  4. http.Fetch( "http://BLOCKED.com/BLOCKED.php?steamid="..tostring(ply:SteamID64()).."", function() MsgN("Working") end, function() MsgN("Not Working") end )
  5. end
  6. hook.Add('PlayerSpawn','PlayerInitialSpawnUpdatesAvatarsPics',UpdateStuffPlz)
  7.  


----

I've seen that you've posted before I could post my reply, so I'll test what you've given me.
« Last Edit: February 28, 2015, 07:32:17 pm by Bite That Apple »
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Questions! Questions! Questions!
« Reply #13 on: February 28, 2015, 07:33:24 pm »
you use http.Fetch to read the contents of a website file.
You use http.Post to send things to a php file on a web server.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Questions! Questions! Questions!
« Reply #14 on: February 28, 2015, 07:37:00 pm »
Now, here's where it can get kind of confusing...

If you need for it to use those two variables to generate a page that you want to return back to the gmod client/server you'll need to pass the information the old fasioned way in the URL string using http.Fetch

I'm not exactly sure what you're trying to accomplish so I don't know what method you need to use.

  • Print