HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Question on Jass and vJass

11-27-2009, 02:08 PM#1
leric
I've read quite a couple of tutorials out there from different sites and forums. But I do have some questions which hope someone can clarify it.

Regarding vJass
==============

Most vJass question arouse from this tutorial and where I have problem getting my activation code from the forum:

vJass OOP Lesson

1. Limit of instance is 8190? Does it mean we can have 8190 of instances of each struct A, B and so on OR A+B+.... total up is limited to only 8190?

2. So vJass has a limitation where inheritance and interface share the same syntax "extend"?
As you can see, in java extends is use for inheritance while implements is use for interface and therefore they both can exist together such as :

Collapse JASS:
 struct holysword extends sword implements attack //where attack is the interface name.

3. I don't remember seeing interface to be a child of class/struct but as(or consider) the parent of a class in common programming.

Quite weird that an interface will be able to call attributes from a class which it means that the interface is a child to that class since a parent class cannot call its child's attribute.
Is this a special feature of vJass or just that I do not know we can do this in common programming language?

** Question 3 is rooted from "Interface Type" part of the tutorial.

4. Is it possible for interface to inherit interface?

5. Can a struct have multiple interface? For example:

struct Hero extends Fire, Ice

or in java

class Hero implements Fire, Ice

6. In Inheritance for vJass, I see something like this in constructor:
Collapse JASS:
local Sword new = Sword.allocate() //<---------- is this a must? 
return new // <----- return inside constructor? 
another vJass way? can I use normal language way like the example below.

Collapse JASS:
struct Sword
    private real damage

    method create takes real damage returns nothing

        set this.damage = damage

    endmethod
endstruct

7. I never need to define the constructor of parent type in child for vJass? such as:

Collapse JASS:
//within the constructor of child
super(a,b,c) //<--------this is what I am talking about
set hand = 4
set leg = 2

8. Function Objects? I don't understand this part since the author mention no difference with functions or just that vJass threat function as an object.
Can describe more the usage of execute() and evaluate() as well?

9. Maybe an explain of function interface in clearer way? Do the author mean that whenever a function meets a function interface's requirement then it will
automatically be extended without need to insert any syntax?

Example:

Collapse JASS:
 function interface OnHeroDeath takes Hero hero returns nothing

function funct1 takes Hero hero returns nothing
//codes...
endfunction

function funct2 takes Hero hero returns nothing
//codes...
endfunction

In this example, does funct1 and funct2 automatically extends "OnHeroDeath"? If yes, then how do we differentiate which function is being
stored into variables or being passed as parameters?

10. Refer to "Fixing One Issue with Implicit Struct Type Names"

Collapse JASS:
library A // <----------- vJass has libraries that we can import?
private keyword i //<-------- outside a struct/class. 

is "i" global variable? Different struct can share it? Due to those programming I did has a seperated
file for each class so I do not know whether this can be done or never seen before which is declare outside the class code. Another question is what is "keyword"? A data type?

11. For most problem in appendix, the cause is .* , so if we always use this.* or structName.* the problems will never occur right?[/quote]

Jass Question
=============

1. JASS do not allow to define variables after an action is done even if that variable is not being used? For Example:

sometimes i want to define variable to test on middle some action to test something will be taken off after testing especially for functions that is rather long.
Collapse JASS:
//some statements
local integer num = 10
//more statements

2. Is it possible to use 2D array? If yes, is it the same way to declare like simple array?

3. Is there a switch/select case conditioning in Jass?

4. Is it possible to overload functions? such as we have 2 or more functions with the same name but taking different parameters type/number of parameters just like usual programming language.

5. What are Indexer? What are the usage of it?
11-27-2009, 02:42 PM#2
Timl fe
Seems you know about programming, so these should be perfect knowledge source for you:
JASS manual
vJASS manual

Answers for JASS part:
1. You can only declare local variables at the beginning of the function - before all statements.
2. No, JASS doesn't allow 2d arrays (but vJASS does).
3. There's no switch/select in JASS (only if statements).
4. No, you can't overload functions in JASS.
5. Indexer? Idk what's that :P

