• Print

Author Topic: Make a function call break out of call stack  (Read 5417 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Make a function call break out of call stack
« on: June 12, 2017, 07:29:39 am »
I didn't really know how to word this, so I'll try to explain it best I can. Basically what I'm asking is; similar to error, how can I make a command that, when called, throws a message then breaks out of the current call stack, without throwing an actual Lua error?
My thought was:

Code: Lua
  1. function ASET.Break( message )
  2.     ServerLog( message ) -- Or some other way to show message, but I'll figure that out later.
  3.     return -- Would this cause the call stack to end?
  4. end
  5.  
Does that work, or am I going to have to do something like "return ASET.Break( "message" )" in the function I want to end?

« Last Edit: June 12, 2017, 07:33:04 am by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Make a function call break out of call stack
« Reply #2 on: June 12, 2017, 02:19:23 pm »
Right, I do understand a "return" will cause a call stack break in the current function, but I'm trying to see if there is a way to make another function cause the stack to end. Such as:
Code: Lua
  1.  
  2. function ACHIEVEMENT:Unlock( ply, type, group, reward, item ) -- REQUIRED: This determines what to do once an achievement has been unlocked. If this function is not present, this achievement will not be loaded.
  3.     if not type or type ~= "kills" or type ~= "time" or type ~= "amount" then
  4.         ASET.Break( "Invalid type " .. tostring( type ) or "nil" .. " given." )
  5.     end
  6.     if hook.Call( "ASETAchievementUnlocked", nil, ply, ACHIEVEMENT.Type ) == false then return end
  7.     net.Start( "Achievement_Unlocked" )
  8.         net.WriteString( type ) -- Only guaranteed val to write.
  9.         if group then net.WriteString( group ) end
  10.         if reward then net.WriteInt( reward, 32 ) end -- Sign high, because I'm not sure what your reward will be.
  11.         if item then net.WriteString( item ) end -- Should be item class
  12.     if ply then net.Send( ply ) else net.Broadcast() end
  13. end
  14.  

And then I use my previous Break() function:
Code: Lua
  1. function ASET.Break( message )
  2.     ServerLog( message ) -- Or some other way to show message, but I'll figure that out later.
  3.     return -- Would this cause the call stack to end?
  4. end
  5.  
So I tried running this on repl.it and that didn't seem to work. I don't like the look of having scattered "return"s in my code, so I'd like to have a function do it.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Make a function call break out of call stack
« Reply #3 on: June 12, 2017, 05:24:14 pm »
see if there is a way to make another function cause the stack to end
Not the way you seem to be wanting.
You'd have to return data to the calling function, the calling function would have to check the data, and if it matched your criteria, end.

A("error")

function A(blah)
    if B(blah) then return
end

function B(blah)
    if blah = "error" return true
    else return .. nil is false
end
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Make a function call break out of call stack
« Reply #4 on: June 12, 2017, 06:55:15 pm »
Got it. I thought there might've been a way to simply call a command to end call stack, but I guess that's not the case.

Thanks for trying to help, though, I understand what you mean now. I do understand how return works but I guess I was looking for a "cleaner" way (IMO).

Oh well, thanks anyways :)
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print