• 🏆 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!

Passing a boolean between triggers

Status
Not open for further replies.
Level 8
Joined
Nov 9, 2008
Messages
502
Hello The People.

I'm pretty new to Jass; have only been using it for a couple of weeks and not even scratched on using structs and libraries and such....I believe these are functions of vJass, right? Which is what I'm using.

I have a couple of functions I made that are particularly usefull and will be needed for more than 1 trigger but how can I pass their return to other triggers without creating a global? I'm guessing this is the purpose of structs/libraries.

So basicly I want to be able to call this function to get the return inside another function, outside of their trigger.

Could someone explain how I would do this?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Well, use globals. As you are passing, globals work perfectly as WC3 is not threaded and so their values will do exactly what you want for instant use as they only have MUI problems if time is involved.

Also a better explination on the problem would be nice, its hard to visualise what you are after.
 
Level 8
Joined
Nov 9, 2008
Messages
502
Well, as you know I'm creating a physics system for this 2D map.

Basicly I've got I nice function that checks fly height of Unit X to the height of each unit in a group and if it is a certain value then returs true. This is good and works with the physics system but I will also need to use it for the attack system.

I know I cannot call a function that isn't written inside the trigger where I'm calling it. I could simply copy the function into the attack system trigger but then it's doubling the code. It would be more efficient if I can call it from anywhere and only have it written once. Why is it that even though the compiler shoves all the code into 1 script it won't allow me to call a function that's declared in another trigger?

If structs and libraries can't do this, what is their purpose?
 
Level 9
Joined
Nov 28, 2008
Messages
704
If you want it written once, use a library. That lets you call the code from anywhere.

JASS:
library ROAR
    function RAWR takes nothing returns nothing
    endfunction
endlibrary

If you're using JNGP, you can now call RAWR anywhere in your map.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Arrays are faster than hashtables. Thus its more efficent to store like 20 variables of data to something for a spell via structs which you then hashtable to an object rather than 20 values which you hashtable to it. But all in all it depends on how they are used.
 
Level 8
Joined
Nov 9, 2008
Messages
502
So structs are like attaching values to handles but without using cache?

There are no tutorials explaining how to use structs and methods.

Also I noticed a few JASS tutorials referring to use of "Katannas system" for using handles. Is this now obsolete since new patch? Or simply replace the altered function?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Katannas system is obsolete as the natives for hashtables do exactly the same.

Structs work like arrays, and from 1 integer can be used to group data for a cerain instance. That integer can be easilly attached to stuff. They basically work at the same speed as arrays when recalling or modifying data which is far faster than hashtabled ever can. Thus like I said you should use 2 the in conjunction.
 
Level 8
Joined
Nov 9, 2008
Messages
502
If someone could give an example of a simple struct that would be helpful...

I was thinking about using libraries, does the fact that it enables you to call a function from anywhere alter the memory usage some way? Like, does it have a negative effect on performance?
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
If someone could give an example of a simple struct that would be helpful...

I was thinking about using libraries, does the fact that it enables you to call a function from anywhere alter the memory usage some way? Like, does it have a negative effect on performance?

A typical knockback struct
JASS:
struct KnockedUnit
    unit u // unit being knocked
    
    real vx // x velocity, already set to 'Speed * Cos(angle * bj_DEGTORAD'
    real vy // y velocity, already set to 'Speed * Sin(angle * bj_DEGTORAD'
    
    real t // time left for the knockback
endstruct

When you create an instance of that, you can refer to it as an integer, instead of a handle and three reals.

As to libraries - like everything in vJass, it just reorganizes your code in a neat way which you don't need to think about. What a library does is simply sticking all the code inside it at the far top of your map's script, just below other libraries which have been declared before (and below globals, I assume).
Thus it has nothing to do with performance, it won't exist after compiling the map.
 
Level 8
Joined
Nov 9, 2008
Messages
502
Ok, that's good to know about libraries.

Taking that example of a struct, presumably you then call the struct to get all that information but what's the format of the returned data? Example of the line where you are retrieving the data?
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Ok, that's good to know about libraries.

Taking that example of a struct, presumably you then call the struct to get all that information but what's the format of the returned data? Example of the line where you are retrieving the data?

You do not call structs, structs are a way to store data in a Object Oriented way (see OOP).
This means that instead of seeing a knockback as a bundle of variables that effect a unit, you actually see it as an object. An object that describes what and how the knockback should do.
That is the concept of structs (see also Classes if you're interested), they're objects that describe things.

If you want to know how to actually use structs in vJass, I suggest you to look at the many vJass tutorials here, or on any other wc3 site. You're also suggested to look at the vJass official manual.
 
Level 8
Joined
Nov 9, 2008
Messages
502
I have heard this OOP but never fully understood it.

A correct anaolgy would be grouping all the factors that make up the movement of wind and calling it "Wind"?

It seems a bit pointless except to re-organise your script. I mean, what I do know is create a trigger to manage physics, a trigger to manage attacking and so on. Creating a trigger to be universal so it works for as many possible situations is logical. What's the point of putting it into a struct.

I just wikid Classes and came up with a nice paragraph:

"The most popular and developed model of OOP is a class-based model, as opposed to an object-based model. In this model, objects are entities that combine state (i.e., data), behavior (i.e., procedures, or methods) and identity (unique existence among all other objects)."

Triggers are composed of variables, and a set of actions to produce a result. I see the 'Identity' would the integer assigned to a struct (?). It looks to me there is little difference betweena struct and a trigger.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
I myself also am not among those who see OOP as some 'god rule of programming' (and I mean in actual applications, not only in wc3), but it does have its uses.
In warcraft it is especially neat with structs since, like I mentioned above, you can refer to a lot of data with one integer, so if you're using a hashtable, for example, you only need to attach that integer.

OOP is mainly there for a neater code. Seeing everything as objects (or 'Entities' as wiki calls them) is fairly logic, seeing as everything around us is an object.

Now to make things a beat more clear - OOP does not mean you take a few variables and bundle them up to an object.
It means you give that object also other properties - methods.
A struct originally (in the 'C' language that is) was just a bundle of variables.
A struct in Jass is equivalent to a Class, which is probably used in every programming language today, in that that it gives you the ability to use functions, that effect a specific object, without actually needing to tell it to in any special way.
An example of a struct without methods in the real world would look like this:
Code:
// a bundle of variables that define a 3d vector
struct vec3
{
    float x,y,z;
};

// add 'a' to 'b' and return the answer
vec3 vec3add(vec3 a,vec3 b);

// subtract 'a' from 'b' and return the answer
vec3 vec3sub(vec3 a,vec3 b);

There is no "this" object, you always have to give two objects.
So a code using that would look like this:
Code:
vec3 a = ...
vec3 b = ...
vec3 c = vec3add(a,b);
vec3 d = vec3sub(a,b);

The OOP equivalent code is a bit nicer (in large programs it becomes a LOT nicer in most cases):
Code:
struct vec3
{
    float x,y,z;
    
    vec3 add(vec3 b);
    vec3 sub(vec3 b);
};

And using it:
Code:
vec3 a = ...
vec3 b = ...
vec3 c = a.add(b);
vec3 d = a.sub(b);

In any case, returning back to Jass - just use structs, in the very least they'll make your code so much readable.

And if you read all this, you're probably thinking "wait, is he supporting OOP or not??", I don't have any definite answer. I use it where I feel fit, but I don't use it everywhere like most (I think) people do.
 
Status
Not open for further replies.
Top