• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] A way around interface?

Status
Not open for further replies.
Level 5
Joined
Dec 1, 2008
Messages
120
Hello.

I'll just go ahead and paste some code here first:

JASS:
library Object

    interface Object
    
        method onCreate takes nothing returns nothing defaults nothing
    
    endinterface
    
    module ObjectModule
    
        static method create takes nothing returns thistype
            local thistype this = allocate()
            
            // do stuff for every Object
            
            static if (thistype.onCreate.exists) then
                call .onCreate()
            endif
            
            return this
        endmethod
    
    endmodule

endlibrary

library ObjType uses Object

    struct ObjType extends Object
    
        method onCreate takes nothing returns nothing
            // do ObjType specific stuff
        endmethod
    
        implement ObjectModule
    endstruct

endlibrary

Now when looking at the outcome it's pretty much a disaster, and I'd like to have as little disaster as possible. I read the "The Art of Modulo Interfaces" tutorial by Nestharus, but I just couldn't apply it to my situtation in any way. So, is there a way to write this in some nice and clean way without vJass interfaces? Thanks.
 
Give me a bit, this isn't what you wanted, lol. Putting up this code so that you can see how you'd run events ;P.

JASS:
library Object uses Event
	globals
		private Event createEvent
		private integer created = 0
	endglobals

	private module Init
		private static method onInit takes nothing returns nothing
			call init()
		endmethod
	endmodule

	struct Object extends array
		implement Alloc

		static method create takes nothing returns thistype
			local thistype this = allocate()

			local integer prev = created
			set created = this
			call createEvent.fire()
			set created = prev

			return this
		endmethod

		private static method init takes nothing returns nothing
			set createEvent = Event.create()
		endmethod

		implement Init
	endstruct

    module ObjectModule
		static if thistype.onCreate.exists then
			private static method doCreate takes nothing returns boolean
				call thistype(created).onCreate()

				return false
			endmethod

			private static method onInit takes nothing returns nothing
				call createEvent.register(Condition(function thistype.doCreate))
			endmethod
		endif
	endmodule
endlibrary

library ObjType uses Object
    struct ObjType extends array
        method onCreate takes nothing returns nothing
            // do ObjType specific stuff
        endmethod
    
        implement ObjectModule
    endstruct
endlibrary

What you want is actually very simple ;). One moment.

JASS:
library Object
	struct Object extends array
		implement Alloc

		static method create takes nothing returns thistype
			local thistype this = allocate()

			//code?

			return this
		endmethod
	endstruct
endlibrary

library ObjType uses Object
    struct ObjType extends array
	    private delegate Object obj

        static method create takes nothing returns nothing
            local thistype this = Object.create()

	        set obj = this //for inheritance

            //code?

            return this
        endmethod
    endstruct
endlibrary

edit
I highly recommend that you know these 3 tutorials

http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/art-module-interfaces-188742/ (you already read this one)
http://www.hiveworkshop.com/forums/...ls-280/coding-efficient-vjass-structs-187477/
http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/custom-event-data-187478/

From here, know these for sure

http://www.hiveworkshop.com/forums/jass-resources-412/snippet-alloc-alternative-221493/
http://www.hiveworkshop.com/forums/jass-resources-412/collections-index-239205/
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-event-186555/
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
http://www.hiveworkshop.com/forums/jass-resources-412/repo-order-ids-197002/
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-working-ai-natives-184297/
http://www.hiveworkshop.com/forums/jass-resources-412/system-item-cleanup-175663/
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/
 
Hey uber! Welcome sort of back.

Anyway, I don't see why your code wouldn't work without the interface.
JASS:
library Object

    /* interface Object
    
        method onCreate takes nothing returns nothing defaults nothing
    
    endinterface */
    
    module ObjectModule
    
        static method create takes nothing returns thistype
            local thistype this = allocate()
            
            // do stuff for every Object
            
            static if (thistype.onCreate.exists) then
                call .onCreate()
            endif
            
            return this
        endmethod
    
    endmodule

endlibrary

library ObjType uses Object

    struct ObjType // extends Object
    
        method onCreate takes nothing returns nothing
            // do ObjType specific stuff
        endmethod
    
        implement ObjectModule
    endstruct

endlibrary

Unless you need polymorphism in the sense of calling a specific method from "Object".

Tbh, if you want proper OO/inheritance you may want to look into wurstscript.
 
Level 5
Joined
Dec 1, 2008
Messages
120
Hello, Purge! Also thanks for the links Nestharus, I'm already using your Alloc Alternative and ErrorMessage. The Event snippet looks very useful too. I'm kinda surprised how much the WC3 modding has improved after all these years.

Anyway, I might've actually given a very, very bad example code, sorry. Tried to keep it too simple I suppose, and left out the essential stuff.

JASS:
library Object

    interface Object
    
        method onSetX takes real x returns boolean defaults true
        method onSetY takes real y returns boolean defaults true
    
        method operator x takes nothing returns real
        method operator x= takes real x returns nothing
        method operator y takes nothing returns real
        method operator y= takes real y returns nothing
    
    endinterface
    
    module ObjectModule
    
        private real p_x
        method operator x takes nothing returns real
            return .p_x
        endmethod
        method operator x= takes real x returns nothing
            set .p_x = x
        endmethod
        
        private real p_y
        method operator y takes nothing returns real
            return .p_y
        endmethod
        method operator y= takes real x returns nothing
            set .p_y = y
        endmethod
    
    endmodule

endlibrary

library ObjType uses Object

    struct ObjType1 extends Object
        implement ObjectModule
    endstruct
    
    struct ObjType2 extends Object
        implement ObjectModule
    endstruct

endlibrary

library Physics uses Object

    struct Physics extends array
    
        private static Object array objArray
    
        private static method update takes nothing returns nothing
            local real x
            local real y
            local integer i = 0
            
            loop
                // Looping through Objects (of any type) added to the Physics engine
                
                // Calculate the physics for each Object
                
                // Move them to new coordinates if optional onSetX/Y method doesn't prevent that.
                if (objArray[i].onSetX(x)) then
                    set objArray[i].x = x
                endif
                if (objArray[i].onSetY(y)) then
                    set objArray[i].y = y
                endif
                
            endloop
        endmethod
        
        private static method onInit takes nothing returns nothing
            call TimerStart(CreateTimer(), .03125, true, function thistype.update)
        endmethod
    
    endstruct

endlibrary
It's a bit long, yes. Also I am having a very hard time writing a good example. The method operator stuff looks kinda useless, but that's where I animate the Objects, just didn't feel it was necessary to code that part for this.
 
Status
Not open for further replies.
Top