• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Allocate Real to unit

Status
Not open for further replies.
Level 13
Joined
Oct 16, 2010
Messages
731
Hi,

I am wondering if there is any way to allocate a Real to a Unit? Ideally without having to create arrays if that's possible? I'm not using Custom Value on units but I'm aware that can only be an integer so that's not very helpful!
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,182
Structs. It's a rather easy way if you know how to use them.

  • Untitled Trigger 001
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
        • Loop - Actions
          • Custom script: local integer i
          • Set u = (Picked unit)
          • Custom script: set i = number.create(udg_u)
      • Custom script: endfunction
      • Custom script: struct number
      • Custom script: unit u
      • Custom script: static method create takes unit u returns thistype
      • Custom script: local thistype this = thistype.allocate()
      • Custom script: set this.u = u
      • Custom script: call BJDebugMsg(GetUnitName(u) + " indexed to: " + I2S(this))
      • Custom script: return this
      • Custom script: endmethod
      • Custom script: endstruct
      • Custom script: function empty takes nothing returns nothing
  • Untitled Trigger 002
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • just change the "2" to the number you want to check.
      • Custom script: local integer i = 2
      • Custom script: local unit u = number(i).u
      • Custom script: call BJDebugMsg(GetUnitName(u) + " detected at " + I2S(i))
Result:
deb3f0464b216edecb5fa005b47abd78.png


edit: It is possible to go from unit to number as well, though it would require some editing.
 

sentrywiz

S

sentrywiz

If its not too many reals, you can create as many unit groups as you need and put specific units in those groups, then use each group for certain real.

Not sure why custom unit value is bad for you though. Why does it have to be a real?
 
Level 13
Joined
Oct 16, 2010
Messages
731
I was trying to think of a way to simplify my loot system. Currently units are placed in different unit groups depending on their tier (changes in difficulties) but then I have to check the unit type of the dead unit and get their loot chance from that (which is a real). And as it stands multiple units share the same loot chance, however as some units should have higher loot really I wanted to see if I could make it a bit more dynamic.

So for example this is kind of how it works... Using Footman and Rifleman as examples:
Unit dies - If unit is in T1 Group - If unit is Footman or Rifleman - set loot chance = 1.5

However given that there is a (for example) Footman Tier 2 + 3 and Rifleman Tier 2 + 3 I thought it might be easier to just give them the loot chance when they spawn, then when they die I just use that directly. So units could have differences as small as 0.1, making it a bit more interesting (and hopefully saving me some space)
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
You can ofcourse use a hashtable and store the drop chance values per unit type.
When you want to increase/decrease chance per unit individually (with the same unit type), then you really need an array with an indexer.

Apart from that, maybe a loot system may be better to use.
 

sentrywiz

S

sentrywiz

I was trying to think of a way to simplify my loot system. Currently units are placed in different unit groups depending on their tier (changes in difficulties) but then I have to check the unit type of the dead unit and get their loot chance from that (which is a real). And as it stands multiple units share the same loot chance, however as some units should have higher loot really I wanted to see if I could make it a bit more dynamic.

So for example this is kind of how it works... Using Footman and Rifleman as examples:
Unit dies - If unit is in T1 Group - If unit is Footman or Rifleman - set loot chance = 1.5

However given that there is a (for example) Footman Tier 2 + 3 and Rifleman Tier 2 + 3 I thought it might be easier to just give them the loot chance when they spawn, then when they die I just use that directly. So units could have differences as small as 0.1, making it a bit more interesting (and hopefully saving me some space)

That's basically what I said as a solution.

You can give them an ability. Make it as many levels as you want, and each level will represent a "real" for you. Then you add the ability to the unit when it spawns, set its level and later check the level when it dies.

Unit Index is what would work best if you want to follow Chaosy's way. Otherwise, stick to what you got.
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
The ability workaround is not a very good approach.
In the first place, you would have to create an ability with a massive amount (if not infinite) levels. This will also increase the loading time massively.
Then, when you want to set or load the chance value, you would have to calculate the chance based on the levels.
And eventually, you would be searching for another way after all.

