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

how to properly initialize globals variable on TS-Template?

Status
Not open for further replies.

V

V

Level 7
Joined
Jan 13, 2019
Messages
284
For build my map, im using warcraft 1.31 with TS along this template cipherxof/wc3-ts-template

But im getting problem when assign values to globals variables, when i initialize them can get the value when printing them, but when i imported them to anothers .ts script they have nil value, why this happend?, what im doing wrong?

This is how i set my map:

main.ts

JavaScript:
import { addScriptHook, W3TS_HOOK } from "w3ts/hooks";
import { File } from "w3ts/system/file"
import { Timer } from "w3ts/handles/timer";
import { init_map } from "init_map";
import { init_players } from "init_players";
import { init_game } from "init_game";

function Main() {
  const gameStart = new Timer();

  gameStart.start(1.0, false, ()=>{
    try {
      init_map();
      init_players();
      init_game();
    }
    catch (e) {
        File.write("error.txt", e);
    }
  })
}

addScriptHook(W3TS_HOOK.MAIN_AFTER, Main);

init_map.ts
JavaScript:
import { Hero } from "hero";
import { Players } from "w3ts/globals/index";
import { MapPlayer, Rectangle, Timer, Unit } from "w3ts/handles";

export const playing:MapPlayer[] = [];
export const team1:MapPlayer[] = [];
export const team2:MapPlayer[] = [];

export let team1_leader:MapPlayer;
export let team2_leader:MapPlayer;

export let timer_start:Timer;
export let timer_spawn:Timer;

export let tower1_1: Unit;
export let tower1_2: Unit;
export let tower1_3: Unit;
export let tower1_4: Unit;
export let tower1_5: Unit;
export let barrack1: Unit;
export let core1: Unit;

export let tower2_1: Unit;
export let tower2_2: Unit;
export let tower2_3: Unit;
export let tower2_4: Unit;
export let tower2_5: Unit;
export let barrack2: Unit;
export let core2: Unit;

export let start:Rectangle;
export let base_left:Rectangle;
export let base_right:Rectangle;
export let spawn_left:Rectangle;
export let spawn_right:Rectangle;

export let core_backdoor = {left:0, right:0}

export let player_done = {amount:0};

export const selector = FourCC("h00F");
export const meleeUnit = FourCC("h005");
export const rangedUnit = FourCC("h006");

export const heroes:Hero[] = []

export function init_map(){
    team1_leader = Players[10];
    team2_leader = Players[11];

    team1.push(team1_leader);
    team2.push(team2_leader);

    timer_spawn = new Timer();
    timer_start = new Timer();

    tower1_1 = Unit.fromHandle(gg_unit_h001_0012);
    tower1_2 = Unit.fromHandle(gg_unit_h002_0035);
    tower1_3 = Unit.fromHandle(gg_unit_h003_0029);
    tower1_4 = Unit.fromHandle(gg_unit_h008_0046);
    tower1_5 = Unit.fromHandle(gg_unit_h008_0026);
    barrack1 = Unit.fromHandle(gg_unit_h004_0028);
    core1 = Unit.fromHandle(gg_unit_h000_0014);

    tower2_1 = Unit.fromHandle(gg_unit_h001_0097);
    tower2_2 = Unit.fromHandle(gg_unit_h002_0111);
    tower2_3 = Unit.fromHandle(gg_unit_h003_0086);
    tower2_4 = Unit.fromHandle(gg_unit_h008_0093);
    tower2_5 = Unit.fromHandle(gg_unit_h008_0094);
    barrack2 = Unit.fromHandle(gg_unit_h004_0067);
    core2 = Unit.fromHandle(gg_unit_h000_0075);

    tower1_2.invulnerable = true;
    tower1_3.invulnerable = true;
    tower1_4.invulnerable = true;
    tower1_5.invulnerable = true;
    barrack1.invulnerable = true;
    core1.invulnerable = true;

    tower2_2.invulnerable = true;
    tower2_3.invulnerable = true;
    tower2_4.invulnerable = true;
    tower2_5.invulnerable = true;
    barrack2.invulnerable = true;
    core2.invulnerable = true;

    base_left = Rectangle.fromHandle(gg_rct_BASE_LEFT);
    base_right = Rectangle.fromHandle(gg_rct_BASE_RIGHT);
    spawn_left = Rectangle.fromHandle(gg_rct_SPAWN_LEFT);
    spawn_left = Rectangle.fromHandle(gg_rct_SPAWN_RIGHT); 
}

init_players.ts
JavaScript:
import { playing, team1, team2 } from "init_map";
import { Players } from "w3ts/globals/index";

export function init_players(){
    for (const p of Players) {
        if (p.controller === MAP_CONTROL_USER && p.slotState === PLAYER_SLOT_STATE_PLAYING) {
          playing.push(p);
          p.id < 5 ? team1.push(p) : team2.push(p);
        }
      }
}

init_game.ts
JavaScript:
import { playing, selector, start, timer_spawn, timer_start } from "init_map";
import { Rectangle, TimerDialog, Trigger, Unit } from "w3ts/handles/index";

export function init_game(){
    const trig = new Trigger();
    const td = new TimerDialog(timer_start);
    const start = Rectangle.fromHandle(gg_rct_START)
    trig.registerTimerEvent(1.00, false);

    trig.addAction(()=>{
        for (const p of playing) {
            let wisp = new Unit(p, selector, start.centerX, start.centerY, 90);
        }

        td.display = true;
        td.setTitle("Start in...");
        timer_start.start(20.0, false, ()=>{
            td.destroy();
            timer_spawn.start(10.0, true, null);
        });   
    });
}

In this case init_game dont execute completely, because timer_start is nil.
Until now only players variables are accesible, but rects, timers, or units dont work outside the init_map.ts script.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Just to reiterate from discord - you can't export undefined variables. If you don't yet have values for them, you can export a wrapper object which will later contain them.

So this won't work:
JavaScript:
// undefined so nothing is exported
export let tower2_1: Unit;
export let tower2_2: Unit;

...

// at this point the exports/imports are already irrelevant so this is never visible outside
tower2_1 = Unit.fromHandle(gg_unit_h001_0097);
tower2_2 = Unit.fromHandle(gg_unit_h002_0111);

But this will:
JavaScript:
// visible outside, but still an empty array
export let towers2: Unit[] = [];

...

// now the array will actually have stuff in it
towers2[0] = Unit.fromHandle(gg_unit_h001_0097);
towers2[1] = Unit.fromHandle(gg_unit_h002_0111);

It doesn't have to be an array, any object will do, so long as it exists when you export it.
 
Status
Not open for further replies.
Top