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

[Lua] How to convert GetUnitGoldCost to LUA?

Level 6
Joined
Mar 27, 2019
Messages
51
Hi there,

I'm trying to convert this native to a LUA Function but I'm failing miserably.

Jass Function converted to LUA Function
JASS:
//Jass
native GetUnitGoldCost takes integer unitid returns integer
//-->LUA<--
function GetUnitGoldCost(unitid) end
Function Call
  • Custom script: udg_Player_GoldCost = GetUnitGoldCost(udg_Temp_Type))
I read on some topic that you need to convert unitid to FourCC whatever. How do I approach this properly so that the native works in LUA?
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,542
Hi there,

I'm trying to convert this native to a LUA Function but I'm failing miserably.

Jass Function converted to LUA Function
JASS:
//Jass
native GetUnitGoldCost takes integer unitid returns integer
//-->LUA<--
function GetUnitGoldCost(unitid) end
Function Call
  • Custom script: udg_Player_GoldCost = GetUnitGoldCost(udg_Temp_Type))
I read on some topic that you need to convert unitid to FourCC whatever. How do I approach this properly so that the native works in LUA?
You have an extra parenthesis at the end of your custom script.
  • Custom script: udg_Player_GoldCost = GetUnitGoldCost(udg_Temp_Type)
Here's an example in Lua:
Lua:
local u = GetTriggerUnit() -- replace this with your desired unit
local id = GetUnitTypeId(u)
udg_Player_GoldCost = GetUnitGoldCost(id)

I don't think you need to use FourCC() here, that's only for when you're typing in Rawcodes.
Lua:
local u = CreateUnit(Player(0), FourCC("Hpal"), 0, 0, 0)
 
Last edited:
Level 6
Joined
Mar 27, 2019
Messages
51
You have an extra parenthesis at the end of your custom script.
  • Custom script: udg_Player_GoldCost = GetUnitGoldCost(udg_Temp_Type)
Here's an example in Lua:
Lua:
local u = GetTriggerUnit() -- replace this with your desired unit
local id = GetUnitTypeId(u)
udg_Player_GoldCost = GetUnitGoldCost(id)

I don't think you need to use FourCC() here, that's only for when you're typing in Rawcodes.
Lua:
local u = CreateUnit(Player(0), FourCC("Hpal"), 0, 0, 0)
I changed it accordingly but if I run the code, the Trigger gets stuck while executing the GetUnitGoldCost function.

Here is my setup:

Map script
Lua:
function GetUnitGoldCost(unitid) end

  • Structure l Construct
    • Events
      • Unit - A unit Begins construction
    • Conditions
    • Actions
      • Custom script: local u = GetTriggerUnit()
      • Custom script: local id = GetUnitTypeId(u)
      • Custom script: udg_Player_GoldCost = GetUnitGoldCost(id)
      • Player - Add Player_GoldCost to (Owner of (Triggering unit)).Current gold
I'm confused, in JASS the native (function) is ready to use when pasted. However in Lua the Player doesnt even get the Gold + the Trigger does not even execute beyond that Gold Part.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,542
I just realized that this doesn't do anything, I'm a bit confused:
Lua:
function GetUnitGoldCost(unitid) end
I think you're relying on some kind of workaround stuff from the past that is not available to use with recent versions.

Instead, you can take advantage of the GetUnitIntegerField() function to pull some data that you've setup in the Object Editor:
Lua:
function GetUnitGoldCost(unit)
    return BlzGetUnitIntegerField(unit, UNIT_IF_GOLD_BOUNTY_AWARDED_BASE)
end
This will return you the unit's gold bounty base value. If you're already using this field then try to find an unused field that is supported.

Of course Gold Cost is an unavailable field because Warcraft 3.
 
Last edited:
Level 6
Joined
Mar 27, 2019
Messages
51
I just realized that this doesn't do anything, I'm a bit confused:
Lua:
function GetUnitGoldCost(unitid) end
I think you're relying on some kind of workaround stuff from the past that is not available to use with recent versions.

