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 variable assignment.

 
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: Sun Jul 22, 2018 8:33 am    Post subject: [LUA]Questions about variable assignment. Reply with quote

I am reading some LUA codes from an addon for World of Warcraft. I don't understand the following:

First,
Code:

local format, strfind, strjoin = format, strfind, strjoin
local tostring, unpack = tostring, unpack

What do these assignments do? There are no values being assigned.

Second,
Code:

local newGroup = IsInRaid() and "raid" or IsInGroup() and "party" or false

Whats the logic of this assignment? I wish there are some brackets here to help me understand...

Thanks a lot.

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

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Sun Jul 22, 2018 9:27 am    Post subject: This post has 1 review(s) Reply with quote

The first is copying global (maybe upvalue) variables to local variables. This could be a minor optimization in a critical part of the code.

The second is the closest thing Lua has to a ternary conditional operator. The "and" and "or" operators short-circuit: if the first operand to "and" / "or" evaluates to false / true respectively, the expression will return the first operand and the second operand will not be evaluated.
That code is equivalent to this:
Code:
local newGroup
if IsInRaid() then
  newGroup = "raid"
elseif IsInGroup() then
  newGroup = "party"
else
  newGroup = false
end

_________________
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
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Mon Jul 23, 2018 5:57 pm    Post subject: This post has 1 review(s) Reply with quote

First one is generally either done for one of two reasons:

1. The vars are within a module.
2. The vars are used to make ease of calling of a function.

In your case, it looks more like the first, or just bad coding in general. When you define a module in Lua, the globals are moved into your modules table, meaning that they technically 'break' for normal usage. More info on that: http://lua-users.org/wiki/LuaModuleFunctionCritiqued

Module creation like this shouldn't be used anymore though since it is deprecated at this point.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Mon Jul 23, 2018 8:09 pm    Post subject: Reply with quote

Thank you Penguin and atom0s for the explanations.

+rep. Smile

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

A simple example is better then ten links. Very Happy
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