I don't answer vJASS part, as I'm not specialist in it.
11-27-2009, 02:57 PM#3
Anitarf
Quote:
Originally Posted by leric
1. Limit of instance is 8190? Does it mean we can have 8190 of instances of each struct A, B and so on OR A+B+.... total up is limited to only 8190?
The limit is on a per-struct basis, so you could have 8000 instances of struct A and 8000 instances of struct B. The only exception is when the two structs extend the same parent struct/interface: in that case, they share the limit. It is possible to extend the limit for an individual struct (just like it is possible to declare extended arrays) but note that that slows down most struct operations and bloats your code.

Quote:
2. So vJass has a limitation where inheritance and interface share the same syntax "extend"?
In vJass, a struct may only extend a single struct or interface. If you want to inherit from multiple sources, you need to look into delegate and module syntax.

Quote:
3. I don't remember seeing interface to be a child of class/struct but as(or consider) the parent of a class in common programming.
Note than an interface doesn't work like a typical parent struct, but instead it is a template that all children structs are required to copy; therefore, you can call an interface method on any child struct because you know that the struct must have it declared (if it doesn't, then JassHelper generates it with the default return value if it is defined in the interface, otherwise you get a compile error).

Quote:
4. Is it possible for interface to inherit interface?

5. Can a struct have multiple interface? For example:
No. The only way to achieve something resembling multiple inheritance is with delegate and module.

