HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

new class?

01-30-2005, 07:20 AM#1
Tabris
I've read a tutorial of JASS and i have understood about 90% (because I already knew Java,C,C++). But there is a point I dont understand:

What are user-Defined Types? Alias ou class?
And how u declare them???

thanks
01-30-2005, 02:45 PM#2
curi
there aren't any.

someone made a class system though, using gamecache to store the info.
01-30-2005, 03:04 PM#3
Tabris
ok
so what is user defined types?

Quote:
Originally Posted by http://jass.sourceforge.net/doc/
User-Defined Types

The handle type can be subtyped, meaning users can define more specific classes of handles. See Global Declarations for more information. All types used in native functions are declared in common.j.

A type new_type which extends another type parent_type is said to be a child of parent_type. For example, widget is a child of handle and unit is a child of widget. The arrangement of children forms a type-tree which is rooted at the base-type, handle.

We define any type to be an ancestor of itself. In addition, a type a is an ancestor of type b if b is the child of some type c, which a is also the ancestor of (i.e., ancestry is transitive). In other words, a is an ancestor of b if there is a path going down from a to b in the type-tree. For example, if c extends a, d extends c, and b extends d, then a is an ancestor of b, and so are c and d.

cI fund something about class... that is about class with game cache????
http://kattana.users.whitehat.dk/script/?script=244
01-31-2005, 12:33 AM#4
PitzerMike
You can't define user-defined types in JASS.

And yes, what you found is a class system that allows pseudo object oriented progremming using game cache.
01-31-2005, 10:54 AM#5
Tabris
can someone write a tuto on it???
Well.... I need to class to finisch my map...^_^
01-31-2005, 03:49 PM#6
PitzerMike
Buy a good programming book, if you want to know more about OOP. It doesn't have much to do with jass. And no, you don't need classes for any map, you don't even seem to understand what classes are.

In the important threads thread and the JASS vault you can find plenty good resources.
02-01-2005, 08:09 AM#7
AIAndy
A short summary on what my class system can do:

Classes are created by calling a special function, they can be inherited from other classes.
Members can be added to that class via a function call.
Methods can be added the same way.
Class variables are supported.
Constructor and Destructor can be assigned.

Classes can be instantiated by a function call, which initializes certain members and calls the constructor.
Instances can be destroyed, which automatically destroys certain members and calls the destructor if specified.
Methods can be called, a this pointer is available in them, calls are polymorph.
Methods can be called at once, timed or triggered (so they fire at a certain time or when a certain trigger fires).
02-01-2005, 09:46 AM#8
Tabris
Quote:
Originally Posted by PitzerMike
Buy a good programming book, if you want to know more about OOP. It doesn't have much to do with jass. And no, you don't need classes for any map, you don't even seem to understand what classes are.

In the important threads thread and the JASS vault you can find plenty good resources.

I think i do because i am already programming in Java and C++ so... ^_^
And for my map i really need class else it is too dificult ^_^

Well.... I'm creating a map based on Populus, Black-and-White,Seven Kindom
and so I create a unit called "village". But each village has to store his population, the number of spy of each players and the reputation of each player and the map is auto-generated so I don't know the number of village.....
So if you have a best way than using class I would give you rep ^_^ ..... else that not doing village ^_^ and create mass units to store variables....
Create units could be possible but class are better. For exemple, for each village I could create 12 flag for the reputation... But I have to create complex trigger to create such a "class" (in fact)....


AIAndy : Thanks and what is exactly the code i need to add???
02-01-2005, 03:24 PM#9
Sven8136
why not use Village1_Stats[array] to store everything? just gotta remember like..
1 = pop
2 = spys
3 = tax rate
etc
02-01-2005, 08:38 PM#10
Tabris
Quote:
Originally Posted by Sven8136
why not use Village1_Stats[array] to store everything? just gotta remember like..
1 = pop
2 = spys
3 = tax rate
etc

Yeah that could be a good idea(I have already thought that ^^)... but as I already say i don't know the number of village....
Oh yeah!!!!! A matrice, i have read somewhere it is possible in JASS that would work.... (need to find out now ^_^ about matrice )

PS But i reallly want to use class because with class you can make more advanced game.... And well I like learn how to use tool ^_^ evan if I don't use them...
02-02-2005, 12:12 PM#11
AIAndy
First read about the return bug and the type conversion that can be done with it.

Before using anything else in the class system, call the function that initializes the class system:
call InitClasses()

