HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

vJASS - Objects with Condition storing.

09-08-2009, 04:48 PM#1
Anachron
Hey.

Just wondering: How do I store a method in an object?
I want to be able to get the return value of a method I
give with parameters...

But I don't know how to do it?
09-08-2009, 05:22 PM#2
Zerzax
Function interfaces?
09-08-2009, 05:55 PM#3
Anachron
No, I want something like this:

Collapse JASS:
struct Test
   func toCall = null   

   static method create takes func fc returns thistype
      local thistype tst = Test.allocate()
      set tst.toCall = fc
      return tst
   endmethod
  
   public method checkCon takes nothing returns nothing
      if .toCall() then
          call BJDebugMsg("CONDITION TRUE!")
      endif
   endmethod

endstruct

public function Condition takes nothing returns boolean
   return true
endfunction

// Call from user
call Test.create(function XX)
09-08-2009, 06:09 PM#4
0zyx0
Function Interfaces!
09-08-2009, 06:54 PM#5
Anachron
Can someone please post the new code how it will look like with function interfaces?
09-08-2009, 07:05 PM#6
moyack
Collapse JASS:
function interface OnTest takes nothing returns nothing 

struct MaTest
    OnTest T
    static method create takes OnTest T returns thistype
        local thistype M = thistype.allocate()
        set M.T = T
        return M
    endmethod
endstruct

function Lol takes nothing returns nothing

endfunction

call MaTest.create(Lol)
09-09-2009, 07:26 AM#7
Anachron
Thanks alot! :)
09-09-2009, 03:10 PM#8
azlier
Technically, this is such a lucky situation you have that you could get away with not using function interfaces. Just use a trigger and evaluate it. Do whatever you want.
09-09-2009, 03:13 PM#9
Anachron
I don't want to evaluate triggers, I just wanted function interfaces, thanks anyway! :)
09-09-2009, 03:51 PM#10
Vexorian
...Except using triggers would suck misserably.

I think a single interface will look better, but hey it is just a matter of taste.

And for the n-th time: It is vJass !
09-10-2009, 07:25 AM#11
Anachron
What am I doing wrong?

Collapse JASS:
function interface onSpawn takes nothing returns nothing
    function interface onCheck takes nothing returns boolean
    function interface onUnitSpawn takes unit u returns nothing  
    
    struct SharedObjects
        onSpawn spawnCall = null
        onCheck checkCall = null
        
        public method setOnSpawnCall takes onSpawn onS returns nothing
            set .spawnCall = onS
        endmethod
        
        private method runOnSpawnCall takes nothing returns nothing
            if .spawnCall != null then
                call .spawnCall()
            endif
        endmethod
        
        public method setOnSpawnCheck takes onCheck onC returns nothing
            set .checkCall = onC
        endmethod
        
        private method runOnSpawnCheck takes nothing returns nothing
            if .checkCall != null then
                return .checkCall()
            endif
            return true
        endmethod
    endstruct

Error:
Collapse JASS:
call .spawnCall()
is not an function.
09-10-2009, 08:42 AM#12
Earth-Fury
Use .evaluate or .execute with function interfaces. Also, function interfaces are not handles, thus you use 0, not null.
09-10-2009, 08:47 AM#13
Anachron
Collapse JASS:
    function interface onSpawn takes nothing returns nothing
    function interface onCheck takes nothing returns boolean
    function interface onUnitSpawn takes unit u returns nothing  
    
    struct SharedObjects
        onSpawn spawnCall = 0
        onCheck checkCall = 0
        
        public method setOnSpawnCall takes onSpawn onS returns nothing
            set .spawnCall = onS
        endmethod
        
        private method runOnSpawnCall takes nothing returns nothing
            if .spawnCall != 0 then
                call .spawnCall.execute()
            endif
        endmethod
        
        public method setOnSpawnCheck takes onCheck onC returns nothing
            set .checkCall = onC
        endmethod
        
        private method runOnSpawnCheck takes nothing returns nothing
            if .checkCall != 0 then
                return .checkCall.evaluate()
            endif
            return true
        endmethod
    endstruct

Is this correct?
09-10-2009, 08:50 AM#14
Earth-Fury
Quote:
Originally Posted by Anachron
Is this correct?

If it compiles and works, then probably. Of course, that's usually true.
09-10-2009, 08:59 AM#15
Anachron
Don't know if it compiles, don't have a compiler here.

Really thanks mate, you are very helpful.