HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Questions on Structs/Globals

05-07-2007, 02:05 AM#1
Joker
I saw this post that emjlr3 had posted on TheHelper site
Quote:
Originally Posted by emjlr3;458499
it basically allows you to create a variable that holds many different variables in it

for ex.

Collapse JASS:
struct blarg
 real x
 real y
 unit hero
 unit targ
 real ang
 real dist
endstruct

//when you then get the struct, or create it later

local blarg dat = blarg.create()

//you can then store your variable values, or get them, jsut by using their names

blarg.x
blarg.y
blarg.dist

//etc.

therefore, all you need transfer between functions in the integer dat, using game cache

in the end, this should be much faster and safer then using gc for all of it

and it didnt seem that bad. Is this all there is to it to a struct?

Do you need some additional scripts for structs?

Also, what are methods, and private functions?

A question not really related to Structs:
does this create a global variable in the script? if not, then what does it do? Do they have to be nulled?
Collapse JASS:
globals
    local unit a
endglobals
05-07-2007, 02:29 AM#2
Mezzer
You need to use the Jass NewGen Pack which contains vJass, which allows you to use that syntax. Basically, it's a program which will go over your script and change it into something that WC3 can understand.

Here's the link for the pack.

And here's a user's guide for vJass.

And that global block gets appended to the global declaration of the map, so making a variable local in there makes no sense.
05-07-2007, 02:30 AM#3
moyack
For your questions:

Quote:
Do you need some additional scripts for structs?
No, only this tool: http://www.wc3campaigns.net/showthread.php?t=90999

Quote:
Also, what are methods, and private functions?
Methods are functions inside the functions which can be used to manipulate the data in the struct.

Private functions are functions that are only valid inside the library or struct defined. Methods are private functions for the struct, for instance.

About your question about the globals, it will generate error, it should be:
Collapse JASS:
globals
    unit a
endglobals


For more information about structs and more improvements to JASS, read this.
05-07-2007, 03:39 AM#4
MaD[Lion]
I have to fix moyack a bit :)

Methods are nothing more than functions inside struct. They are used to manipulate variables/data inside this struct, which functions cant.
Why they are called method dont ask me. You can call them functions if u want :P
Just that if we talk aboit method, then we know: ok we are talking about functions inside struct.

private functions or private variable are variables that can only be accessed inside a scope.
A struct has it's own scope, also does a library.
You can also use private functions without declaring libraries or structs, bu just declearing the scope.

Private functions is very nice if you only want your system to use a function, but not let anything else run it outside the scope. This way it's more secure.

example i always use:
Collapse JASS:
private method onDestroy takes nothing returns nothing
...
endmethod
Cus i dont want people to clean up anything before the object is destroyed.

If you dont know, onDestroy will be called every time u destroy a struct.
---

And why did u think of declaring locals inside globals, that doesnt make sense.
You dont want a global if you want a local, and you dont want a local if you want a global, not both :P
05-07-2007, 04:41 AM#5
Toink
Private functions cannot be called by functions outside the scope/library it is in.

Scopes are just like libraries, except that they do not get moved to the top of the script.

Opposite to the private functions and/or variables, there are the public members.. Publics are similar to privates except that they are able to be called by a function outsite the scope/library they are in. For example..

Collapse JASS:
scope blah

    globals
        unit u // If you declare globals inside a scope/library, it cannot be used by functions outside the scope/library, which means you can have 10 different globals named u in 10 different scopes/libraries.
    endglobals

    private function MyFunc takes nothing returns nothing
        call KillUnit(u) // Kill the unit 'u' ?
    endfunction

    public function MyPubFunc takes nothing returns nothing
        set u = GetTriggerUnit()
        call MyFunc() // Calls the private function
    endfunction

endscope

function Func takes nothing returns nothing
    call blah_MyPubFunc() // The syntax to call a public is <SCOPENAME>_<Function Name>
endfunction


Hope that also helped you :/

But you should just read the manual included in the JASS NewGen pack.
05-07-2007, 07:23 AM#6
MaD[Lion]
ye that manual explains everything. DOnt try to understand all, jsut try to make one thing work, before going on the new one
05-07-2007, 07:52 PM#7
Joker
Ty guys :) I think im finally making some progress on structs. Also, i meant not to put the "local" in my global. My bad =P so back on the question
Quote:
Originally Posted by me
does this create a global variable in the script? if not, then what does it do? Do they have to be nulled?
05-07-2007, 08:12 PM#8
Anitarf
Toink: what you said about the global variable u would only be correct if you added the "private" prefix to it's declaration.

MaD[Lion]:
Quote:
Methods are nothing more than functions inside struct. They are used to manipulate variables/data inside this struct, which functions cant.
Wrong, the only struct members that cannot be manipulated by outside functions are the ones with the private prefix.

I've seen more errors/odd use of words in the thread, but can't be bothered to read it again. Joker, just read the JassHelper documentation that Mezzer linked to, things are pretty well explained there. As for your question, struct members are nothing more than global variable arrays, that's why you can't have more than 8191 structs of the same type at once, due to the array size limit. As globals, the individual variables in a struct don't need to be nulled, the thing that you have to watch out for is to destroy dynamicaly created structs when you no longer need them, else you risk hitting that size limit.
05-08-2007, 03:58 AM#9
MaD[Lion]
well im not wrong, you can with functions only if you do struct.var, but u cant change it with the var name directly. But ok your right :P
05-08-2007, 04:17 AM#10
Toink
Quote:
Originally Posted by Anitarf
Toink: what you said about the global variable u would only be correct if you added the "private" prefix to it's declaration.

Ooh, right. Sorry about that.

Quote:
Originally Posted by Joker
does this create a global variable in the script? if not, then what does it do? Do they have to be nulled?

Time for a little lesson on variables..

First I'll answer your question. Yes. That creates a global variable in the script. However, if you declare globals inside a scope or library they cannot be used by other functions and you can name them without having a problem, BUT be careful, if you do not add the prefix 'private' it can be used by functions outside the scope/library. For example:

Collapse JASS:

globals
    unit u
endglobals

scope blah
    globals
        private unit u // Notice that we use the name u again. This global can only be used inside the scope it is in, in this case in blah.
    endglobals

    private function PrivFunc takes nothing returns nothing
        call KillUnit(u)
    endfunction

endscope

function Func takes nothing returns nothing
local unit blarg = GetTriggerUnit() //We are declaring a local variable, if you're not sure what locals are, they are exactly like globals [b]except[/b] that they are only used in the function they are in, in this case it is in Func
    call KillUnit(blarg)
    set blarg = null //We are setting variable to null. This is because this variable will leak memory. Why? Because it can only be used in one function remember? 
endfunction //And if the function(Func) executes/triggers again, it declares a different variable named u and [b]does not[/b] overwrite the previous variable. So the variable stays in your computer's memory.


Globals do not need to be nulled. This includes privates. Why? Because globals are overwritten when their values are changed. And globals can also be used by every trigger and function except for private globals.

Collapse JASS:

globals
    unit u
endglobals

function Func takes nothing returns nothing
    set u = GetTriggerUnit() // Sets the value of the global u
    call KillUnit(u) // Kills the unit u
endfunction

Notice that I did not nullify it. Why? When the function triggers again it actually overwrites the value of the global.

Hope that helped you. I spent some time making this stuff just to explain it to you, so show some appreciation by giving us rep if we did help you.

If you got more questions go to the Tutorials section, you'll find answers.