• Check out the results of the Techtree Contest #19!
  • 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.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

Operators

Status
Not open for further replies.
I did have the same problem as you when I read through the operator explanation part, I start to understand how it works after several testing in the editor.



Operator - To simplify it, it is a function which would replace specific terms if the terms is the same as operator's term.

Operator Types - There are few operator types you could use, there are :
  1. method operator [] takes <TYPE> t returns thistype
  2. method operator []= takes <TYPE> t, <TYPE2> t2 returns nothing
  3. method operator < takes <TYPE> t returns boolean
  4. method operator == takes <TYPE> t returns boolean
  5. method operator NAME takes nothing returns <TYPE>
  6. method operator NAME= takes <TYPE> t returns nothing

Example :
JASS:
struct A
	method operator [] takes integer i returns thistype
		return this*i
	endmethod
endstruct

function test takes nothing returns nothing
	local A b = A.create()
	call BJDebugMsg(b[2]) //notice this, a syntax of [] is used
endfunction
And so, the operator [] will "replace" the [] syntax, for easier understanding, let's see how the jasshelper will compile it :
JASS:
 function s__A__getindex takes integer this,integer i returns integer
		return this * i
 endfunction

function test takes nothing returns nothing
 local integer b= s__A__allocate()
	call BJDebugMsg(I2S(s__A__getindex(b,2))) //notice this, operator "replaced" the [] syntax
endfunction
So, allocate b will gives you the value of 1, after the calculation, the value of b will ends up with 2.

Example for operator []=
JASS:
struct A
	private string array str[10]
	
	method operator []= takes integer i, string s returns nothing
		set this.str[2] = s
	endmethod
endstruct

function test takes nothing returns nothing
	local A b = A.create()
	//because of the equal syntax, "set" is used instead of call
	set b[2] = "Hello" // []= syntax is used here
endfunction

After jasshelper compile :
JASS:
 function s__A__setindex takes integer this,integer i,string s returns nothing
		set s___A_str[s__A_str[this]+2]=s //this is pretty complicate, could ignore it, this is not the main point
 endfunction

function test takes nothing returns nothing
 local integer b= s__A__allocate()
	call s__A__setindex(b,2, "Hello") // operator "replaced" here
endfunction

Same goes to operator < and operator ==, but they must return boolean


For the last 2 types operator, you can enter any name to them as long as they contains only alphabet characters.
JASS:
struct A
	private string str
	
	method operator NAME takes nothing returns string
		return this.str
	endmethod
	
	method operator NAME= takes string s returns nothing
		set this.str = s
	endmethod
endstruct

function test takes nothing returns nothing
	local A b = A.create()
	set b.NAME = "Hello" //operator NAME= is used
	call BJDebugMsg(b.NAME) //operator NAME is used
endfunction

After compiled :
JASS:
 function s__A__get_NAME takes integer this returns string
		return s__A_str[this]
 endfunction
	
 function s__A__set_NAME takes integer this,string s returns nothing
		set s__A_str[this]=s
 endfunction

function test takes nothing returns nothing
 local integer b= s__A__allocate()
	call s__A__set_NAME(b,"Hello") //operator NAME= is used
	call BJDebugMsg(s__A__get_NAME(b)) //operator NAME is used
endfunction




You're probably asking this: Why don't we just use variables instead of method operators?

Keeping a variable like hp inside the struct and constantly updating it is really stupid.
The method operator allows you to directly return the "GetWidgetLife" native.
Extracted from :Structs for Dummies

In my opinion, I think that operators make thing looks nicer :D Hopefully this could help you to understand how operators work.
 
Status
Not open for further replies.
Back
Top