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

[vJASS] Object, Class and Method

Status
Not open for further replies.
Level 16
Joined
Mar 3, 2006
Messages
1,564
I don't care what goes behind the scene when you enter something like that:
set l = this.x to be converted to this set l = x[this]

JASS:
struct car
    public wheel front_left
    public wheel front_right
    public wheel rear_left
    public wheel rear_right
    private engine VG30DETT
    private integer Number_Of_Bolts
    public direction whichDirection

    method steering takes nothing returns direction
        return this.direction
    endmethod
endstruct
Is car an object ?
wheel is a member of the struct 'car', is it called a class ?

Ofc, there is no variable of type wheel so the integer 'Number_Of_Bolts' is this a class or what ?

If car is an object it surely must have methods and properties, methods affect properties and vice verse. Is this line right to change direction property of the car:

return this.direction

And if I have a line like that:
set thistype(id).inCombatV = false

Is it possible for thistype to take "." ? Also how is it possible to add the brackets "thistype(id)" ???
 
Level 16
Joined
Mar 3, 2006
Messages
1,564
I was poking around a little and I made something like that:

JASS:
struct Size
    real height
    real width
endstruct

struct Label
    integer value
    @Size size@
endstruct

function printValues takes nothing returns nothing
    local label label1
    local integer x
    local real w

    set w = label1.size.height

endfunction
I have another question which perhaps I am asking it due to my misunderstanding to the whole oop concepts, can I make the line marked between the 2 @ s to be the type only without the name and I can create the name from the function ?

something like that:

JASS:
struct Size
    real height
    real width
endstruct

struct Label
    integer value
    @Size@
endstruct

function printValues takes nothing returns nothing
    local Label label1
    local Size size
    local integer x
    local real w

    set w = label1.size.height

endfunction
NOTE
the 2 @ s should make the line red-high-lighted but I don't know what's wrong
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
i dont understand what your question is, the @ dont work and objects dont exist in JASS

just chill back and wait for the next jass tutorials (assuming they'll cover this)

thistype refers to the struct that its in so

struct blah
set thistype.variable = 0
endstruct

wil set blah.varable = 0

this refers to the instance of the struct
struct blah
set this.variable = 0
endstruct

will set this only for that specific instance of the struct when its allocated.

this.x is converted to x[this] because structs arent supported in jass so they make every variable an array than allow certain indices for every instance of every struct using that variable.

to your top question,

car is a struct. wheel is a struct. (your thinking in JAVA...)

an object would be a motor or something you can physically touch

NumberOfBolts is an integer within a struct. it would be referenced by car.NumberOfBolts (which is the improper camelcase)

referring to your brackets question,

JASS:
 static operator [] takes (whatever) returns thistype
    return (whatever)
endoperator

if your struct has this in its insides, than you can do

struct[].hi
or
thistype[].hi
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
no, each variable must have its name, thats the magical door to the RAM memory you are accessing, what would happen If I had 2 sizes in Label without name? if I called xxx.Size.something how would system know which size to use?

this should look like this:

JASS:
struct Size
    real height
    real width
endstruct

struct Label
    integer value
    Size size
endstruct

function printValues takes nothing returns nothing
    local Label label1 = Label.allocate()
    local integer x
    local real w

    set w = label1.size.height

endfunction

remember, every variable must have Its name!
also without allocating an instance of Lable1 you would not have instance of it(0 instance, invalid in most cases) and unexpected things may be set to w(first use would be 0 for sure)

Also the Size size in printValues is its own variable, if you want to access its members, you should use size.height and size.width without the label1. ...
thats because label1. xxx will refer to xxx in the label1 object but the Size size in printValue is its own variable, object

Hope this will help you a little bit
 
Level 16
Joined
Mar 3, 2006
Messages
1,564
@ Arhowk

i dont understand what your question is, the @ dont work and objects dont exist in JASS
That's because I understand the whole OOP vaguely as I never learned JAVA or any programing language except for Qbasic (yes Qbasic and don't get surprised) and MATLAB.

So objects don't exist in vJASS, but about classes, is structs considered a class because I can remember that in the JassHelperManual it is said that:
"Methods are just like functions, the difference is that they are associated with the class" in vJASS case class would be struct I guess.


this.x is converted to x[this] because structs arent supported in jass so they make every variable an array than allow certain indices for every instance of every struct using that variable.
Yes I know that structs aren't supported in JASS but the whole idea of making the vJASS is to introduce you to an environment similar to the OOP one.

to your top question,

car is a struct. wheel is a struct. (your thinking in JAVA...)

an object would be a motor or something you can physically touch

NumberOfBolts is an integer within a struct. it would be referenced by car.NumberOfBolts (which is the improper camelcase)

referring to your brackets question,


JASS:
static operator [] takes (whatever) returns thistype     return (whatever) endoperator
if your struct has this in its insides, than you can do

struct[].hi
or
thistype[].hi
Well, problem is that I can't understand operator at all that I some times think it is useless, the only way that I could understand it is an explanation suitable for a KG student. :grin:
 