Chaosy's example is just a unit indexer but then the other way around... which is also done by normal indexers.

What you are asking right now is "How can I change something from perfectly coded into something that is quite horrible?"... not the question most people ask who come here.

But let me ask you this: Why is it that you refuse to use arrays?
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,182
@wiet
Never said it was better :p

@Iceman
Which is exactly what a unit indexer does, it gives a number to a unit and it increase the more are added otherwise value would collide this fact is inevitable.
Difference is that I did not add the deindex part, otherwise it would be a functional indexer. It's harder to use, but that's not equal to straight out bad.

"Bad" is subjective though, so it might be insanely bad in your eyes I suppose.
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
@Iceman
Which is exactly what a unit indexer does, it gives a number to a unit and it increase the more are added otherwise value would collide this fact is inevitable.
Difference is that I did not add the deindex part, otherwise it would be a functional indexer. It's harder to use, but that's not equal to straight out bad.

Not exactly...

A unit indexer is a system that gives each unit a unique number (between 1 and 8190 for JASS).
No unit in the game can (should) have the same number as another unit.
The value of this number can be retrieved even when you only have the reference to the unit (unit variable).

What you do is store a list of units in an array and load the unit reference by using the integer that you linked it to.
What you made is exactly a unit array. No more, no less.
However, it is not necessarily bad and could be a first step when someone wants to learn how structs work.
The problem is that eventually, you will reach the limit at 8190 (8192 -1 for index numbers, -1 for JASS weird stuff with loading games with stuff on 8191)
 
chaosy,

  • it leaks
  • it does not recylcle
  • does not work after 8191 instances (with point above it's not good)
  • does not detect new units, only pre-placed
  • no easy way to get a indexed unit, nor the index

In GUI it wouild look like:
  • Set Indexer = Index + 1
  • Set UnitArray[Index] = GivenUnit
Then for indexing you always would run the gui trigger above, and set the GivenUnit variable first.
And sorry, but this method would be even better than with the one with structs because it had less overhead.

When people ask for help you should know better and just link to already existing system/tutorials out there if it's too complicated to create one quickly.
That's what they are made for.
It's of course you created quickly a code that seems like to solve it on a quick look, but it doesn't help anyone if it doesn't work properly.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,182
When did I state it was close to a finished product? And when did I say it was better than existing ones?

I said it's functional. Which means that it works, nothing more and nothing less.

And the code I gave him does what he requested, which was to get a unit by using a number. (without using arrays for whatever reason)

And I did not link the unit indexer because I do not like unit indexers, thus I don't encourage it. (silly, I know. No need to point that out)

edit: changed my post a few times.
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
Gotcha. I could reverse it though.
It would be easily done by looping through the instances in the struct.

Anyway, I was not trying to compete with existing systems. I just did what he requested without questions.

By looping through it, you woult take a very long time (compared to storing the integer in a hashtable stored under the handle id of the unit) so that is not really nice.

And you did the opposite of what he wanted :D
He wanted to store a real on a unit, not a unit on an integer.
 
Level 13
Joined
Oct 16, 2010
Messages
731
Well this has got a little heated...

I know how to do it with arrays as I have probably 30ish spells that are all MUI and use multiple arrays. I was just wondering whether there would be an easy way to allocate a real to a unit, like custom values are. Rather than the unit dying then going through a loop of the index to find the unit, then the relevant real.

It does seem that using an array would be easier (i think it'd need 2 arrays and 2 non-arrays?) than the above. Was just wondering if there was an easy alternative!

As it stands there is about 3-4 pretty lengthy triggers just to find the unit's loot chance so I wanted to simplify it. If arrays are the easier method I'll just use them!

Thanks for the help anyway!
 
Status
Not open for further replies.
Top