Error when call " lua_call() " in PB

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 782
Joined: Fri Dec 04, 2015 9:26 pm

Error when call " lua_call() " in PB

Post by skinkairewalker »

hi everyone !

i have a problem when i call > lua_call () function in PB

screnshot > http://prntscr.com/dp4jkt

someone know how can i fix it ?
User avatar
Dadido3
User
User
Posts: 54
Joined: Sat Jan 12, 2008 11:50 pm
Location: Hessen, Germany
Contact:

Re: Error when call " lua_call() " in PB

Post by Dadido3 »

Hi,

there are several things going wrong in your example. But the main reason it crashes is that you try to call a lua function which doesn't exist.
The first thing you should do is to use lua_pcall instead of lua_call, as this won't crash your application, but instead give you a nice and readable error message of what went wrong.
The second thing is that you are trying to get the global variable "CB_OnLoop" which doesn't exist yet. You need to run your lua file first in which this function is declared.
Additionally to that you need to pop the result elements from the stack after lua_pcall or lua_call, otherwise they will stay there forever.

Example:

Code: Select all

; #### Push functions and arguments on the stack
lua_getglobal(*Lua_State, "CB_OnLoop") ; function to be called
lua_pushstring(*Lua_State, "HelloWorld :)") ; push first argument

; #### Do the call (1 argument, 1 result). This pops the function and an argument from the stack, and pushes the result on the stack
If lua_pcall(*Lua_State, 1, 1, 0)
  Debug "Error: " + PeekS(lua_tostring(*Lua_State, -1), -1, #PB_UTF8)
EndIf

; #### Retrieve result
If Not lua_isnumber(*Lua_State, -1)
  Debug "Result is not a number"
EndIf

Debug "Result is: " + lua_tonumber(*Lua_State, -1)
lua_pop(*Lua_State, 1) ; Pop returned value(s) from stack (This has to be the same as the r parameter in pcall)
Btw, if you get an error with the lua_pcall macro, just redownload my include. Just fixed a silly mistake there.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 782
Joined: Fri Dec 04, 2015 9:26 pm

Re: Error when call " lua_call() " in PB

Post by skinkairewalker »

thanks by you answer Dadido3 :D
Post Reply