| 09-08-2007, 10:07 AM | #1 |
Just a quick question.. JASS:struct StructData real r = 0//"NORMAL" value endstruct function Func takes nothing returns nothing local StructData d = StructData.create() set d.r = 5 call SetUnitUserData(GetTriggerUnit(), d.r) call StructData.destroy(d) endfunction If i used call StructData.destroy(struct) will it reset the struct's data back to "normal"? Will the real r be back to 0? |
| 09-08-2007, 10:14 AM | #2 |
No, that is done when the struct is created. Edit: There is always the onDestroy method, where you could set r to 0. |
| 09-08-2007, 10:26 AM | #3 |
Wouldn't the correct syntax for destroying that struct be d.destroy()? |
| 09-08-2007, 10:31 AM | #4 | |
Quote:
But interfaces can't have that method.. :| |
| 09-08-2007, 10:58 AM | #5 |
Why do you need them to have it? I don't know much about interfaces, but as I understand them they allow you to have any-struct-type.same-method-name() You can already have any-struct-type.destroy() which will call the onDestroy method for that struct type. |
| 09-08-2007, 01:22 PM | #6 | |
Quote:
-- The only reason you would have to do this would be that you are actually accessing the struct after you destroy it? That's something I would not really recommend to be ever done. |
| 09-08-2007, 01:35 PM | #7 |
Well it can't be that bad.. Oh and another question.. if I set d.r to 5 then destroy the struct. Then create another struct of the same structtype(StructData in this case) will the <NewStruct>.r = 5 ? JASS:struct structdata real r = 1 endstruct function func2 takes nothing returns nothing local structdata d call TriggerSleepAction(10.) set d = structdata.create() //AFAIK, it just gets a pointer to the parallel array call BJDebugMsg(R2S(d.r)) endfunction function func takes nothing returns nothing local structdata d = structdata.create() set d.r = 5 call ExecuteFunc("func2") call structdata.destroy() endfunction In other words.. will func2 display '5' ? |
| 09-08-2007, 01:43 PM | #8 | ||
Quote:
No, because of this: JASS:struct structdata real r = 1 endstruct Quote:
|
| 10-01-2007, 09:22 PM | #9 |
Speaking of structs, when you destroy a struct (which is an integer), what happens to the actual integer value of the struct. |
| 10-01-2007, 09:29 PM | #10 |
nothing? Imagine every struct got an stack and an integer variable N=0 when you want to allocate() we first verify, is the stack empty? If the stack is empty, N is increased and the return for allocate() is N. If the stack is not empty, then we return the stack's top and also remove the top element from the stack. And .destroy, simply adds the integer id of the struct to the stack, so allocate() can return it once again. |
| 10-01-2007, 10:07 PM | #11 |
If I might add a bit to this discussion. Structs are global array indexes. Struct Id's have values in range from 0..8190 (0 means null for structs) So basically struct (or struct id to be precise) is a subset of integer. Integer that contains struct id is called this You don't have to write this you can simply use a dot syntax. JASS:struct StructName integer xxx timer tictac endstruct this.x is the same as .x inside a struct Now remember I said this is actually a struct id, and I also said struct id's are indexes in global arrays. Well that is because vJass actually works like this: this.xxx = 7 <--> s__StructName_xxx[this] = 7 call DestroyTimer(tictac) <--> call DestroyTimer(s__StructName_tictac[this]) s__StructName_xxx and s__StructName_tictac are simple global arrays: JASS:globals integer array s__StructName_xxx timer array s__StructName_tictac endglobals As you can see struct with 2 attributes (xxx and tictac) is actually 2 separate global arrays + this |
| 10-01-2007, 10:12 PM | #12 |
I'm pretty sure that 'this' is an argument for any function that uses .stuff. Otherwise, you use a.stuff, where a is an integer which you are defining at the beginning of the function, by saying 'local Struct a'. Each struct knows what it is, because, if you call a method which isn't static, then you have to call it instance.func(), or a.func(). Then, it just passes a through as 'this'. |
| 10-01-2007, 10:19 PM | #13 |
Ah yes I forgot that detail, this: method add takes real a, real b returns real is actually: function add takes integer this, takes real a, real b returns real So when you call a method of a struct: call SomeStruct.add(1, 2) it actually is: call add(SomeStruct, 1, 2) Struct variables are nothing but integers in range 0..8190 that contain the this. and they do all they work by passing this around |
