Struct/vJass Tutorials - Help wanted please

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
> Q2. Libraries, structs and globals all have the same level in the function heirarchy correct? Scratch that.

As far as I know:

globals
libraries
structs

Although I'm sure Cohadar will jump from somewhere is a hammer to fix some mistake.

> Q3. What are the global blocks I keep hearing about?

Where you create global variables, like the variable editor, however they are used just like libraries:

JASS:
globals
    variabletpye array? variablename
    ...
endglobals
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
Ok. So I think I understand globals (you would ONLY create (=declare?) global variables in those global blocks correct? You would not also put other functions in the globals?

I am on the way to understanding libraries (i.e. make all functions inside the library be read first and able to be called from anywhere.)

Structs: Are a way of putting multiple variables into one? I.e. ... something.
What is the ".create" and ".destroy". It's obvious by their name that they create and destroy something...but what is that something?
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
Kingpin

I think he meant "members".

I apologize for my stupid comment before. That was arrogant of me. I understand what you mean now.
Since I made it so public before, I thought I'd better do the same in putting it right. No hard feelings?

I'd like to hear more from you in this thread if you are willing. :)
 

The_Kingpin

Member (Who are you and why should I care?)
Reaction score
41
Structs are sorta hard to explain/understand... but...

Struct is short for Data Structure. Think of it sort of like a package. It has things inside of it. Variables and functions. In a function, you can create a new "package" with the .create method (functions inside structs are called methods.). Here's an example.

JASS:
struct MyStruct // Treat this like a template.
	integer a = 5
	integer b = 5
endstruct

function DoSomething takes nothing returns nothing
	local MyStruct var = MyStruct.create() // Treat a struct name like a type of variable. .create() makes a new instance of a struct based on MyStruct.
	call BJDebugMsg(I2S(var.a)) // Displays "5".
endfunction


You access things inside a struct with <structvar>.<variablename> . Sort of like how you find files in your computer: My Computer/My Documents/Pictures/photo.jpg Sort of like that, but with dots instead of /es.

the .destroy() method destroys an instance of a struct (Important! you can only have 8190 instances of a single struct out at one time!)

Structs aren't just for holding variables, they can have functions inside of them too. They're called methods.

JASS:
struct MyStruct
	integer a = 5
	integer b = 5

	method Add takes nothing returns integer
		return this.a + this.b //use &quot;this&quot; to get variables inside a struct.
	endmethod
endstruct

function DoSomething takes nothing returns nothing
	local MyStruct var = MyStruct.create()
	call BJDebugMsg(I2S(var.Add())) // Displays &quot;10&quot;.
endfunction


Plus one of the great things about structs is that you can use them instead of handle variables. You can store a struct (as integer) holding all the values onto a unit instead of attaching multiple variables to a unit.

There's a lot lot more to structs, I won't get into them just yet.
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
Q7. Structs don't actually do anything by themselves? Q7b.They are a holding pen for your sheep (variable values?)

Q8. You use "this.something" to get variables from inside structs when you are inside? Q8b.You cannot do the same thing from outside a struct? (You would use the struct name instead to get the variables i.e. from your example.) (I think you said exactly that.)

You access things inside a struct with <structvar>.<variablename> . Sort of like how you find files in your computer: My Computer/My Documents/Pictures/photo.jpg Sort of like that, but with dots instead of /es.

Q9.Is "this." a universal thing or is it only for inside structs?

I can sorta see why you would not separate your libraries and structs from the spell trigger. Some spells would not make sense if you did that, even if they do work. You would be scratching your head wondering where some variables and calls came from.

Q10. So, what does before and after preprocessing look like? Taking your example again...
Before
JASS:
struct MyStruct // Treat this like a template.
	integer a = 5
	integer b = 5
endstruct

function DoSomething takes nothing returns nothing
	local MyStruct var = MyStruct.create() // Treat a struct name like a type of variable. .create() makes a new instance of a struct based on MyStruct.
	call BJDebugMsg(I2S(var.a)) // Displays &quot;5&quot;.
endfunction


The preprocessor does something like this?

JASS:
//struct MyStruct // Treat this like a template.
//	integer a = 5
//	integer b = 5
//endstruct
// My guess is this actually gets deleted.

function DoSomething takes nothing returns nothing
	local integer var = 5
	call BJDebugMsg(I2S(var)) // Displays &quot;5&quot;.
endfunction
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
I got it! (i think - sorry for double post - just so excited that i might have got it.)

Structs are arrays! Kinda - They are the vJass equivalent of []?
Please tell me I'm correct...
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Q7. I'm not sure I understand you... Mind rephrasing that?
Q8. Precisely.
Q9. this. works only in structs, where else would it be useful? Note that this. can be replaced with one simply . .
Q10. Nope, by all means it doesn't work like that, although I'm not entierly sure how it DOES work. This is the time for Vexorian to come in and clarify this.

As for saying structs are arrays, basically yes. I mean, no, however you may think of them as array, when instead of [#] you put .string.
 

Cohadar

master of fugue
Reaction score
209
Apprentice:
Master I do not understand this kung-fu technique,
it is complicated, it is hard and it makes no sense to me,
I think it is of no use.

Master Fu:
Do not think but do,
you need not understand to use,
when you use, understanding comes to you.
 

Doom-Angel

Jass User (Just started using NewGen)
Reaction score
167
Apprentice:
Master I do not understand this kung-fu technique,
it is complicated, it is hard and it makes no sense to me,
I think it is of no use.

Master Fu:
Do not think but do,
you need not understand to use,
when you use, understanding comes to you.
lol i was thinking the same thing all this questions make it more confusing than the actual use of it....
 

The_Kingpin

Member (Who are you and why should I care?)
Reaction score
41
Apprentice:
Master I do not understand this kung-fu technique,
it is complicated, it is hard and it makes no sense to me,
I think it is of no use.

Master Fu:
Do not think but do,
you need not understand to use,
when you use, understanding comes to you.

Q F T

Like me, I learn by doing.

Here' I'll post another example of it's usage.

JASS:
struct MyStructA
	string s = &quot;ham&quot;
endstruct

struct MyStructB
	string s = &quot;cheese&quot;
endstruct

function DoSomething takes nothing returns nothing
	local MyStructA a = MyStructA.create() // New instance of MyStructA
	local MyStructA aa = MyStructA.create() // Another instance of MyStructA
	local MyStructB b = MyStructB.create() // New instance of MyStructB
	call BJDebugMsg(a.s+&quot; and &quot;+b.s) // Displays &quot;ham and cheese&quot;
	set a.s = &quot;foo&quot;
	set b.s = &quot;bar&quot;
	call BJDebugMsg(a.s+&quot; &quot;+b.s) // Displays  &quot;foo bar&quot;
	call BJDebugMsg(a.s+&quot; &quot;+b.s+&quot; &quot;+aa.s) // Displays &quot;foo bar ham&quot;
	call a.destroy()
	call b.destroy()
        call aa.destroy()
endfunction


Here's another example using methods.

JASS:
struct Blah
	real a = 61
	real b = 9
	unit f = CreateUnit(&#039;hpea&#039;,Player(0),0,0,0)

	method Add takes nothing returns real
		return this.a + this.b //this = the struct instance containing the method.
	endmethod

	method Kill takes nothing returns nothing
		call KillUnit(this.f)
	endmethod
endstruct

function DoSomething takes nothing returns nothing
	local Blah s = Blah.create()
	call BJDebugMsg(R2S(s.Add())) // Displays 70.
	call s.Kill()
endfunction
 

Doom-Angel

Jass User (Just started using NewGen)
Reaction score
167
JASS:
return this.a + this.b

from where execly did you call for this.a?
i didn't see you declare any "this"
 

Doom-Angel

Jass User (Just started using NewGen)
Reaction score
167
so it doesn't matter whatever the struct name is as long as the method is inside it i could refer it as this.<variable>?
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Yes, this. is just like using the struct's name. Note that you could do .member instead of this.member.
 

The_Kingpin

Member (Who are you and why should I care?)
Reaction score
41
so it doesn't matter whatever the struct name is as long as the method is inside it i could refer it as this.<variable>?

this.<variable> or just .<variable>. However, it cannot be used in static methods or on static members.
 

Doom-Angel

Jass User (Just started using NewGen)
Reaction score
167
it cannot be used in static methods or on static members.
assuming i didn't hear this one i think i understood.... (i don't wanna hijack the thread..)
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
assuming i didn't hear this one i think i understood.... (i don't wanna hijack the thread..)

Go for it. The more understanding we get the better.

Q13442. Statics are private globals inside a struct?

Cohadar said:
Apprentice:
Master I do not understand this kung-fu technique,
it is complicated, it is hard and it makes no sense to me,
I think it is of no use.

Master Fu:
Do not think but do,
you need not understand to use,
when you use, understanding comes to you.

This.29 = brilliant lol. Although it might be a little ahead of my predicament. I .... yes master. I go to do.
 

The_Kingpin

Member (Who are you and why should I care?)
Reaction score
41
Almost, they're just not private.
Statics are members of a struct that are not instanced, hence the name "static".

Here's an example:
JASS:
struct MyStruct
	integer a = 4
	static integer b = 5
endstruct

function Do takes nothing returns nothing
	local MyStruct m = MyStruct.create()
	call BJDebugMsg(I2S(m.a)) // Displays &quot;4&quot;.
	call BJDebugMsg(I2S(m.b)) // Error
	call BJDebugMsg(I2s(MyStruct.b)) // Displays &quot;5&quot;.
	call m.destroy()
endfunction
 
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