brand new jass beginner +rep for help

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
ok when you use
JASS:
local a = struct.create

it creates a struct instance and you can use the variable a to get that structs intsance number, and you can use that number in any trigger to get that instance correct?

and that instance can store multiple variables, so its essentially like a hashtable in gui
 

Dirac

22710180
Reaction score
147
No, hashtables are the same for gui and for jass.

It's like an array variable on your variable editor.

the number that struct.create returns is the index the array takes

array[index]=value

index.array=value

this.number=value
 

luorax

Invasion in Duskwood
Reaction score
67
Something like this:

JASS:
local <type> <name> = <type>.create(<arguments)
local thistype this=thistype.create()
local Data d=Data.create(u,150.)


But yes, you got the point. Because structs are basically arrays, the struct instance is an integer, that represents the index of the instance. So when you're trying to get a struct member like this:

JASS:
call KillUnit(Data(this).owner)


it's converted to something like this:

JASS:
call KillUnit(s_Data_owner[this])


If you take a look at the popular vJASS systems, you'll notice that everytime you see a "data" field, it's always an integer. Well, this is the reason. You can always create a dummy struct, create a new instance and store everything necessary in it (or later you'll be able to work only with structs), so that you can retrieve it later, when needed. It's actually a very powerful thing IMO.

EDIT:
JASS:
call KillUnit(Data(this).owner)


is equal to:

JASS:
local Data d=this
call KillUnit(d.owner)


It's called typecasting.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
alright this is my example of using a struct to store stats for an rpg, just an example:
JASS:
struct UnitIndex
  unit IndexedUnit
  real CritChance
  real DodgeChance
endstruct
JASS:
//here we determine the crit/dodge chance
function CritChanceArith takes unit u returns real
  return (SquareRoot(I2R(GetHeroAgi( u, true))) + SquareRoot(I2R(GetHeroInt( u, true)))) x 2
endfunction

function DodgeChanceArith takes unit a returns real
  return SquareRoot(I2R(GetHeroAgi( u, true))) x 3
endfunction

//this down here is the function i would call to create the struct
function SaveNewHero takes nothing returns nothing
  local a = UnitIndex.create()
  call SetUnitUserData( GetTriggerUnit, a)
  set GetUnitUserData.IndexedUnit = GetTriggerUnit
  set GetUnitUserData.CritChance = CritChanceArith(GetTriggerUnit)
  set GetUnitUserData.DodgeChance = DodgeChanceArith(GetTriggerUnit)
endfunction
 

Laiev

Hey Listen!!
Reaction score
188
Jass is case sensitive

JASS:
struct UnitIndex
  unit IndexedUnit
  real CritChance
  real DodgeChance
endstruct


and this

JASS:
function SaveNewHero takes nothing returns nothing
  local a = UnitIndex.create
  call SetUnitUserData( GetTriggerUnit, a)
  set GetUnitUserData.IndexedUnit = GetTriggerUnit
  set GetUnitUserData.CritChance = CritChanceArith(GetTriggerUnit)
  set GetUnitUserData.DodgeChance = DodgeChanceArith(GetTriggerUnit)
endfunction

//>

function SaveNewHero takes nothing returns nothing
  local UnitIndex a = UnitIndex.create() //you forget the type
  //call SetUnitUserData( GetTriggerUnit, a) // don't do this, you'll bug every index system
  set a.IndexedUnit = GetTriggerUnit
  set a.CritChance = CritChanceArith(a.IndexedUnit)
  set a.DodgeChance = DodgeChanceArith(a.IndexedUnit)
endfunction
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
why would using the unit's custom value bug the index system? if i was making my own system to index units
 

luorax

Invasion in Duskwood
Reaction score
67
JASS:
struct DamageData
    unit unit
    real critChance
    real dodgeChance
endstruct

function CritChanceArith takes unit a returns real
    return (SquareRoot(I2R(GetHeroAgi(u,true)))+SquareRoot(I2R(GetHeroInt(u,true))))*2
endfunction

function DodgeChanceArith takes unit a returns real
    return SquareRoot(I2R(GetHeroAgi(u,true)))*3
endfunction

function SaveNewHero takes nothing returns nothing
    local DamgeData a=DamgeData.create()
    set a.unit= GetTriggerUnit()
    set a.critChance=CritChanceArith(a.unit)
    set a.dodgeChance=DodgeChanceArith(a.unit)
    call SetUnitUserData(a.unit,a)
endfunction

// or something similar to your example, just to see how typecasting works;
// note that the one above is way faster

function SaveNewHero takes nothing returns nothing
    local DamgeData a=DamgeData.create()
    call SetUnitUserData(GetTriggerUnit(),a)
    set DamgeData(GetUnitUserData(GetTriggerUnit())).unit= GetTriggerUnit()
    set DamgeData(GetUnitUserData(GetTriggerUnit())).critChance=CritChanceArith(GetTriggerUnit())
    set DamgeData(GetUnitUserData(GetTriggerUnit())).dodgeChance=DodgeChanceArith(GetTriggerUnit())
endfunction


However I'd do it like this:

JASS:
struct DamageData
    unit unit
    real critChance
    real dodgeChance
    method getCritChance takes nothing returns real
        return (SquareRoot(I2R(GetHeroAgi(this.unit,true)))+SquareRoot(I2R(GetHeroInt(this.unit,true))))*2
    endmethod
    method getDodgeChance takes nothing returns real
        return SquareRoot(I2R(GetHeroAgi(this.unit,true)))*3
    endmethod
    static method create takes unit u returns thistype
        local thistype this=thistype.allocate()
        set this.unit=u
        set this.critChance=this.getCritChance()
        set this.dodgeChance=this.getDodgeChance()
        call SetUnitUserData(this.unit,this)
        return this
    endmethod
endstruct

function SaveNewHero takes nothing returns nothing
    call DamageData.create(GetTriggerUnit())
endfunction


Now take a deep breath, then try to figure out what and why I did in my example.

EDIT:

why would using the unit's custom value bug the index system? if i was making my own system to index units

Because indexing systems use the unit's user data to attach the unit's index to the unit. Overwriting it would obviously bug the system.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
i understand what you did, you used methods to set the values within the struct rather than using a return from another function then saved them using a static method that way you only save the unit and not all the information

and i was meaning, what if this was my index system, thats how i wanted to treat it, not like i was using someone elses

ill check it out at home ayanami, blocked at work :/

what does this.something stand for? i assume it means that instance but i want to be sure
 

luorax

Invasion in Duskwood
Reaction score
67
"this" represents the actual instance. It's only available in non-static methods, however you can declare it in statics manually.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
and thats declared with the line: local thistype this=thistype.allocate
correct?

how am i doing so far for having started jass almost exactly 7 days ago?
 

tooltiperror

Super Moderator
Reaction score
231
Don't ask how you are doing. Judge yourself, and being "good" at Jass doesn't really matter.

Naming conventions are very important, by way of constants you can find the scope and context of a variable without finding where it is declared.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
i meant more where should i be headed from here, what would the next step be, not am i like super super good... sorry if i stated the question wrong, ill read up on conventions tomorow when i have all day to zonk out at home

how do i add events and conditions to a function? i understand how to call functions with actions, but iv never actually added conditions at this point
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
i see, thats easy enough, thanks lol
what would a condition function look like?
 

Laiev

Hey Listen!!
Reaction score
188
JASS:
function SomeAction takes nothing returns nothing
endfunction
function SomeCondition takes nothing returns boolean
    return false //action will not be executed, if return true, action will be executed
endfunction
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
can a struct have an array in one of the variables encapsulated?

ie:
JASS:
struct example
  real x[10]//<--- to save 10 arrays in a single instance
endstruct
 

Ayanami

칼리
Reaction score
288
can a struct have an array in one of the variables encapsulated?

ie:
JASS:
struct example
  real x[10]//<--- to save 10 arrays in a single instance
endstruct

Using array instance members decreases the total instances that you can have. It's 8190 by default. If I'm not wrong, by declaring a instance member with array size of 10, your max instances become 819.
 

Dirac

22710180
Reaction score
147
As Ayanami says it does that because it "simulates" 2d arrays, but you're really only using 1 array and split it in 10 parts (in that case). I advise you to not do that ever, if you want to store multiple values, learn some about linked lists (i should probably write a tutorial about that too).
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
the idea i had would only store values for heroes, so if a legit map has over 819 heroes at one time, im pretty sure the maker should be shot in the face, not 100% on that one though lol

and is there a way to change the variables stored by a struct and add/remove more to/from it after the game has started
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top