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

[JASS] Loads of Questions!

Status
Not open for further replies.
Level 16
Joined
Oct 12, 2008
Messages
1,570
Hello people,,

I am slowly but certainly learning Jass,,
Now i have come to a point where Jass is (i think) understandable to me now,
And i want to get the fine things of vJass,,
So here are my questions:
Strucs/Methods:
1) Why use a struct over just local variables?
2) I heard of 'Attaching a struct to a unit' once, how?
3) Do methods always have to be placed in a Struct?
4) What is the differenct between:
JASS:
local StructName data = data.Create
//or
local StructName data = data.allocate

Library:
1) What is a library for?
2) Why use a library over normal functions?

Scope:
1) What is a scope for?
2) Why use a scope over normal functions?

and last:
1) What is the big difference between scopes and librarys? What is the best to use for what and why? Are there any nice advantages to one that the other one hasnt got?

Well, i hope you take the time to read it xD

-Yixx,,-
 
Level 3
Joined
Aug 20, 2008
Messages
54
Librarys are used for things such as periodic movement and such. They do not contain an initialize function. Scopes are used for vJASS functions, they do contain initialize functions. They are used like a regular JASS trigger, but scopes are vJASS. Structs are safe, and yes, methods must be in the struct, example:
JASS:
scope Structs

    struct Data
        unit u

    method structdata takes blah returns blah
    endmethod

    endstruct

endscope
As for attaching, Im not the best with JASS yet so Im not 100% sure, so I wont provide false information. As well as the data creation, not 100% sure but Ill give you my opinion, I think .Create makes the data, and .allocate maybe finds the created data? Anyway, hope that helped you.
 
xAbysSx unfortunately gave you a (kind of) wrong answer. I'll start from the top, however:

Structs/Methods:
1) Structs can hold lots of data - it is easier to handle 10 variables with one struct than it is with 10 local variables.
2) If you do SetUnitUserData and give the struct as the integer argument, the struct is attached to the unit. You can retrieve it with GetUnitUserData. There are also custom attachment methods which allow you to attach structs to anything, not just units.
3) xAbysSx got this one right - you can't have methods outside structs, but why would you want to? Methods are just like normal functions, except they can access the private members of their struct and must be called by StructVariable.MethodName.
4) Unless you have declared a custom create method, there is no difference. But if you declare a custom create method, the create method should initialize some stuff, while allocate just assigns the struct an array position.

Library:
1) Any code you put in a library will go into the map header, no matter where it is in the trigger editor. This is useful for organising your systems.
2) Libraries are not functions. They contain functions. You can do something like this:
JASS:
library MyLib
    function MyFunc takes nothing returns nothing
    endfunction
endlibrary
In fact, any Jass you can put anywhere can be put into a library.

Contrary to what xAbysSx said, libraries can also have initialization functions. They are declared like this:
JASS:
library MyLib initializer Init
    function Init takes nothing returns nothing
    endfunction
endlibrary

Scopes:
1) A scope is exactly the same as a library, except the code doesn't go into the map header. It just goes where you put it. Libraries can contain scopes, however.
2) xAbysSx has already answered this question perfectly :D

If you want more information about vJass, look here: JassHelper 0.9.F.0
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
Ok, i think i got it now =)
But some more questions:
1) Can i place like 2 structs in a library?
2) What is the advantage of placing a scope in a library?
3) Can i declare globals anywhere in the script? Or only at the beginning of a library/scope?
If i say this:
JASS:
Library myLib
   globals
      private integer = 0
      private unit
   endglobals
   private function MyFunc takes nothing returns nothing
      //do my actions
   endfunction
endlibrary
4) Will the globals i put there as private only be usable in this library?
5) Will the function i put there as private only be usable in this library?
6) What are stacic globals?
7) Constant globals are always the same and cannot be changed right? (duh, why do i even ask?)
8) I didnt quite get what the difference was between these:
JASS:
 local Structname data = data.Create
//and
local Structname data = data.allocate
As what i saw from some spells/systems:
I think:
JASS:
local Structname data = data.create
//is used for in a function
local Structname data = data.allocate
// is used in a method that stands in that same struct,,
// Tell me if i am right on this!

Well, i might come up with other questions later:grin:

Thanks!
-Yixx,,-
 
1) Yes. You can have as many structs as you like.

2) The scope could then have private members only accessible from within that scope. It just helps to organize your code.

3) You can put globals blocks anywhere, just not inside other globals blocks, functions or structs.

4) Yes.

5) Yes.

6) All globals are "static". Static means independent of any function or struct. Static struct members are just like normal globals, except you have to access them by doing StructName.StaticMember.

7) Yup. They must be initialized in the globals block.

8) Well, an example...

JASS:
struct MyStruct
    integer i
    real r
    unit u
    static method create takes integer i, real r, unit u returns MyStruct
        local MyStruct s = MyStruct.allocate() //allocates an instance of the struct for you to initialize
        set s.i = i
        set s.r = r
        set s.u = u
        return s
    endmethod
endstruct

function MakeStruct takes nothing returns nothing
    local MyStruct s = MyStruct.create(10, 3.5, CreateUnit(Player(0), 'hpea', 0.00, 0.00, 0.00))//Create a struct, passing it values...
    call TriggerSleepAction(5.00)
    call s.destroy()
endfunction
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
Ok, so answer this with only yes or no plz:
JASS:
local Structname data = data.allocate()
is used withing the SAME struct as declared? (so in the method in that struct)

JASS:
local Structname data = data.Create()
is used within a function?

Also, do i NEED to put parameters after '.allocate()' or '.Create()'
 
It's kinda hard to explain. In order to explain it properly I will first explain how structs work internally...

When you declare a struct, each of its members gets turned into an array. For example:
JASS:
struct MyStruct
    integer i
    real r
    unit u
endstruct
Will end up as:
JASS:
globals
    integer array s__MyStruct_i
    real array s__MyStruct_r
    unit array s__MyStruct_u
endglobals

What allocate does, is it assigns an array position for the struct, then returns that array index in the form of an integer. Hence, you can pass structs to functions which take integers.

Create, unless you've made your own create method, does exactly the same thing as allocate. If you have made your own create method, however, as I did as an example in my previous post, you must use allocate to get your struct, before initializing it manually. Create only takes parameters if you tell it to.
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
And that is why in a method (if in the same struct) you use allocate, because it is not intialized yet (havend declared end of struct yet)
and Create() in a function, cause it has initialized already,, right?
Ah well, anways you have been very helpfull so far =D,, i want to thank you for that! ^^
Check your rep box and see how i reward you! >=D

BTW: If i am talking shit now: please dont correct me ^^,, i will find it out when i start using it for the frist time
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
allocate() is a function that looks for an unused index in your arrays (like element said, structs are converted to arrays). create() is a customisable function which you have to make yourself (the default create function simply allocates and that's all it does), allowing you to, in addition to allocating an unused index, initialize your variables, etc.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Since no body mentioned it, scopes give pre-names to all your functions within them.

Let's say you have a trigger page (that white page) called "MyTrigger", you can put a scope called MyTrigger and then do something like this:

JASS:
scope MyTrigger

function bla takes nothing returns nothing
    ...
endfunction

function InitTrig takes nothing returns nothing
    ...
endfunction

endscope

This will rename bla to MyTrigger_bla (and any other function beside the init), and InitTrig to InitTrig_MyTrigger.
 
Status
Not open for further replies.
Top