Can I do this (Struct destruction)?

Zwiebelchen

You can change this now in User CP.
Reaction score
60
The title says everything: Does this work properly?
JASS:

struct cat
	integer a
	unit b
	...

	method onDestroy takes nothing returns nothing
		call KillUnit(.b)
		set .b = null
	endmethod
endstruct

struct dog
	real c
	timer d
	...

	method onDestroy takes nothing returns nothing
		call DestroyTimer(.d)
		set .d = null
	endmethod
endstruct


function TestFunc takes nothing returns nothing
	local dog CatOrDog
	if GetRandomInt(1,2) == 1 then
		set CatOrDog = dog.create()
	else
		set CatOrDog = cat.create()
	endif
	call CatOrDog.destroy()
endfunction

In other words: Does the right onDestroy() method fire upon calling the .destroy() command? Can I disguise 'dog' with 'cat'? As far as I know, structs are only integers, so it should work, doesn't it? Which onDestroy() method fires when a 'cat' struct is destroyed by CatOrDog.destroy()?
 

Laiev

Hey Listen!!
Reaction score
188
>> In other words: Does the right onDestroy() method fire upon calling the .destroy() command?
Yes
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
>> In other words: Does the right onDestroy() method fire upon calling the .destroy() command?
Yes
No it doesn't. I tested it now. Only the matching destroy function fires. If I destroy a Catstruct which was declared as Dog type inside the function, nothing happens.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Why not just do this:
JASS:
 function TestFunc takes nothing returns nothing
	local dog CatOrDog
        local cat DogOrCat
	if GetRandomInt(1,2) == 1 then
            set CatOrDog = dog.create()
            call CatOrDog.destroy()
	else
	    set DogOrCat = cat.create()
            call DogOrCat.destroy()
	endif
endfunction
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
Why not just do this:
JASS:
 function TestFunc takes nothing returns nothing
	local dog CatOrDog
        local cat DogOrCat
	if GetRandomInt(1,2) == 1 then
            set CatOrDog = dog.create()
            call CatOrDog.destroy()
	else
	    set DogOrCat = cat.create()
            call DogOrCat.destroy()
	endif
endfunction
You do realize that this was just an example with no practical sense, don't you?


Use interface.
Hmm, can you be more specific? I never used interfaces before.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
function TestFunc takes nothing returns nothing
	local dog CatOrDog
	if GetRandomInt(1,2) == 1 then
		set CatOrDog = dog.create()
                call CatOrDog.destroy()
	else
		set CatOrDog = cat.create()
                call cat(CatOrDog).destroy()//typecasting of struct.
	endif
endfunction

The type of struct is declared as dog. It will auto typecasted even you used it to store cat. However, you may use typecasting to make it back
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top