• 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.

Taking any struct as an arg for function?

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

Suppose I've got a bunch of structs which are different kinds of wrappers around unit but they don't actually have any inheritance/extension relations.

Let's further suppose they all have a unit instance field called "u."

Now suppose I want to write a single function that disables/enables invulnerability for that unit u. Is this possible? It'd be up to the client to make sure they pass in a struct which has a unit field named "u."

JASS:
function setStructUnitInvulnerable takes integer whichStruct, boolean flag returns nothing
  call SetUnitInvulnerable(whichStruct.u, flag)
endfunction

I don't think that works yet, but is there a way to make this function?
 
It isn't possible without interfaces/extensions. It is probably better to input the unit directly. Otherwise, you'll end up with a bunch of excess code determining the type of struct. The reason why is that there are multiple variables that are potentially being accessed by whichStruct.u (it could be the "u" variable of struct A, or struct B, or some other struct, etc.). If you consider it from a regular JASS perspective, it would require some really weird if/then/else structure, since JASS isn't that dynamic when it comes to variables.
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Would it be possible with an interface?

Here's some expanded code, or would this not work the way I outlined in the OP.

Side question: Do interfaces contribute to the max struct count? I wouldn't think so, but just wish to be certain.

JASS:
interface Unit
  unit u
endinterface

struct A extends Unit
  ...
endstruct

struct B extends Unit
  ...
endstruct

function killStructUnit takes Unit whichUnitStruct returns nothing
  call KillUnit(whichUnitStruct.u)
endfunction

I know it would work if I made interface its own struct, but then that would run into problems, as I've got about 6-7 structs with a unit field, and I think together they would get over the 8190 JASS_MAX_ARRAY_SIZE.

It is probably better to input the unit directly.

This method doesn't work, because 99% of the time the actual unit handle won't exist until actual gameplay, and it's better to pass a reference to it, as the function call which operates on the unit handle doesn't happen until the unit handle isn't null. This is so I can write out all the scripts in a clean API before hand--for example, if I am scripting the death of a unit in the quest, there's no way I can have a handle on it before runtime, if I don't want to arbitrary about it, as that unit won't have existed until the player is at that part in the quest (and no, I'm not preplacing units).
 

Deleted member 219079

D

Deleted member 219079

I think you need to use interfaces. Interfaces use a lot of executefuncs so try to avoid using them.
 
Status
Not open for further replies.
Top