Login

russian armor

win condition that will balance resources based on team size

29 May 2019, 02:09 AM
#1
avatar of kwok

Posts: 60

Hello, I'm trying to learn scar and am a rookie to lua/programming in general. Can someone tell me what is wrong with the following? Note: I'm doing this for CoH1, but I assume it should be roughly the same for CoH2.

The wincondition I'm trying to make is something that balances resource rate based on team size. So if it's a 1v2, then the team with less people will get more resources.

30 May 2019, 14:38 PM
#2
avatar of eliw00d
Honorary Member Badge

Posts: 756 | Subs: 8

So, the assignment operator is not the same as the equality operator.


if Player_GetTeam(i)=1 then


should be


if Player_GetTeam(i)==1 then


Also, unless it just cut off, you don't have an end for your function.

You should really use local variables to store anything that you use more than once. For example, you call World_GetPlayerAt multiple times. Function calls can become expensive in loops. You should also indent for readability. I highly suggest using Visual Studio Code or some other IDE to help with that.

Here is a cleaned up version of your script:

g_CheckAnnihilate = true

function OnInit()
team_a = 0
team_b = 0

for i = 1, World_GetPlayerCount() do
if (Player_GetTeam(i) == 1) then
team_a = team_a + 1
else
team_b = team_b + 1
end
end

if (team_a > team_b) then
b_ratio = team_a/team_b
a_ratio = 1
else
a_ratio = team_b/team_a
b_ratio = 1
end

for i = 1, World_GetPlayerCount() do
local player = World_GetPlayerAt(i)
Player_SetPopCapOverride(player, 350)
if (Player_GetTeam(i) == 1) then
Modify_PlayerResourceRate(player, RT_Manpower, a_ratio)
Modify_PlayerResourceRate(player, RT_Munition, a_ratio)
Modify_PlayerResourceRate(player, RT_Fuel, a_ratio)
else
Modify_PlayerResourceRate(player, RT_Manpower, b_ratio)
Modify_PlayerResourceRate(player, RT_Munition, b_ratio)
Modify_PlayerResourceRate(player, RT_Fuel, b_ratio)
end
end
end

Scar_AddInit(OnInit)
31 May 2019, 03:42 AM
#3
avatar of kwok

Posts: 60

Thank you so much for the response! This makes sense. Ah I feel embarrassed, you are right I am missing an end on the function.

After I've added the clean version of your code, I still get a fatal scar error. The only additional line that I didn't include in the original post was

import("ScarUtil.scar")

at the beginning of the code. Note, that I did this in CoH1 if that adds any nuance.


Another fundamental question, when would I need to put something in parenthesis and when wouldn't I? I don't quite understand why you put parenthesis around the if condition (Player_GetTeam(i) == 1)
31 May 2019, 05:09 AM
#4
avatar of eliw00d
Honorary Member Badge

Posts: 756 | Subs: 8

Yeah, you always need that import due to maps using the functions it provides.

You don't need to use parentheses, but it's not a bad habit to get into if you ever wanted to program in a different language. It is entirely optional in Lua, so up to you in the end. I just did so out of habit and I like the way it looks. *shrug*
31 May 2019, 05:14 AM
#5
avatar of eliw00d
Honorary Member Badge

Posts: 756 | Subs: 8

Also, just noticed something else:


Player_GetTeam(i)


should be


Player_GetTeam(player)


Since it expects a player and not a number.
1 user is browsing this thread: 1 guest

Ladders Top 10

  • #
    Steam Alias
    W
    L
    %
    Streak
Data provided by Relic Relic Entertainment

Replay highlight

VS
  • U.S. Forces flag cblanco ★
  • The British Forces flag 보드카 중대
  • Oberkommando West flag VonManteuffel
  • Ostheer flag Heartless Jäger
uploaded by XXxxHeartlessxxXX

Board Info

842 users are online: 842 guests
0 post in the last 24h
7 posts in the last week
34 posts in the last month
Registered members: 49117
Welcome our newest member, topcsnvncom
Most online: 2043 users on 29 Oct 2023, 01:04 AM