Now you can create your classes (best do that in your initialization function right after the call to InitClasses.
call CCreate("Foo", null)
call CCreate("Bar", "Foo")
This creates two new classes, Foo and Bar. Bar inherits from Foo.

Note that throughout the class system a C at the beginning of the function means it deals with classes while an I means it deals with Instances. IC means it deals with the class of the given instance.

To add a member to the class, use:
call CAddMember("Foo", "MyMember", "integer")
That adds a member of type integer with the name MyMember to Foo.
The currently directly supported types are string, integer, real, group, location, unit, effect, stack, locationstack, timer, terraindeformation, destructable, trigger, boolean.
You can still use other types via the return bug.
Some of these types are automatically destroyed when an instance is destroyed like groups, timers, ...

To add a method to a class, use:
call CAddFunction("Foo", "MyFunction", "MyRealFunction")
That means add the method MyFunction to Foo, and the real implementation is function MyRealFunction. That means you somewhere have function MyRealFunction takes nothing returns nothing ...

To make an added method the constructor use:
call CSetConstructor("Foo", "MyFunction")
MyFunction is now the constructor.

call CSetDestructor("Foo", "MyFunction")
The same, except it is the destructor we set.

Now the class is created and we can instantiate it as often as we want:
local string my_instance = ICreate("Foo")
This creates an instance of the class Foo and the reference to that instance is stored in my_instance. The constructor MyFunction (which is MyRealFunction) will automatically be called.

Once we do not need an instance any more, we can destroy it:
call IDestroy(my_instance)
The destructor will automatically be called before destroying it.

Now we want to call some methods. There are about 5 ways to call a method:

1) Calling it right now:
call ICall(my_instance, "MyFunction")
This calls MyFunction of my_instance at once. The return value is ignored.

2) Calling it right now and using the return value:
local string s = ICall(my_instance, "MyFunction")
This calls MyFunction of my_instance at once. The return value is stored in s (the return value is always a string, use the return bug or wrapper classes to return other types).

3) Calling it after a certain time has passed (The calling function will continue):
call ICallTimed(my_instance, "MyFunction", 5, false)
This calls MyFunction of my_instance after 5 seconds have passed.

4) Calling it every x seconds:
call ICallTimed(my_instance, "MyFunction", 5, true)
This will call MyFunction of my_instance every 5 seconds until MyFunction returns something other than null.

5) Calling it when a certain trigger fires:
call ICallTriggered(my_instance, "MyFunction", my_trigger, false)
The method MyFunction of my_instance is called once my_trigger fires. The last boolean works similar to the one with Timed.

All these calls are polymorph. So the system will look for the method first in the class of the passed instance, then in its super class and so on.


Now the last part. How to access the instances from within methods (or other functions):
The function GetThis() returns the this pointer if you are within a method. Best put it in a local variable at the start of the method. Like:
local string this = GetThis()

To get a member use:
set i = IGetInteger(this, "MyMember")
Get MyMember from the instance this and store it in i.

To set it use:
call ISetInteger(this, "MyMember", 5)
Set MyMember from the instance this to 5.

There are similar functions for the other types.
For class variables the functions are nearly the same except that they have a C instead of a I and they take the class name and not the instance:
call CSetReal("Foo", "MyClassVariable", 3.6)


That are most of the useful aspects. If you have further questions, feel free to ask.
I hope it helps.
02-02-2005, 03:32 PM#12
Tabris
ok! Thanks.
Hum... I have done a search about return bug but there is 25 pages... I'm not brave enough to open all thread ^_^ Can you help me?

Quote:
Originally Posted by a good moderator
To add a method to a class, use:
call CAddFunction("Foo", "MyFunction", "MyRealFunction")
That means add the method MyFunction to Foo, and the real implementation is function MyRealFunction. That means you somewhere have function MyRealFunction takes nothing returns nothing ...
Well... We are agree that "MyFunction" is function for instance? I am just curious but does your system allow static function?

And what the script I have to add to my map before before using your system?
02-03-2005, 06:34 AM#13
AIAndy
In short: The return bug is the bug that only the last return in a function is checked for having the right type. That means by having a return before that one you can return anything. The use is that with that you can typecast. So you can store any type of variable in an integer (although interpreting it as an integer will usually return crap).

Quote:
Well... We are agree that "MyFunction" is function for instance?
I don't quite get what you mean with that sentence.

If you mean with static function that polymorphism does not apply to it, then you can simulate that by using a special call function that calls a method of a specified super class.

The script you have to add is the one you find at the link you posted. Best put it in the map header. The part at the beginning between globals and endglobals needs to be created as variable in that map though (and not copied to the map header).
02-03-2005, 01:45 PM#14
Tabris
For the script I have'nt notice you post in this website all the script.
My question was : Can you declare static function?
CAddFunction is for non-static function, no?

About the return bug, does JASS allow "empty return" for "function takes nothing returns nothing"?
If it does so function that retuns nothing can return something too?
like
------code----- variable : integer i
return i
return
----code------
And well I don't see exactly what it can use for.....
02-05-2005, 12:23 PM#15
AIAndy
Quote:
Originally Posted by Tabris
For the script I have'nt notice you post in this website all the script.
My question was : Can you declare static function?
CAddFunction is for non-static function, no?
What exactly do you mean with static function ?

Quote:
About the return bug, does JASS allow "empty return" for "function takes nothing returns nothing"?
If it does so function that retuns nothing can return something too?
like
------code----- variable : integer i
return i
return
----code------
And well I don't see exactly what it can use for.....
Never tried it. But even if he accepts that you could likely not access that return value. Just try it out if you are interested.