Jesus4Lyf
Good Idea™
- Reaction score
- 397
Ever noticed that WC3 does not allow code type arrays?
In other words...
Absolutely breaks. Can't do it!
Introducing the answer to the question nobody asked:
CodeVar!
Interface:
Library:
Demonstration:
Furthermore, these structs don't leak. As in, you never need to destroy them. Calling CodeVar.create(function SomeSpecificFunc) will always result in the exact same struct. These pretty much are conditions, except you can get the code back out of them, and use code that doesn't return a boolean, and tell it to execute/evaluate directly.
Imagine that!
In other words...
Absolutely breaks. Can't do it!
Introducing the answer to the question nobody asked:
CodeVar!
Interface:
JASS:
local CodeVar myCodeVar = CodeVar.create(function x)
OR
local CodeVar myCodeVar
set myCodeVar.code=function x
myCodeVar.code --> function x
call myCodeVar.exec()
call myCodeVar.eval()
if myCodeVar.eval() then
call TriggerAddCondition(SomeTrig, Condition(myCodeVar.code))
endif
etc...
Library:
JASS:
library CodeFuncs
struct CodeVar // By Jesus4Lyf
private static constant trigger Trig=CreateTrigger()
static method create takes code f returns CodeVar
return f
call DoNothing()
return 0
endmethod
method operator code takes nothing returns code
return this
call DoNothing()
return null
endmethod
method operator code= takes code f returns CodeVar // Crashes if changed to int
return .create(f)
endmethod
private static method execCode takes code f returns nothing
call TriggerClearActions(.Trig)
call TriggerAddAction(.Trig,f)
call TriggerExecute(.Trig)
endmethod
private static method evalCode takes code f returns boolean
call TriggerClearConditions(.Trig)
call TriggerAddCondition(.Trig,Condition(f))
return TriggerEvaluate(.Trig)
endmethod
method exec takes nothing returns nothing
call .execCode(.code)
endmethod
method eval takes nothing returns boolean
return .evalCode(.code)
endmethod
endstruct
endlibrary
Demonstration:
JASS:
function Test takes nothing returns boolean
call BJDebugMsg("Magic.")
return false
endfunction
function NotMagic takes nothing returns nothing
call BJDebugMsg("NOT magic! Just JASS. Or so they say... <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink ;)" loading="lazy" data-shortname=";)" />")
endfunction
globals
CodeVar array demo
endglobals
function Demonstration takes nothing returns nothing
set demo[1].code=function Test // Same as line 3
set demo[2]=CodeVar.create(function NotMagic)
set demo[3]=CodeVar.create(function Test) // Same as line 1
call demo[1].eval()
call demo[2].exec()
call demo[3].eval()
if demo[1].code==function Test then
call BJDebugMsg("Yep, same")
endif
if demo[2]==CodeVar.create(function NotMagic) then
call BJDebugMsg("Zomg, also same")
endif
endfunction
Furthermore, these structs don't leak. As in, you never need to destroy them. Calling CodeVar.create(function SomeSpecificFunc) will always result in the exact same struct. These pretty much are conditions, except you can get the code back out of them, and use code that doesn't return a boolean, and tell it to execute/evaluate directly.
Imagine that!
You can actually skip lines of code by incrementing the struct by 8.
If you ignore how retarded this is or never realise, this could be really cool to code with!
If you ignore how retarded this is or never realise, this could be really cool to code with!