Instead, you can take advantage of the GetUnitIntegerField() function to pull some data that you've setup in the Object Editor:
Lua:
function GetUnitGoldCost(unit)
    return BlzGetUnitIntegerField(unit, UNIT_IF_GOLD_BOUNTY_AWARDED_BASE)
end
This will return you the unit's gold bounty base value. If you use that already, you can look for a different Integer field that you aren't using and that is supported by this Get() function.

Of course Gold Cost is an unavailable field because Warcraft 3.
I'm relying on the WC3 Modding Information Center. Just trying to convert the Function listed in the API from JASS to Lua.
JASS:
native GetUnitGoldCost takes integer unitid returns integer
The Function in JASS returns the value of how much the Unit thats being called costs.
However I don't find a way to get the Function to work for Lua.

I just used your method, works like a charm with a bit of extra work. Still curious how to fix the JASS Function tho.

Lua:
function GetUnitGoldCost(unit)


    return BlzGetUnitIntegerField(unit, UNIT_IF_LUMBER_BOUNTY_AWARDED_SIDES_PER_DIE)


end

  • Structure l Construct
    • Events
      • Unit - A unit Begins construction
    • Conditions
    • Actions
      • Set VariableSet Temp_Unit = (Triggering unit)
      • Custom script: udg_Player_GoldCost = GetUnitGoldCost(udg_Temp_Unit)
      • Player - Add Player_GoldCost to (Owner of Temp_Unit).Current gold
      • Unit - Change ownership of Temp_Unit to Neutral Passive and Retain color
      • Unit - Remove Temp_Unit from the game
Thanks for your advice Uncle!
 
Level 24
Joined
Jun 26, 2020
Messages
1,852
Hi there,

I'm trying to convert this native to a LUA Function but I'm failing miserably.

Jass Function converted to LUA Function
JASS:
//Jass
native GetUnitGoldCost takes integer unitid returns integer
//-->LUA<--
function GetUnitGoldCost(unitid) end
Function Call
  • Custom script: udg_Player_GoldCost = GetUnitGoldCost(udg_Temp_Type))
I read on some topic that you need to convert unitid to FourCC whatever. How do I approach this properly so that the native works in LUA?
What happens is you don't have to define the functions from the common.ai in Lua like in Jass.
Basically, just do nothing, you can just use the function normally.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,542
I'm confused, GetUnitGoldCost() is not a real function.
1697760011961.png
 
Level 6
Joined
Mar 27, 2019
Messages
51
Is a function from the common.ai file, to use it in Jass you have to add the line:
vJASS:
native GetUnitGoldCost takes integer unitid returns integer
While you can just use it in Lua.
Exactly, this is what confused me, you don't need to put the function into the Map Custom Script Code in Lua.

  • DEBUG404
    • Events
    • Conditions
    • Actions
      • Set VariableSet Temp_Type = (Unit-type of Temp_Unit)
      • Custom script: udg_Player_GoldCost = GetUnitGoldCost(udg_Temp_Type)
      • Game - Display to (All players) for 30.00 seconds the text: (String(Player_GoldCost))
This is all you need to figure out how much the Gold Cost of the called unit is in Lua.

Thanks alot HerlySQR!
 
Level 24
Joined
Jun 26, 2020
Messages
1,852
If you wonder why, the reason is because in Jass you first have to define a function to can write it later in the code.
JASS:
function First takes nothing returns nothing
    call Second() // This causes an error
endfunction

function Second takes nothing returns nothing
endfunction
While in Lua you don't need to define the function before writing it (unless is local):
Lua:
function First()
    Second() -- Doesn't cause an error
end

function Second()
end
Since the common.ai functions are defined after all the map scripts you have to write that line to you be able to use it in your map, while in Lua is not necessary.
 
Top