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

Classes in vjass?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
Could someone show me how you create "classes" with vjass?

I would like to create a class based on a unit, "Farm" The information contained here should be:
Count (int)
Group (group)
Effectivity (real)
Time (int)
Resource (int)

- How to create this?
- How to add/remove units from group?
- How to change the int's and reals?

When the unit dies the class is removed.
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
Read vjass manual in regard to getting basic knowldge about structs and vjass features.
- How to add/remove units from group?
Adding/removing unit from a group does not require you to use vjass. Those are native functions declared as:
JASS:
native GroupAddUnit takes group whichGroup, unit whichUnit returns nothing
native GroupRemoveUnit takes group whichGroup, unit whichUnit returns nothing
- How to change the int's and reals?
The is one or two typecasting tuts in Tutorials Section, however, this should be more than enough.

- How to create this?
Your struct could take shape of:
JASS:
globals
    // connect unit's id with struct's instance
    private integer array instances
endglobals

struct Farm extends array
    unit farm
    group group
    integer count
    real effectivity
    integer time
    integer resource

    implement Alloc

    static method create takes (params here) returns thistype
        local thistype this = allocate()
        
        // set stuff
        set instances[GetUnitId(farm)] = this

        return this
    endmethod

    method destroy takes nothing returns nothing
        // if you need to, destroy the group & unit
        set farm = null
        set group = null

        call deallocate()
    endmethod

    static method onDeindex takes nothing returns boolean
        local thistype this = instances[GetIndexedUnitId()]

        if (this != 0) then
            call destroy()
        endif

        return false
    endmethod

    private static method onInit takes nothing returns nothing
        call RegisterUnitIndexEvent(Condition(function thistype.onDeindex), EVENT_UNIT_DEINDEX)
    endmethod

endstruct
Alloc because we are lazy. You will need an Unit Indexer library for such stript - DEINDEX event should be very usefull for you. In this example I've used UnitDex.
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
Alloc, srsly? Are you fucking kidding me?

He has no fucking clue of vJass and wants to understand how structs work. And the first code you show him uses a custom allocator?
This is bullshit. Let him learn how vJass works, if he mastered it he can think about all that custom crap. If he starts using these things before he even knows the basics he will never understand why hes actually using it. This is exactly how all the hives users were turned into braindead zombies with no freewill who walk around the whole day yelling "AAAAAAAALOC", "EXTENDS ARRAYYYYYY", "BRAAAAIN"...

@Pinzu: just read the jasshelper manual
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
@muzzel you are fucking angry on someone or something? - dont come here. Go yell on somelese, if he allows you to. Pinzu is asking about plenty of things daily and I saw that we he is not afraid about vjass. I have shown him a "possible shape" - there is nothing wrong with that.

And as you could see/read from previous threads - many times I've been explaining stuff he was asking about in simple way. Here, I've posted manual because thats what he needs to read at first. I've also linked few tutorials he should read before moving on in earlier threads.
He asked about possible solution - how that struct might look like.

@sethmachine nope. If you don't use allocation via table, how can normal struct instance have higher value than 8191? Simple, it can not. Thats why we use arrays in such case. GetUnitId returns value in 1-8191 range in most popular indexers to allow us to use arrays.
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
Dude there is NO advantage using alloc here, so why confuse him like heck? I know its really popular here to bloat up simple scripts with lots of fancy libraries for "more performance" (wat, 0.00234782934% faster?, congratz genious). But hes trying to learn vJass, so this is hardly the time and place for "hey look im using hipster dependencies, #yolo #swag".
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
(...)

I've used alloc not because I'm spead freak (which I'm not) but because I didn't want to create a code bloat - it's better for him to see shorter code at first. Meaby I should have just posted "struct Farm" without anything else, yet pasting allocate/deallocate would not bring anything good either.
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
sethmachine said:
JASS:
set instances[GetUnitId(farm)] = this
Can't handle ids go easily above 8192? For that he should use Table or a hashtable?

bannar said:
@sethmachine nope. If you don't use allocation via table, how can normal struct instance have higher value than 8191? Simple, it can not. Thats why we use arrays in such case. GetUnitId returns value in 1-8191 range in most popular indexers to allow us to use arrays.

I was not referring to the value of the struct instance, which do range from 1-8191, because they are just indices for parallel arrays or whatever. I mean the unit handle id. And what exactly is GetUnitId? farm is a unit handle. Are you referring to the unit id type?

So I thought UnitHandleIds can be very high valued, e.g.

JASS:
integer array A
local integer i = 0
local integer unitHandleId = 0
loop
  exitwhen i = 8000
  set unitHandleId = GetHandleId(CreateUnitAtLoc(..., 'hfoo', ...))
  set A[unitHandleId] = i
  set i = i + 1
endloop

So you are telling me Bannar that I won't get array index out of bounds exception in this loop? I was certain HandleIds can be as high valued as rawcodes.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
@sethmachine are you for real? Have you read the second part of my message?
GetUnitId is one of the most popular custom functions.. why? UnitDex and pretty much every indexer welcomes you. You are talking about GetHandleId... Please, read carefully.

I was using a Bribes GUI unit Indexer. :)

