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 


How do I read all values a lua script or function returned??

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Wed Feb 27, 2013 1:27 pm    Post subject: How do I read all values a lua script or function returned?? Reply with quote

Hi,

I'm executing this lua code:
Code:
local m = getMemoryViewForm()
if m ~= nil then
return disassemblerview_getSelectedAddress(memoryview_getDisassemblerView(m))
end
return 0


This is how I do it (on cheatengines main thread of course!):

Code:

int originalStack = GetTop(); << returns zero
LoadString(luaCode);
int preCallStack = GetTop(); << returns 1 because the code is on the stack
ProtectedCall(0, -1, 0); <<< just a wrapper for the c-function: "pcall"
int postCallStack = GetTop(); << returns 2, why???
...


Now what I don't understand:
originalStack = 0
preCallStack = 1
so far so good, but after the protected call why is postCallStack = 2?
It should be 1 or not ??

Now when I look what values there are on the stack like this:
Code:

string a = GetString();
Pop(1);
string b = GetString();


a and b are the same value (always the correct one, the currently selected line in the memory view).

So why does "return disassemblerview_getSelectedAddress..." push the the return value on the lua stack twice ??

Now another example (lua)
Code:

return getOpenedProcessID()


when this is the code I am executing then the stack is like this:
originalStack = 0
preCallStack = 1 (the value on the stack is the lua-function)
postCallStack = 1 (the value on the stack here is the return value of the called function)

That single value on the stack is correct. (the process it of CEs target)

My understanding is that pcall pops the function off the stack and only leaves the return values.
But it seems that is only the case for my second example.
In my more complex code (the first example) there are suddenly two (same) return values.
Why is it like this?

In general, how do I get the number of values a lua script returned?
I always thought the difference between the stack-top before and after the "pcall" is the number of return values ??
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25288
Location: The netherlands

PostPosted: Wed Feb 27, 2013 2:51 pm    Post subject: Reply with quote

disassemblerview_getSelectedAddress() returns 2 results. The currently selected address and the secondary address (when multiselecting lines, the same as the first one otherwise).

That is why postCallStack is 2 because there are 2 values returned.

I assume that your second parameter is the nResults parameter. If you change that from -1 to 1 it will only return 1 result (the first one)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Chris12
Expert Cheater
Reputation: 1

Joined: 27 Apr 2012
Posts: 103

PostPosted: Wed Feb 27, 2013 11:49 pm    Post subject: Reply with quote

Ahhh
That clears up everything. Thanks 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 -> Cheat Engine Lua Scripting 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