• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[General] Libraries - Accessing Outside Variables & Functions

Status
Not open for further replies.
Level 16
Joined
Mar 27, 2011
Messages
1,349
I'm creating a library that needs to access external functions and variables but I get compile errors. Here are some specific examples.

Accessing External Variables

JASS:
globals
    private timer array musicTimers[udg_MAXNUMBERPLAYERS]
endglobals

This throws a compile error. As a workaround I think I could create another "GetGameData" library and use it as a requirement. This library could have functions to get various pieces of game data and I could call these functions on map init. Is this the right way to go about it?

Accessing External Functions

I need a library to call a function outside of it's scope. Only problem is the function it's trying to use is NOT from a library. It's just normal Jass. Is there any way to use these functions without converting the whole Jass to a VJass library and using it as a requirement?
 
I'm creating a library that needs to access external functions and variables but I get compile errors. Here are some specific examples.

Accessing External Variables

What syntax error are you getting? There's no need to define a size for globals unless they are struct members. You should also be able to use udg variables from your globals blocks.

Accessing External Functions
I need a library to call a function outside of it's scope. Only problem is the function it's trying to use is NOT from a library. It's just normal Jass. Is there any way to use these functions without converting the whole Jass to a VJass library and using it as a requirement?

If you mean that the function is declared below your library then you can use ExecuteFunc or .execute or .evaluate.

JASS:
function A takes nothing returns nothing
    call ExecuteFunc("B")
    call C.execute(15)
    if (D.evaluate()) then
       
    endif
endfunction

function B takes nothing returns nothing
    // below A
endfunction

function C takes takes integer param returns nothing
    // below A
endfunction

function D takes nothing returns boolean
    return true
endfunction
It would be more efficient to wrap the functions in a library through, as the above will generate code.
 
Level 16
Joined
Mar 27, 2011
Messages
1,349
What syntax error are you getting? There's no need to define a size for globals unless they are struct members. You should also be able to use udg variables from your globals blocks.

See below for a screenshot.



wrong-size-definition-png.306180



udg_MaxPlayers is defined as an integer with value of 8 in the variable editor. Not sure why it isn't working, but you're saying I can omit the array size in the global block for musicTimers?

If you mean that the function is declared below your library then you can use
ExecuteFunc
or
.execute
or
.evaluate
.

I need to use functions from InventorySystem which is normal Jass for my PartySystem. Hopefully the screenshot below helps explain what I'm trying to achieve. If I call a InventorySystem function from my PartySystem library, I get a syntax error: "Undeclared function GetUnitBagNumber maybe you meant............"


outside-library-png.306179



Edit: Here is one of the function calls I'm making from PartySystem. I'm calling a function from InventorySystem. The below function call with throw a syntax error "Undeclared function..."

JASS:
local integer currentBag = GetUnitBagNumber(trainer)
 

Attachments

  • Outside Library.png
    Outside Library.png
    58.9 KB · Views: 416
  • Wrong Size Definition.png
    Wrong Size Definition.png
    44.9 KB · Views: 448
Last edited:
Level 39
Joined
Feb 27, 2007
Messages
4,992
You should also be able to use udg variables from your globals blocks.
I was under the impression array size definitions could only accept literal input (like [16]) or integers declared as constant.

@Radicool Based on what I see in the screenshot all you need to do is put a library definition around the entirety of your inventory system and then make your PartySystem require it.
JASS:
library InventorySystem
//Put code here
endlibrary

...

library PartySystem requires InventorySystem

The only time you need to manually define array size is if the array is a member of a struct or if the array is 2-dimensional or if you want an array bigger than the default maximum of 16384 32,768 (used to be 8192).
 
Last edited:
Level 16
Joined
Mar 27, 2011
Messages
1,349
I was under the impression array size definitions could only accept literal input (like [16]) or integers declared as constant.

Based on what I see in the screenshot all you need to do is put a library definition around the entirety of your inventory system and then make your PartySystem require it.

Sure I can do this. I assume I can reference udg_ global variables from libraries in general? My Inventory system uses a few globals, so I don't want a library declaration breaking the system.

The only time you need to manually define array size is if the array is a member of a struct or if the array is 2-dimensional or if you want an array bigger than the default maximum of 16384 (used to be 8192).

Ok good to know. I've remove the size definition for simplicity.
 
Level 39
Joined
Feb 27, 2007
Messages
4,992
As long as they aren't private, any global can be accessed by any other library even without listing the source library as a requirement. All globals are merged into a single block in the war3map.j that comes before every function definition.

udg_ globals are no different, though as Bo shows below they do come last in the globals block so library globals cannot reference udg_ globals.
 
Last edited:
Yeh, size is without impact for usage if you are under array limit. It just always creates 1 string array variable:

JASS:
globals
    constant integer i = 1
    string array text [1]
endglobals
->
upload_2018-9-8_10-4-3.png


also pJass won't bother, and something like this normals work (with array size "1"!):
JASS:
scope Test initializer test
    function test takes nothing returns nothing
        local integer i = 0
        loop
            exitwhen i > 10
            set text[i] = I2S(i)
            set i = i + 1
        endloop
       
        set i = 0
        loop
            exitwhen i > 10
            call BJDebugMsg((text[i]))
            set i = i + 1
        endloop
       
    endfunction
endscope


So like @Pyrogasm said array size becomes useful as vjass feature when wanting higher arrays, then it internally creates multiple string array variables:
JASS:
globals
    string array text [200000]
endglobals
->
upload_2018-9-8_10-2-26.png


..or when needing to split the array internally for a struct.. as a struct is just a normal array, too, which then would need to be devided.


for udg_ usage in libs:

JASS:
library MyLib initializer Init
    globals
        private integer number = 2
    endglobals
+
upload_2018-9-8_10-29-6.png



becomes
->
upload_2018-9-8_10-29-49.png


so when my global got defined, udg_i is not even known yet -> can't use.

also, when the Init function from library or struct runs, then the value for the udg_ is still not assigned, yet:
upload_2018-9-8_10-33-23.png


so the udg_ is useable in the init funcs, but has just the initial value (0/null).

The new array limit is btw 32,768. ^^
 
Status
Not open for further replies.
Top