Struct array members all have the same value for each instance

dudeim

New Member
Reaction score
22
Hey,

ok so I'm having a problem with array struct members.

JASS:
//creating a random struct with struct array member
struct test

real array value[25] //so this is a simple real array with 25 indexes (or instance dunno precisly but doesn't matter)

endstruct
//ok now i'm gonna use another function to set values to the struct

function init takes....
local test a = test.create()
local test b = test.create()
//now i'm gonna set the values
set a.value[1] = 1
set b.value[1] = 2

call BJDebugMsg(R2S(a.value[1])) //displays 2 while it has to be 1
call BJDebugMsg(R2S(b.value[2])) //displays 2
endfunction


So am I doing something wrong or is it impossible to have array struct members for each instance or something? as that would be rather stupid.

Thanks
 

Sevion

The DIY Ninja
Reaction score
413
JASS:
//creating a random struct with struct array member
struct test

real array value[25] //so this is a simple real array with 25 indexes (or instance dunno precisly but doesn't matter)

endstruct
//ok now i'm gonna use another function to set values to the struct

scope sa initializer init
function init takes nothing returns nothing
local test a = test.create()
local test b = test.create()
//now i'm gonna set the values
set a.value[1] = 1
set b.value[2] = 2

call BJDebugMsg(R2S(a.value[1])) //displays 2 while it has to be 1
call BJDebugMsg(R2S(b.value[2])) //displays 2
set a.value[1] = 2
set b.value[2] = 3
call BJDebugMsg(R2S(a.value[1])) //displays 2 while it has to be 1
call BJDebugMsg(R2S(b.value[2])) //displays 2
set a.value[1] = 4
set b.value[2] = 5
call BJDebugMsg(R2S(a.value[1])) //displays 2 while it has to be 1
call BJDebugMsg(R2S(b.value[2])) //displays 2
set a.value[1] = 6
set b.value[2] = 7
call BJDebugMsg(R2S(a.value[1])) //displays 2 while it has to be 1
call BJDebugMsg(R2S(b.value[2])) //displays 2
endfunction
endscope


Works fine for me.
 

dudeim

New Member
Reaction score
22
Hmmm ok that does seem to work for me too, but why is this code not working then (my real problem code:p)

JASS:
scope ItemInit initializer init

private function init takes nothing returns nothing
local Item i = Item.create('I000') //custom create method to assign an index to an item type
local Item i2 = Item.create('I001') //Each item has their own struct instance so I can set data to each ItemType
set i.data[1] = 123
set i2.data[1] = 456
    
    call BJDebugMsg("value from i [" + I2S(i.data[1]) + ":::" + I2S(i)) //this displays "value from i [456:::1" should be "value from i[123:::1"
    call BJDebugMsg("Value from i2 [" + I2S(i2.data[1])+ ":::" + I2S(i2))//this displays "value from i2 [456:::2" this one is correct
    //so the instances of the struct are diffrent but why are the i.data[1] and i2.data[1] both 456
endfunction
//the create method is probably not the problem but if you want to see it just ask
endscope


So why is this not working then?
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
can we see the code where the struct is defined?
 

dudeim

New Member
Reaction score
22
Sure
It might be a bit messy
JASS:
library ItemData requires TimerUtils
    globals
    private integer index = 1
    hashtable Hash = InitHashtable()
    endglobals
    
    function SetItemData takes integer i returns integer
    if HaveSavedInteger(Hash, 0, i) == false then
        call SaveInteger(Hash, 0, i, index)
        set index = index + 1
    endif
	return index-1 //to return the index of this item id
endfunction

function GetItemData takes integer i returns integer
	return LoadInteger(Hash, 0, i)
endfunction

function HasItemData takes integer i returns boolean
    return HaveSavedInteger(Hash, 0, i)
endfunction    

    
    struct Item
        integer ThisItemId
        real array stats[26]
        real array leveladd[26]
        unit owner
        integer array data[5]
        
    
        implement ItemStats //aditional methods so this whole trigger stays small
        implement ItemMisc
    
    static method create takes integer itemid returns Item
        local Item i
        if HasItemData(itemid) == false then
            set i = Item(SetItemData(itemid))
            set i.ThisItemId = itemid
        else
            set i = Item(GetItemData(itemid))
        endif
        return i
    endmethod
    
    endstruct
endlibrary
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
i don't suppose you're running out of struct indexes to allocate (lots of items being allocated I guess)? declaring an array[26] as a member limits the number of instances you can have to about 307

but I'm guessing this is test code for this system and that's not the case


edit: it's probably your create method, you don't even have a call to allocate()

try something like

JASS:
static method create takes integer itemid returns Item
    local Item i
    if HasItemData(itemid) then
        return HaveSavedInteger(Hash, 0, itemid)
    endif
    set i = Item.allocate()
    i.ThisItemId = itemid
    SaveInteger(Hash, 0, itemid, i)
    return i
endmethod
 

dudeim

New Member
Reaction score
22
Thanks this worked didn't know .allocate was needed thought it was an extra simplere method to Structname(index)
And I probably won't have so many items registered but know I know the limit:p thanks;)
Well the weirdest thing whas that normal members (non-array) did have different values for the indexes
Rep+
 

Tyrulan

Ultra Cool Member
Reaction score
37
I take it you're used to languages like Java or C# which Allocate for you.
 

Solmyr

Ultra Cool Member
Reaction score
30
To make it clear, there's nothing wrong with your create method, even though you don't use [ljass].allocate()[/ljass]. However, inside the struct's allocator (that is generated by JassHelper and called by [ljass].allocate()[/ljass]), there is some stuff which makes sure that the array members of each different struct instance get different indices. You, on the other hand, don't have it (and doing it manually would be a pain in the ass), so you're basically doing this:

[ljass]set data[1] = 123[/ljass]
[ljass]set data[1] = 456 /* This overrides the previous value. */[/ljass]

Whereas, when you use [ljass].allocate[/ljass], the aforementioned turns into:

[ljass]set data[1] = 123[/ljass]
[ljass]set data[6] = 456[/ljass]

Now, as I believe that using [ljass].allocate()[/ljass] is completely senseless in this case, I would suggest you use dynamic arrays. It is quite easy:
JASS:
type itemStats extends real array [26]
type itemLevelAdd extends real array [26]
type itemData extends integer array [5]

struct Item
    integer ThisItemId
    itemStats stats
    itemLevelAdd leveladd
    unit owner
    itemData data

    method onDestroy takes nothing returns nothing
        call this.stats.destroy()
        call this.leveladd.destroy()
        call this.data.destroy()
    endmethod

    static method create takes integer itemid returns Item
        local Item i
        if (not HasItemData(itemid)) then
            set i = Item(SetItemData(itemid))
            set i.ThisItemId = itemid
        else
            set i = Item(GetItemData(itemid))
        endif
        set i.stats = itemStats.create()
        set i.leveladd = itemLevelAdd.create()
        set i.data = itemData.create()
        return i
    endmethod
endstruct


I take it you're used to languages like Java or C# which Allocate for you.
What you said here is completely irrelevant.
 

dudeim

New Member
Reaction score
22
Na what I'm using now works for me also I heard that you can't perfectly detect when an item is destroyed/created.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top