Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[LUA]Questions about _G.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Sat Aug 11, 2018 11:11 am    Post subject: [LUA]Questions about _G. Reply with quote

Code:

local x = "some string..."
_G[x] = 5print(_G["some string..."])


If I build this, the output is "5".

Code:

local x = "some string..."
_G[x] = 9087print(_G["some string..."])


If I build this, the output is "9087".

Why? I don't understand the second line of the code, how come there is a number in front of "print"? Is that a function of some sort? If not, what is it? It doesn't make any sense to me.... Sad

Thanks.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sat Aug 11, 2018 11:32 am    Post subject: This post has 2 review(s) Reply with quote

What are you trying to do exactly?

Code:
local x = "some string..."
_G[x] = 5print(_G["some string..."])

with this you set "_G[x]" to the value of 5, and the first time it's ran you'll just print "nil".

And with the other you do the same but set it to 9087 instead of 5. It's doing exactly what you told it to do.


If you change the code a bit:
Code:
local x = "some string..."
_G[x], s = 5, print(_G["some string..."])
print(_G[x])
print(s)

The first time it's ran you'll get.
Quote:

5


And the next time it's ran you'll get.
Quote:
5
5


Because you just set every thing to 5, for whatever reason.

This "_G["some string..."]" is the exact same variable as "_G[x]" in your code.
SO this:
Code:
local x = "some string..."
_G[x] = 5print(_G["some string..."])

Is the same as this:
Code:
local x = "some string..."
_G[x] = 5print(_G[x])

_________________


Last edited by TheyCallMeTim13 on Sat Aug 11, 2018 11:42 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Sat Aug 11, 2018 11:40 am    Post subject: Reply with quote

It's how Lua parses tokens. "9087print" isn't a valid number, so the parser splits it up and interprets "9087" as a number and "print" as an identifier. The identifier doesn't make sense if it was part of the assignment, so the parser will interpret that as two separate statements: an assignment and a function call.

Use Google for information on what _G is. (_ENV may also be relevant)

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Sat Aug 11, 2018 11:55 am    Post subject: Reply with quote

@TheyCallMeTim13

@ParkourPenguin

Thank you both for the quick answer. I get it now. Isn't it a little weird that Lua parses tokens this way? xD

+rep if I can

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sat Aug 11, 2018 12:06 pm    Post subject: Reply with quote

I thinks it's why most languages (if not all, just not sure), don't allow variables to start with numbers.

So:
Code:
5var = 5

Isn't valid.

But this is:
Code:
_5 = 5

_________________
Back to top
View user's profile Send private message Visit poster's website
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Sat Aug 11, 2018 12:12 pm    Post subject: Reply with quote

It's a consequence of not requiring an explicit token to end statements (e.g. semicolon in C / C++). Lua could make a rule that disallows this, but if it's not hurting anything, why bother?
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites