Queations on library/scope and locals.

Zalinian

New Member
Reaction score
0
I'm trying to use jass to make some trigger based spells. I need to use local variables so that they can be multi-instanced. However, a local can only be used in a single function, and my spells span over several functions. Is there a way to make variables local to a set of functions? Is this how library/scopes work? I'm using custom text on the trigger, so I have the addtrigger, and triggerconditions functions and such.
If someone could give a small explanation of how this all fits in and possibly a small or semi-complex example (I learn best from dissecting others' code) I would greatly appreciate it.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
Libraries work by sending all of the contents of it to the map header when saving.
So something like:
JASS:

library myLibrary
     globals
          //globals
     endglobals
     function DoStuff takes nothing returns nothing
          // do stuff
     endfunction
     // more functions
endlibrary


That'll all be in the map header when saved. It helps make it more organized.
Scopes kinda do the same thing, but a function can only be used in the scope it was declared in. So:
JASS:

scope myScope
     function myFunct takes nothing returns nothing
          // do stuff
     endfunction
     function mySecondFunct takes nothing returns nothing
          call myFunct() // works, because myFunct is in the same scope
     endfunction
endscope
function myThirdFunction takes nothing returns nothing
     call myFunct() // does not work, since "myThirdFunction" is not in the same scope as "myFunct"
endfunction


As for "locals in other triggers", I think you'll want a struct. I can't really tell you too much about a struct, becuase I'm still trying to learn about them myself.
 

Zalinian

New Member
Reaction score
0
I mainly just need to know how to make variables able to be referred to in multiple functions, but local in a way to allow multi-instancing.

Edit: I read up a bit on structs, and that may be where I need to head, but I'm not sure.

My custom abilities are very complex and use several different variables of data and multiple functions. I just need to make it multi-instance for when multiple people play the same type of hero.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
I can't really tell you much about structs. I don't know them very well... I can't find any really good documentation on them (other than the JassHelper manual).

But as far as I know, you have to tell the function that it uses the struct, or else it can't access its data, and multiple functions can use a struct, and that's what makes it so good. But then again, that's all I know.

Multi-instance should work with locals though, according to what I know. Have you tested it with locals?
If itllwork with locals, there is no need to complicate things with structs.
 

Flare

Stops copies me!
Reaction score
662
Scopes kinda do the same thing, but a function can only be used in the scope it was declared in.
Noooooo. Private syntax prevents code from being accessed from outside its enclosing structure (be it a struct, scope, or library). The example you have shown works.

However, if you had prefixed function myFunc takes nothing returns nothing with private, then you are unable to call the function from outside the scope


As regards structs, a simple analogy would be that they are like boxes. Sure, you could handle X different objects individually but, if you need to carry them around (i.e. transferring the objects, or your data, between functions), a box does make it much simpler since you are capable of carrying all your objects, at once, without any fuss.

But, that's being very simple about it. A struct itself is an enclosing structure like a scope or library which is capable of containing dynamic variable members (each struct instance will get an instance of each dynamic member) and static variable members (which, in a nutshell, are non-arrayed global variables).

Structs can also contain methods which are the same as functions, other than the manner in which they are called - there's dynamic methods which (naturally) take a struct instance as an argument, and static methods which don't naturally take a struct instance as an argument.

Now, some code
JASS:
struct Test //declaring our struct, and naming the enclosure
  real x //dynamic variable member
  static integer InstanceCount = 0//static variable member
  
  method UpdateX takes real newVal returns nothing //dynamic method
    set this.x = newVal //'this' is the name of the struct instance parameter that dynamic methods take
  endmethod

  static method DisplayTotalInstances takes nothing returns nothing
    call BJDebugMsg ("Total instance number: " + I2S (Test.InstanceCount))
  endmethod
endstruct

function TestFunc takes nothing returns nothing
  local Test data = Test.create ()
//a struct instance variable is declared like any other data type
//the .create () method is another example of a static method
//all structs possess this method, and it can be overridden by the end-user by declaring your own static create method

  call data.UpdateX (5.)
//dynamic methods are called by prefixing the method name with the struct instance's variable name, and a period

  call BJDebugMsg ("x is " + R2S (data.x))
//dynamic variable members are referenced in a similar way

  set Test.InstanceCount = Test.InstanceCount + 1
//static variable members are referenced by prefixing the variable name with the struct's name
//i.e. the name given to the whole struct enclosure, not just the instance and a period

  call Test.DisplayTotalInstances ()
//static methods would be called in a similar way to dynamic methods,
//but you prefix the method name with the struct enclosure's name, like a static variable
endfunction


Hopefully that will be of help to you (be sure to ask if anything is unclear, I may not have elaborated on everything :p)
 

Zalinian

New Member
Reaction score
0
So, I've read and heard about scopes, libraries, and structs. Can anyone give some details on the advantages/disadvantages on the mentioned? The ability I'm working on has some variables for data that are local to each instance triggered, but must be accessible by all of the functions used to make the ability(ie: I need something that's half way between global and local).

Edit: Don't get me wrong, the info both of you have posted was helpful and I'm sure whatever knowledge isn't relevant to what I'm trying to do will be useful to me for future coding.

Now that I'm home, I'll try some tests to see if I can accomplish this task.


Edit: After reworking some of my code into a struct setup, I conducted several tests and the results were perfect. Theres still more to do but your suggestion(both of you) on using structs has worked like a charm so far. Tomorrow I'll work on the rest.
Thank you very much for the input. +rep.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • 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

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top