• 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.

Using 1 struct type in 2 libraries issue

Status
Not open for further replies.
Level 13
Joined
Jan 2, 2016
Messages
978
Okay, I have one struct in "Library2", which requires optional "Library1".
The problem is that I want both of the libraries to be able to use that struct, but I want the struct to be in Library2, since Library1 is only optional.
And since 2 requires 1, 1 gets initialized before 2, and it can't use the struct from 2 :vw_death:
Any way to solve this?
 

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
There would be two separate structs that do exactly the same thing but you would only use one of them depending on if the library exists or not.

Actually screw textmacros, you can just do that manually.
JASS:
library A
	struct structA
	endstruct
endlibrary

library B requires optional A
	struct structB
	endstruct
endlibrary

library test
	private function onInit takes nothing returns nothing
		static if LIBRARY_A then
			call structA.create()
		else
			call structB.create()
		endif
	endfunction
endlibrary
 
Level 7
Joined
Oct 19, 2015
Messages
286
If library1 is also using the struct, then it should require library2, not the other way around.
 
Level 13
Joined
Jan 2, 2016
Messages
978
Library2 is calling functions from Library1, so 1 must be the one required.
Library1 is kind of a add-on library, that extends the functionality of Library2.
(At the moment 1 and 2 are in the same library, but I want to split them as not everyone needs Library1, yet it's making the library longer and harder to maintain)
 
Level 7
Joined
Oct 19, 2015
Messages
286
Well, what is Library1 doing with the struct? If it's a public struct then you can still use it even if it's declared lower in the script, sure calling the struct methods will use .evaluate but if you're not doing it a lot then that's not really an issue.

Alternatively, you could put the code from library1 into module(s) and inject it directly into the struct in library2.
 
Level 13
Joined
Jan 2, 2016
Messages
978
Ah, but I have 1 more question..
How can I make 1-2 functions from Library2 exist only when Library1 is there?

like: if Function1 is in Library1, and Library2 has a function "Function2", which I want to be usable only when Library1 is there, since it calls functions from there.
JASS:
library lib1
    function example1 takes nothing returns nothing
        // do stuff " 1 "
    endfunction
endlibrary
JASS:
library lib2 requires optional lib1
    function example2 takes nothing returns nothing
        // do stuff " 2' "
        call example1()
    endfunction

    function example3 takes nothing returns nothing
        // do stuff " 2 "
    endfunction
endlibrary
 
Level 7
Joined
Oct 19, 2015
Messages
286
If those functions should exist only if library 1 is in the map, then put them in library1.

Alternatively, you can put them inside a static if block.
 
I believe that you can use LIBRARY_lib1 to check if lib1 exists.

JASS:
static if LIBRARY_lib1 then
    // the code within this block will only be written if
    // library "lib1" is declared
endif

Just as a forewarning--if you have to start having cyclic dependencies, you might want to start rethinking how you're organizing your code. Usually those dependencies can be removed just by tweaking the way one library interacts with the other. Or sometimes you might want to think: "does this really belong in a separate library?" Of course, at the end of the day you just want to get your code to work. But for programming in general, it is nice to think about. :)
 
can I put a function inside the static if or do I need to filter out only the functions from lib1?

Try it.

You should check if it compiles with lib1 existing and with lib1 not existing. If it fails to compile when lib1 doesn't exist, then fix those errors and place things in static ifs until "lib1" becomes completely optional.

It is ugly and really hurts code readability, but sometimes it'll make sense. The optimizer removes uncalled functions anyway, iirc, so there isn't much of a point in doing it besides providing options for users.
 
Status
Not open for further replies.
Top