Quote:
6. In Inheritance for vJass, I see something like this in constructor:
Collapse JASS:
local Sword new = Sword.allocate() //<---------- is this a must? 
return new // <----- return inside constructor? 
another vJass way? can I use normal language way like the example below.
No, vJass constructors are required to return thistype, so they must be written like this (although in rare cases you might use something other than allocate to get an index, but normally you don't).

Quote:
7. I never need to define the constructor of parent type in child for vJass? such as:

Collapse JASS:
//within the constructor of child
super(a,b,c) //<--------this is what I am talking about
set hand = 4
set leg = 2
Not sure what you mean here. When writing a constructor for a child type, the call to the allocate method must use the parent's constructor arguments.

Quote:
8. Function Objects? I don't understand this part since the author mention no difference with functions or just that vJass threat function as an object.
Can describe more the usage of execute() and evaluate() as well?
Function objects are distinct from native code variables since they are a vJass construct: when compiled to JASS, they are just integers that represent indexes in a trigger array, the triggers in that array then use these functions as their conditions/actions so the function can then be run by evaluating or executing the trigger. evaluate and execute are slower than regular function calls, however they allow you to run functions that are declared later in the script, execute also allows you to start those functions in a new thread and allows those functions to have waits (but is slower than evaluate).

Quote:
9. Maybe an explain of function interface in clearer way? Do the author mean that whenever a function meets a function interface's requirement then it will
automatically be extended without need to insert any syntax?
...
In this example, does funct1 and funct2 automatically extends "OnHeroDeath"? If yes, then how do we differentiate which function is being
stored into variables or being passed as parameters?
The functions extend the function interface automatically. When you store a function in a function interface variable or pass it to a function as an argument, you need to specify the name of this function.

Quote:
10. Refer to "Fixing One Issue with Implicit Struct Type Names"

Collapse JASS:
library A // <----------- vJass has libraries that we can import?
private keyword i //<-------- outside a struct/class. 

is "i" global variable? Different struct can share it? Due to those programming I did has a seperated
file for each class so I do not know whether this can be done or never seen before which is declare outside the class code. Another question is what is "keyword"? A data type?
i can be anything: a variable, a struct, a method or function name. The "private keyword" only means that when i is declared in that library, it must be private. This is mostly just a workaround due to how JassHelper works: if you use a struct in your library before it is declared, and it is declared as private, then the parser will not know yet that it is private when it will first see you use it, so it won't add the private prefix to it; then, later, when the struct is declared as private, it will start adding the prefix to later references to that struct. As a result, you will later get compile errors. The keyword allows you to tell the parser that an identifier will be private before you declare that identifier, thus avoiding this issue.

Quote:
11. For most problem in appendix, the cause is .* , so if we always use this.* or structName.* the problems will never occur right?
Right. However, most examples given in that tutorial work just fine with the latest JassHelper, the only exception being using statis methods as code arguments, in that case you need to write function thistype.methodname instead of just function .methodname.



Quote:
1. JASS do not allow to define variables after an action is done even if that variable is not being used? For Example:

sometimes i want to define variable to test on middle some action to test something will be taken off after testing especially for functions that is rather long.
No, all local variables must be declared at the start of the function.

Quote:
2. Is it possible to use 2D array? If yes, is it the same way to declare like simple array?
2D arrays are only supported in vJass.

Quote:
3. Is there a switch/select case conditioning in Jass?
Not sure what you mean by this.

Quote:
4. Is it possible to overload functions? such as we have 2 or more functions with the same name but taking different parameters type/number of parameters just like usual programming language.
No.

Quote:
5. What are Indexer? What are the usage of it?
Not sure what you mean, if you are refering to unit indexing systems such as AutoIndex they allow multiple libraries to make use of unit custom values without conflicts, which would otherwise occur if each library tried to use the unit custom values directly.
11-27-2009, 07:41 PM#4
weaaddar
Quote:
Originally Posted by leric

1. Limit of instance is 8190? Does it mean we can have 8190 of instances of each struct A, B and so on OR A+B+.... total up is limited to only 8190?
Technically, only if care about performance are you limited to 8190. You actually may have less by using an array member. You can always extend your structs to use more in their declarations::
i.e. struct Name [AmountYouNeed].
Quote:
2. So vJass has a limitation where inheritance and interface share the same syntax "extend"?
Not really a limitation. But yes. Inheritance and interfaces are treated the same in VJass/Zinc.

Quote:
3. I don't remember seeing interface to be a child of class/struct but as(or consider) the parent of a class in common programming.
This isn't a question. But yes, Vjass has many limitations. One of them being that interfaces for a long time were the only way to get virtual methods.
Now you can use the stub keyword in a struct which means it's virtual, i.e. overridable in an inheriting class.
Quote:
4. Is it possible for interface to inherit interface?
No. Interfaces must be at the top of the chain.
Quote:
5. Can a struct have multiple interface? For example:
Nope :/ Thankfully, Vjass is so weak for library support its hard to imagine why you'd ever want it. If you do come across a case, you can use delegates as a form of emulation, or modules.

Quote:
6. In Inheritance for vJass, I see something like this in constructor:

The create method is not a constructor. It is a static factory that doesn't know its type. Use it to initialize variables on the object that are normally private or don't. If you don't need to do any initialization, the factory is implicit and without parameters.
Quote:
7. I never need to define the constructor of parent type in child for vJass? such as:
No, if you need the factory, then using thistype.allocate() automatically calls the parent factory.
Quote:
8. Function Objects? I don't understand this part since the author mention no difference with functions or just that vJass threat function as an object.
Can describe more the usage of execute() and evaluate() as well?
Function objects allow you to treat functions as first class objects. You can do something like this::
interface_type myVar = someFunctionInTheMap;
And then you can pass that to another method.

As for invocation, there are 2 ways. myVar.evaluate() is synchronous invocation, meaning the callsite thread waits until the callback finishes. Since it is synchronous you can get its return value. execute() is asynchronous meaning the callsite does not wait, and continues running.

Quote:
9. Maybe an explain of function interface in clearer way? Do the author mean that whenever a function meets a function interface's requirement then it will
automatically be extended without need to insert any syntax?
Yes exactly that. If you have something like a func_int_int interface and you have a function double which takes an integer and returns an integer, then any place you need a func_int_int you can send in double or func_int_int.double (whichever you prefer).

Quote:
Jass Question
=============

1. JASS do not allow to define variables after an action is done even if that variable is not being used?
Yes, like C, all locals must be declared at the top of the function.

Quote:

2. Is it possible to use 2D array? If yes, is it the same way to declare like simple array?
Not in Jass. you can use Vjass though.

Quote:
3. Is there a switch/select case conditioning in Jass?
No. You can use if/elseifs though.

Quote:
4. Is it possible to overload functions? such as we have 2 or more functions with the same name but taking different parameters type/number of parameters just like usual programming language.
No. CJass allows you to define around multiple functions with multiple parameters but I wouldn't use it.

Quote:
5. What are Indexer? What are the usage of it?
I'm not sure why you call this a Jass question. Do you mean []?
11-28-2009, 10:33 AM#5
leric
2 - delegate and module? I suppose i will go through the manual of vJass once I have my time.

3. Since extending an interface is only fulfilling it's contract, it doesn't mean that an interface can call methods on child which is not contracted isn't it?
From the quote, you can see that the author call create() using killable interface where it only contract on die(). How is it related? can I possibly call other method
that is declare inside the struct that extends the interface using the example from the quote?

Quote:
Collapse JASS:
function InterfaceTypeLocal takes player p, real x, real y returns nothing
    local Killable m = Hero.create(p, x, y) // Saving an instance of Hero into a variable of type Killable
endfunction

function InterfaceTypeReturn takes player p, real x, real y returns Movable
    return Hero.create(p, x, y) // a function with return type Killable returning an instance of Hero
endfunction


8. Still don't quite get it until i practice it i guess. But in what circumstances we will use execute and evaluate?

5. Indexer - I read it somewhere from a Jass tutorial but forgotten where it is. Suppose not array index that was being refer to as it was a sub topic itself.
11-28-2009, 10:55 AM#6
Deaod
Quote:
8. Still don't quite get it until i practice it i guess. But in what circumstances we will use execute and evaluate?
There are multiple applications. You can use them to execute a function following a certain "function interface" (refer to the vJass manual linked to earlier for an in-depth explanation).
Since .execute uses the native ExecuteFunction internally, you can start a new (virtual) thread with this, to circumvent a few limits like the op-limit or the inability to use TriggerSleepAction in your current thread.
.evaluate uses the native TriggerEvaluate which also starts a new thread with a new op-limit, but such threads simply ignore any and all TriggerSleepAction calls. And as weaaddar mentioned, you can safely return values from functions using .evaluate.
11-28-2009, 12:03 PM#7
Anitarf
Quote:
Originally Posted by leric
3. Since extending an interface is only fulfilling it's contract, it doesn't mean that an interface can call methods on child which is not contracted isn't it?
From the quote, you can see that the author call create() using killable interface where it only contract on die(). How is it related? can I possibly call other method
that is declare inside the struct that extends the interface using the example from the quote?
Incorrect. The author didn't call create using the Killable interface, he called it directly on the Hero struct. He then stored the created struct to a Killable type variable, which he can do because Hero extends Killable.

Quote:
8. Still don't quite get it until i practice it i guess. But in what circumstances we will use execute and evaluate?

Collapse evaluate:
function interface FuncInterfaceTest takes nothing returns nothing
globals
    private FuncInterfaceTest F = A
endglobals

function A takes nothing returns nothing
    call B() // This line causes a compile error, you cannot call function declared lower in the script.
    call B.evaluate() // this line doesn't cause an error.
endfunction

function B takes nothing returns nothing
    call F() // Compile error, you can't directly call function interfaces.
    call F.evaluate() // No error, .evaluate and .execute were designed for function interfaces.
endfunction

Collapse execute:
function A takes nothing returns nothing
    call TriggerSleepAction(5.0) // Wait 5 seconds.
endfunction

function B takes nothing returns nothing
    local integer i
    set i=i+1 // Using an uninitialized variable crashes the thread. 
endfunction

function C takes nothing returns nothing
    call A() // This delays all future actions by 5 seconds.
    call A.execute() // This starts A in a new thread, so this function can continue running immediately.
    call B.execute() // This starts B in a new thread, so when it crashes it won't also crash this function.
endfunction
11-28-2009, 03:56 PM#8
leric
My mistake to read the code opposite for the interface killable.

Well so evaluate and execute is for the usage of threads. I'll check out the manual try to understand it.

When a thread crashes, will it cause memory leak?
11-28-2009, 06:42 PM#9
Anitarf
No idea. We have no use for deliberately crashing a thread, so noone has looked much into it.
11-30-2009, 07:20 PM#10
leric
Collapse JASS:
function Build1 takes nothing returns nothing

    call AdjustPlayerStateBJ( 1000, Player(0), PLAYER_STATE_RESOURCE_GOLD )
    call SetProduce(1, TREE_LIFE , 0)
    call SetProduce(5, WISP , 0)
    call SetProduce(1, ELF_ALTAR , 0)
    call SetProduce(2, MOON_WELL , 0)
    call SetProduce(1, ANCIENT_WAR , 0)
    call SetProduce(3, ARCHER , 0)
    call TriggerSleepAction( 5.00)
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0,"Building now....")
endfunction

