How do you check for total active totems?

Endif · 218

Endif

  • Jr. Member
  • **
    • Posts: 7
on: August 11, 2019, 07:07:17 pm
I'd like to check how many totems are active while maintaining the totem's original purpose (generate aether).

There should be a CONST_ENERGYLEVEL because CONST_AMMO isn't working for the totems, assuming because it's green packets, not red.

Any guidance would be helpful,
Thanks!



Builder17

  • Community Guild
  • *****
    • Posts: 1025
  • My account picture is from Nitrome Mutiny.
Reply #1 on: August 12, 2019, 05:50:23 am
I'd like to check how many totems are active while maintaining the totem's original purpose (generate aether).

There should be a CONST_ENERGYLEVEL because CONST_AMMO isn't working for the totems, assuming because it's green packets, not red.

According to this, CONST_AMMO works, but only for CRPL created totems http://knucklecracker.com/wiki/doku.php?id=crpl:crpltutorial:code_examples#connect_totems_to_win

Modified version to suit your purpose better:
Code: [Select]
# Original author: J
# This script has to be called totem.crpl
# ------------------------------------------
once
"TOTEM" CurrentCoords CreateUnit ->totemUID
"totem" ->unitName
4 delay self 0 Destroy
endonce

Code: [Select]
# Author: J
# ------------------------------------------
$totemsrequired:1
$scriptname:"totem.crpl"

once
ClearTraceLog ShowTraceLog
2 delay
"unitName" "totem" GetCoresWithVar dup ->totemcount 0 do # Finds all crpl created totems
<-scriptname "totemUID" getScriptVar
"totem" I concat ->!
loop
endonce

0 ->activecount
<-totemcount 0 do
"totem" I concat <-! dup ->unitUID CONST_AMMO GetUnitAttribute
0 gt if
<-activecount 1 add ->activecount
"Totems currently active: " <-activecount Trace2
endif
loop

# If all totems are active, blow up all enemy units (insta-win)
<-activecount <-totemsrequired gte if
#Replace these with your functionality
#0 0 99999 GetEnemyUnitsInRange 0 do
# dup Self neq if
# 2 Destroy
# else
# pop
# endif
# loop
# Self 2 Destroy
endif



GoodMorning

  • Community Guild
  • *****
    • Posts: 3369
  • (Pusillanimous)
Reply #2 on: August 12, 2019, 11:38:56 pm
It works on any Totems, the CRPL part in that script is to acquire the UID of the Totems properly.

A narrative is a lightly-marked path to another reality.


Endif

  • Jr. Member
  • **
    • Posts: 7
Reply #3 on: August 13, 2019, 01:37:07 am
Sorry, I meant to update this post the other night, but I figured out a way that works.

Code: [Select]
once
106 242 GetUnitAt ->totem1
210 5   GetUnitAt ->totem2
63 38   GetUnitAt ->totem3
199 104 GetUnitAt ->totem4
endonce

5 1 do
"totem" I concat <-! CONST_AMMO GetUnitAttribute
5 gt <-activeTotems add ->activeTotems
loop



Endif

  • Jr. Member
  • **
    • Posts: 7
Reply #4 on: August 13, 2019, 08:36:58 am
It works on any Totems, the CRPL part in that script is to acquire the UID of the Totems properly.

I've heard about some issues with grabbing UID on the discord chat. What's that all about exactly? I mean, I understand that GetUnitAt can retrieve multiple units and that could be an issue, but what's the problem with using it on an application specific situation in which there will only be one unit at that location?

The only issue I can surmise is in the case of a Canon flying overtop of it. Would that trigger the GetUID?



GoodMorning

  • Community Guild
  • *****
    • Posts: 3369
  • (Pusillanimous)
Reply #5 on: August 13, 2019, 08:53:09 pm
It could. The usual reason for caution is that people try to get the UID of a PZ or similar unit, and end up with the wrong one. Sometimes they end up destroying the core which runs the script.

For this application, you should be fine. In fact, you could even hard-code the UIDs directly (not a suggestion, merely an observation).

A narrative is a lightly-marked path to another reality.


Endif

  • Jr. Member
  • **
    • Posts: 7
Reply #6 on: August 14, 2019, 12:34:12 am
In fact, you could even hard-code the UIDs directly (not a suggestion, merely an observation).
Reh-heh-heaally?

I wasn't too sure how the UID's work. I figured they were a random number derived from something and assigned at the beginning of the game. How do I know what will affect how the UID is derived? Are they the same if I move them?



GoodMorning

  • Community Guild
  • *****
    • Posts: 3369
  • (Pusillanimous)
Reply #7 on: August 14, 2019, 06:47:18 pm
The UID is assigned when the unit is created, and kept until it is destroyed. For example, a Mortar will have the same UID when you move it, save/load, and change the settings.

A PZ is not the same unit as the Emitter that left it, and a Guppy base is not the same unit as the aircraft. Thus, they will have different UIDs.

UIDs are unique, so they won't be reused. However, it's generally best to treat them as "magic" unless you have a very specific reason not to.

For this, I would try to use GetAllUnitsInRange and then find the Totem. There are some circumstances where a hardcoded UID is good, but not many of them. (And it's risky even then. What if you accidentally miss when using "delete" in the editor, forget to change the script, and you only find the problem after playing through to what should be the end?)

A narrative is a lightly-marked path to another reality.