Okey, so I implement Alloc and UnitDex. Then what? Could you update your code to incloude a event that then creates the farm unit. You then setup the instance. Then you create 2 random units. Add them to the farm.group and change the values. Note that the real/integer values can start differently?
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
Before we go on: do you understand why have you used both alloc & indexer and what are they for? In case, we can move further with simpler example of that one is too hard for you. Not that I don't believe in you, but I'd like to avoid angry penguins running around ^)^

Btw, you will learn nothing if I just write the code down for you. It's best if you ask - community answers -> you poste your code, we help you improve it. That is basically how help zones works.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Before we go on: do you understand why have you used both alloc & indexer and what are they for? In case, we can move further with simpler example of that one is too hard for you. Not that I don't believe in you, but I'd like to avoid angry penguins running around ^)^

Btw, you will learn nothing if I just write the code down for you. It's best if you ask - community answers -> you poste your code, we help you improve it. That is basically how help zones works.

The Unit Udex you use for event registration. When to create the struct and when to remove it. The Alloc, well, I don't really know why it works, or even how, just that you use the library to make the commands you do?

On the issue of learning, its true there are gaps, and there will be gaps and frankly I can accept it. If you have a problem with showing someone who is somewhat reluctant(?) in taking the full course, I understand. My main purpose with all my posts are to implement something in my map. I now think structs are useful for what I want to do, if you can show me how to implement it, perhaps I pick up somethings that I can use in the future, maybe I don't. But personally I'm happy to be in this "need-to-know" basis with all its limits, but I can understand it must be bothersome to see the same guy post quesion after question..? (You will see alot less of my spam when school starts, hopefully.) :thumbs_up:

Btw, you will learn nothing if I just write the code down for you. It's best if you ask - community answers -> you poste your code, we help you improve it. That is basically how help zones works.
I just need to see what you did inside a real trigger so I can understand how to implement it properly, I can't get it to work so you can't see a code. I mean, even your globals outside a library or a scope(?) is confusing to someone like me.

Either way, thanks for introducing me to a new unit indexer, im using that now instead.

Why did you use the "API" from the UDex system as opposed to GetUnitUserData(farm)?
 
Last edited:
Level 14
Joined
Jun 27, 2008
Messages
1,325
Okey, so I implement Alloc and UnitDex. Then what?

The Alloc, well, I don't really know why it works, or even how, just that you use the library to make the commands you do?

Congratulations, Bannar. Thanks to your bullshit mr pinzu here has now a cooking recipe for structs burned into his mind which says:

~~~ COOKING RECIPE FOR STRUCTS/CLASSES (DONT KNOW DIFFERENCE LOL #YOLO) ~~~
  1. type the magic words "struct name extends array implement Alloc" because thats just what you do
  2. [...]
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
The Unit Udex you use for event registration. When to create the struct and when to remove it. The Alloc, well, I don't really know why it works, or even how, just that you use the library to make the commands you do?
Yes, I've used indexer for event registration - you asked for code that removes an instance if given unit (farm) is removed, that is why I needed an indexer. However, I wanted you to tell me this :)
You need to read the manual to get better insight about structs and then move to structs that extends array i.e array of struct type.

I've used alloc frankly because I'm lazy to write allocation over and over again. We use struct when we want to implement objects - connect fields with behaviour i.e methods. If so, each instance has to be allocated before we can actually use it. Destroy method allows struct to clean up shit left behind given instance and recycle it's index.
but I can understand it must be bothersome to see the same guy post quesion after question..? (You will see alot less of my spam when school starts, hopefully.) :thumbs_up:
Not at all. If I wasnt here to help, I wouldnt even open the thread. I'm aware that someone might need plenty of statements to finaly understand what it is all about :p
I just need to see what you did inside a real trigger so I can understand how to implement it properly, I can't get it to work so you can't see a code. I mean, even your globals outside a library or a scope(?) is confusing to someone like me.
Again, I didn't want to paster everything including custom allocation and deallocations so you get confused with code bloat. Of course you should put this inside library or scope - because of "private" my code wouldn't even compile - those can be placed only within scope/library.
Why did you use the "API" from the UDex system as opposed to GetUnitUserData(farm)?
I wanted to mark that I'm using an indexer library without actually writing all the necessary stuff (library + requirements / or static if with GetHandleId in case you don't have any indexer etc.).

I'm out for today, good luck with studing structs.

Congrats muzzel, instead of being helpfull, you are just creating useless spam. Penguins are ashamed of you.
 

Deleted member 219079

D

Deleted member 219079

Mk holy crap no need to fight ~~

class in java / other language = struct in vJass

check my tut for basics of structs, i explain some words of concept too

also don't use bribe's unit indexer ~~
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
also don't use bribe's unit indexer ~~
Since when Bribe's stuff is crap? It's perfect UI designed for GUI-ers. Of course, if we use vJass there is no reason to choose that one over Nes' one or UnitDex.

Hope that I've not offended you - I'm just sure that Bribe's scripts are made with highest quality, and I've found no reason to deny that.
 

Deleted member 219079

D

Deleted member 219079

I used Bribes UI when I was a GUIer too ^_^ but our op is starting to grow into a jasser :p
 
Status
Not open for further replies.
Top