function main takes nothing returns nothing
    
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0,"Hi")
    call StartThread(function Build1)
endfunction

I need to correct multiple errors from this code.

1. The SetProduce() is declared as natives in common.ai but it seems I can't make use of it. Don't files share declarations? I saw files like undead.ai sharing functions with common.ai.

2. Is there already a user made list of native functions which I can reference to as I don't see all of them shown in common.j ?

3. From elf.ai file, I saw something like WISP used as unitid but I don't find such variable at any .ai file or common.j & blizzard.j. Where could it be? Any goof and convenient way to get unitid using their name?

4. Is there any place where I can reference native function's name beside common.j?

Also I can't figure out how to use JNGP properly as it doesn't seems that there is a place for me to write my AI code therefore I used notepad and copy to JNGP to use the syntax checker and it's not that convenient.
11-30-2009, 07:25 PM#11
PurplePoot
Quote:
Originally Posted by leric
1. The SetProduce() is declared as natives in common.ai but it seems I can't make use of it. Don't files share declarations? I saw files like undead.ai sharing functions with common.ai.
Common.ai is not accessible to anything other than AI scripts.

Quote:
Originally Posted by leric
2. Is there already a user made list of native functions which I can reference to as I don't see all of them shown in common.j ?
They are all in common.j.

Quote:
Originally Posted by leric
3. From elf.ai file, I saw something like WISP used as unitid but I don't find such variable at any .ai file or common.j & blizzard.j. Where could it be? Any goof and convenient way to get unitid using their name?
There is presumably a variable somewhere which you missed.