Last edited:
Level 23
Joined
Apr 16, 2012
Messages
4,041
ow yes, thistype(id).whatever is typecasting, it means that you are trying to access whatever in a struct with this set to id, for instance:

JASS:
struct abc
    integer a
endstruct

function aa takes nothing returns nothing
    local abc A = .allocate()
    local abc B = .allocate()
    set B.a = 4
    call B.destroy()
    call BJDebugMsg(I2S(abc(2).a))
endfunction

this will print 4 because when you allocate instance A the abc A is actually integer which is set to 1(first instance), then B is set to 2(second instance) and then you set a[2] to 4, then you deallocate it(useless in this case but to show it works even after deallocating) you then with doing abc(2).a says to JH that you want to acces integer a in struct abc on index 2, which will be wrote as a[2] which is 4, its the same as if you did again B.a(deallocating doesnt mean the B is nulled, it only puts the number B was holding back to stack so it can be poped back later at other instance)

Arhowk, if there are no objects in vJass, there are no objects in C++ as well
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
Arhowk, if there are no objects in vJass, there are no objects in C++ as well

give me one object in vJass. you cant create them. you can create them in vJass though

Object (no vJass equivalent) : Jaguar, Spike, Motor, Controller
Class (struct): Jaguar, Relay, Joystick

some examples used in robots.

you cant use vJass to recieve input fro.............

actually, i guess you could technically qualify the display monitor, keyboard, and whatnot as objects but you cant reference them only blizzard-given natives that reference them and you cannot create your own
 
Level 16
Joined
Mar 3, 2006
Messages
1,564
give me one object in vJass. you cant create them. you can create them in vJass though

Object (no vJass equivalent) : Jaguar, Spike, Motor, Controller
Class (struct): Jaguar, Relay, Joystick

some examples used in robots.

you cant use vJass to recieve input fro.............

actually, i guess you could technically qualify the display monitor, keyboard, and whatnot as objects but you cant reference them only blizzard-given natives that reference them and you cannot create your own

I am a bit confused about something you said, objects are things you can touch, if that is the case then how an object exist in language like java ?
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
I am a bit confused about something you said, objects are things you can touch, if that is the case then how an object exist in language like java ?

i told you

examples : Jaguar, a type of motor that spins a wheel or soomething else that can go forwards or backwards
Spike : a type of motor that inputs/outputs ether 1, 0, or -1 (its actually measured in voltages, forget the proper voltage values)

You can use JAVA to control these and set their values. Thus, theyre called objects because JAVA can interact with them and they are touchable
 
Level 16
Joined
Aug 7, 2009
Messages
1,406
I hope I understood your question correctly, and if so, I hope my post will clarify things; just ignore it otherwise.

---

Imagine set thistype(id).inCombatV = false as if it was the following code:

JASS:
local thistype this=id
set this.inCombatV=false

These two scripts do the very same thing: take the instance with the index of "id" and set the value of its "inCombatV" member to false.

And yes, the next two lines are also valid:
JASS:
call thistype(id).steering()
local direction var=thistype(id).steering()
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
So basically, natives and functions are a way to deal with my computer objects (monitor, keyboard and mouse) but the native function itself is not considered as an object just a method to deal with an object, correct me if I am wrong.

yes. the native function would be the equivalent of an inherit class in java
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
- inherit class ? is it different from normal class or they are one and the same ?


- handles, integers, reals, etc ... what are these wrt native functions ? are they something like classes of another class (i.e. sub-class) ?

im not in the mood for giving a java lession. natives are not functions. those are natives. native functions are specific to jass because thats what blizzard gaves us
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
inheritance is the same as if you hade struct extending other structs
real(float in WC3), integer(int), boolean are all native types which comes with all languages
strings are strings but that is class itself in C++, code is just a function pointer which is also native
However handle is a huge class which has many objects(around 100) inheriting it, every Jass variable which is subtype of handle is in fact inherited in terms of languages like C++, thats why you can put unit into function which takes widget, agent or even handle
in fact when you call function like KillUnit it is most likely translated to some class method call(unit.kill() maybe, maybe Im wrong tho but its one of possibilities)
 
Level 16
Joined
Mar 3, 2006
Messages
1,564
im not in the mood for giving a java lession.
No harm in asking, if you are not in mood, that's fine. No need to answer. (no offense meant)

Anyways,

natives are not functions. those are natives. native functions are specific to jass because thats what blizzard gaves us
So there are differences between native and native function.

inheritance is the same as if you hade struct extending other structs

"inheritance" would be something like a class having the same properties of another class ?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
thats exactly what it is
when class inherits from other class it automatically copies all its public and protected functions and variables and I think it works the same way in vJass but nowadaynoone uses it because of the amount of TriggerEvaulate-ons it creates
its like a base class is parent and the inherited class is a child, the child gets some of the parents (shit, forgot the word) by genetics but some doesnt
 
Level 16
Joined
Mar 3, 2006
Messages
1,564
  • ESC
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Custom script: call labelTest()
JASS:
struct Size
    real height
    real width
endstruct

struct Position
    real x
    real y

    method reposition takes real newX , real newY returns nothing
        set .x = newX
        set .y = newY
    endmethod

endstruct

struct Label
    integer value
    Size size
    Position position
endstruct

struct TextBox
    string value
    Size size
    Position position
endstruct

function labelTest takes nothing returns nothing
    local Label label1 = Label.create()
    local integer v = label1.value
    local real w = label1.size.width
    local real h = label1.size.height
    local real x = label1.position.x
    local real y = label1.position.y

    call BJDebugMsg( R2S(x) + "," + R2S(y) )
    call label1.position.reposition(2,5)
    set x = label1.position.x
    set y = label1.position.y
    call BJDebugMsg( R2S(x) + "," + R2S(y) )
endfunction

function takeDataFromTextBoxIntoLabel takes nothing returns nothing
    local TextBox tb1 = TextBox.create()
    local Label label1 = Label.create()
    set label1.value = S2I(tb1.value)
endfunction
Why every time I press ESC I get the message:

-
2.000,5.000
2.000,5.000
Except for the first time which was:

0.000,0.000
2.000,5.000
Shouldn't Position struct (class) a member of the struct Label so the instance of Label (label1) must be the same as Position ?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
you get 0.000 0.000 because the position intially has no values in the index(in this case it will be 0 because you are not creating it)
also you should call label1.destroy() at the end and in the label1 have a .destroy() method which calls deallocates and nulls all the values or they will not be overwritten

thats around what happens, you all the time try to get to 0th instance of position, first time the x and y(position.x and position.y) are 0 because you didnt initialize them, all next times they are 2 and 5 because you set them to the values with calling label1.position.reposition(2, 5)

I didnt understand the question tho, can you rewrite it please?
 
Level 16
Joined
Mar 3, 2006
Messages
1,564
I didnt understand the question tho, can you rewrite it please?

Ok, what I wanted to say is when I created an new instance for the struct Label (forget everything about class) I want to use this instance for Position, in other words, I want to change the position of label1 only and when I create an new Label the position should be the initial position (0,0). I hope that should clarify things ?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
you then need to declare your own create method in the Label struct, which will create a new instance of position

If you want to use a Position position with the same values for every instance of Label, make it static and give it intial value at init

So, if you want every Label to have its own Position, do something like this:

JASS:
struct Position
    real x
    real y

    method reposition takes real newX , real newY returns nothing
        set .x = newX
        set .y = newY
    endmethod

endstruct

struct Label
    integer value
    Size size
    Position position
    
    static method create takes nothing returns thistype
        local thistype this = .allocate()
        set this.position = Position.create()
        return this
    endmethod
endstruct

Basically, when you call Label.create(), you will create new isntance for it(thistype this = .allocate() ) as well as for the position it holds

However, if you only want one position for all Labels, you can do this:

JASS:
struct Position
    real x
    real y

    method reposition takes real newX , real newY returns nothing
        set .x = newX
        set .y = newY
    endmethod

endstruct

struct Label
    integer value
    Size size
    static Position position
    
    static method onInit takes nothing returns nothing
        set position = Position.create()
    endmethod
endstruct

the static means it will be the same value for every instance(it will no longer be array) so it holds one value for every instance of Label

ps, I have excluded Size struct from this code
by default, the position will be 0, 0 but also what you could do is create a destroy method which sets the position back to 0, 0 when it is deallocated.
Example:

JASS:
struct Position
    real x
    real y

    method reposition takes real newX , real newY returns nothing
        set .x = newX
        set .y = newY
    endmethod
    
    method destroy takes nothing returns nothing
        set .x = 0
        set .y = 0
        call this.deallocate()
    endmethod

endstruct

struct Label
    integer value
    Size size
    Position position
    
    static method create takes nothing returns thistype
        local thistype this = .allocate()
        call position.create()
        return this
    endmethod
    
    method destroy takes nothing returns nothing
        call position.destroy()
        call this.deallocate()
    endmethod
endstruct

so now when you create a new Label, its position will have intial values of 0, 0(default in arrays), and when you destroy the instance it will "change the position" back to 0, 0(it will destroy it with nulling the values)

so you can do things like:
JASS:
function test takes nothing returns nothing
    local Label l = Label.create()
    local real x = l.position.x
    local real y = l.position.y
    call BJDebugMsg(R2S(x) + ", " + R2S(y))
    call l.position.reposition(4, 6)
    set x = l.position.x
    set y = l.position.y
    call BJDebugMsg(R2S(x) + ", " + R2S(y))
    call l.destroy()
endfunction

because it is local, you should destroy the instance of Label(Wc3 will not do it for you) because you no longer need it or use it, this way you should get 0.000, 0.000 4.000, 6.000 every time when you hit ESC now
 
Status
Not open for further replies.
Top