Quote:
Originally Posted by leric
4. Is there any place where I can reference native function's name beside common.j?
Predefined ones? No.
11-30-2009, 09:36 PM#12
Earth-Fury
Quote:
Originally Posted by leric
1. The SetProduce() is declared as natives in common.ai but it seems I can't make use of it. Don't files share declarations? I saw files like undead.ai sharing functions with common.ai.

common.j defines the API for .j files.

common.ai defines the API for .ai files.

If you copy and paste the native declarations from one to the other, it will sometimes work.

Quote:
Originally Posted by leric
2. Is there already a user made list of native functions which I can reference to as I don't see all of them shown in common.j ?

I don't know of a list of functions from common.ai that work in common.j. Usually, the map will explode/fail rather largely if a native from common.ai will not work in common.j.

Quote:
Originally Posted by leric
4. Is there any place where I can reference native function's name beside common.j?

You can add native declarations to your map. JASS Helper will move them to the correct place, and delete duplicate ones. This means you don't have to edit common.j to add new natives to your map. You simply have to include the native declaration to your map script.

Quote:
Originally Posted by leric
Also I can't figure out how to use JNGP properly as it doesn't seems that there is a place for me to write my AI code therefore I used notepad and copy to JNGP to use the syntax checker and it's not that convenient.

What are you trying to actually do? .ai scripts are barely ever used, as 99 times out of 100, it's possible to do all you need and want to in a .j script.
12-01-2009, 02:02 PM#13
leric
I'm trying to create AI for melee map. So the file type for my codes are store in .ai. I will try not to use triggers so that I can test the AI against AMAI which I will call my .ai files on certain conditions.

I've found that SetProduce() return a boolean which I have nothing to accept the return value. No user made list? then I guess it will take me quite much time to figure out every command in warcraft such as build, move, attack-move, stop and so on. I'll will try to declare natives from common.j in .ai files as I will need to use alot natives from common.j.

--Edited----

Oh I also wonder how do I make my peons to build somewhere around the town. I saw the IssueBuildOrderbyID() function which requires me to provide the peon itself and location. But how do I get the specific unit which maybe I ask 3 to mine gold and 1 to chop wood or randomly ask one of the wood harvester to response to build order. Also how can I can get good location to build? Seems like even the common.ai or race.ai did not call IssueBuildOrderbyID() or any build function but the ai knows how to build. Is there some other files which I miss/should refer to where the AI get their work done such as move, build and so on?
12-02-2009, 08:01 PM#14
leric
Any I found what wrong's with my codes already. It is just impossible to call natives from .j in .ai file. Guess working without a compiler which tells you the error takes time to really solve problems.

Now I wonder how can I perform more raw actions without being able to touch natives from common.j