Teleport Help

Everything about mods can be found here.

Moderator: Morax

Teleport Help

Postby Dudekahedron » 03 Nov 2016, 22:36

Hi guys,
I've run into issue trying to modify the teleport script. I'm trying to add in a return functionality, and I've got the following so far:

In \mods\MyMod\hook\lua\sim\Units.lua:
Code: Select all
    OnTeleportUnit = function(self, teleporter, location, orientation)
        local ReturnTime
        local Return
        local ReturnPos
        local bp = self:GetBlueprint().Economy
        if bp then   
            if bp.TeleportReturn == true then
                ReturnTime = bp.TeleportReturnTime
                Return = true
                ReturnPos = self:GetPositionXYZ()
            else
                ReturnTime = false
                Return = false
            end
        end
        if self.TeleportDrain then
            RemoveEconomyEvent( self, self.TeleportDrain)
            self.TeleportDrain = nil
        end
        if self.TeleportThread then
            KillThread(self.TeleportThread)
            self.TeleportThread = nil
        end
        self:CleanupTeleportChargeEffects()
        self.TeleportThread = self:ForkThread(self.InitiateTeleportThread, teleporter, location, orientation)
        if Return == true then
            WaitSeconds(5)
            self.TeleportThread = self:ForkThread(self.InitiateTeleportThread, teleporter, ReturnPos, orientation)
        end
    end,

So, in bp.Economy in relevant units I've added TeleportReturn = true and TeleportReturnTime = 'n'

In game, I get the following error:
Code: Select all
WARNING: SCR_LuaDoFileConcat: Loading "c:\programdata\faforever\gamedata\lua.nx2\lua\sim\unit.lua" failed: c:\programdata\faforever\gamedata\lua.nx2\lua\sim\unit.lua(4081): unexpected symbol near `='

There's 4047 lines in the original Unit.lua, so 4081 is line 34, specifically at the end of the OnTeleportUnit function.

Any ideas?

Edit: I should clarify, line 4081 is also the end of file, i had some redundant stuff following
Check out my mod (in development): /viewtopic.php?f=41&t=13326
User avatar
Dudekahedron
Avatar-of-War
 
Posts: 118
Joined: 10 Apr 2016, 22:01
Has liked: 42 times
Been liked: 54 times
FAF User Name: Dudekahedron

Re: Teleport Help

Postby Dudekahedron » 03 Nov 2016, 23:03

.... The comma at the eof was the issue.

So the new problem is it simply doesn't work now
Check out my mod (in development): /viewtopic.php?f=41&t=13326
User avatar
Dudekahedron
Avatar-of-War
 
Posts: 118
Joined: 10 Apr 2016, 22:01
Has liked: 42 times
Been liked: 54 times
FAF User Name: Dudekahedron

Re: Teleport Help

Postby KeyBlue » 03 Nov 2016, 23:10

I assume that the code contains the entire file.

If you want to add functions to Unit class, you can't simply write another function.
Since the class ends at the end of the original file, this new function is outside of the class.

The error is probably caused by the ',' , since this is an unexpected symbol outside of tables.

Try this :

Code: Select all
local oldUnit = Unit
Unit =  Class(oldUnit) {
    OnTeleportUnit = function(self, teleporter, location, orientation)
        local ReturnTime
        local Return
        local ReturnPos
        local bp = self:GetBlueprint().Economy
        if bp then   
            if bp.TeleportReturn == true then
                ReturnTime = bp.TeleportReturnTime
                Return = true
                ReturnPos = self:GetPositionXYZ()
            else
                ReturnTime = false
                Return = false
            end
        end
        if self.TeleportDrain then
            RemoveEconomyEvent( self, self.TeleportDrain)
            self.TeleportDrain = nil
        end
        if self.TeleportThread then
            KillThread(self.TeleportThread)
            self.TeleportThread = nil
        end
        self:CleanupTeleportChargeEffects()
        self.TeleportThread = self:ForkThread(self.InitiateTeleportThread, teleporter, location, orientation)
        if Return == true then
            WaitSeconds(5)
            self.TeleportThread = self:ForkThread(self.InitiateTeleportThread, teleporter, ReturnPos, orientation)
        end
    end,
}
User avatar
KeyBlue
Priest
 
Posts: 403
Joined: 28 Jan 2016, 01:06
Has liked: 140 times
Been liked: 93 times
FAF User Name: KeyBlue

Re: Teleport Help

Postby Dudekahedron » 03 Nov 2016, 23:25

So by adding the two lines at the start, this is going into the class to overwrite the OnTeleportUnit function then?

Using this, I get a new error:
Code: Select all
WARNING: Error running OnTeleportUnit script in Entity dal401 at 1c5ef808: attempt to yield across metamethod/C-call boundary
         stack traceback:
            [C]: in function `WaitTicks'
            ...ogramdata\faforever\gamedata\lua.nx2\lua\siminit.lua(28): in function `WaitSeconds'
            ...gramdata\faforever\gamedata\lua.nx2\lua\sim\unit.lua(4092): in function <...gramdata\faforever\gamedata\lua.nx2\lua\sim\unit.lua:4066>


Why doesn't the waitseconds work?

Edit: There's a lot more at issue here than just the waitseconds as it turns out.
Simply removing the time delay;
Code: Select all
WARNING: Error running lua script: ...data\faforever\gamedata\lua.nx2\lua\system\utils.lua(44): attempt to loop over local `t' (a number value)

ughhh
Check out my mod (in development): /viewtopic.php?f=41&t=13326
User avatar
Dudekahedron
Avatar-of-War
 
Posts: 118
Joined: 10 Apr 2016, 22:01
Has liked: 42 times
Been liked: 54 times
FAF User Name: Dudekahedron

Re: Teleport Help

Postby Exotic_Retard » 03 Nov 2016, 23:38

ok so you cant just waitseconds anywhere you want.

it has to be in a place where its not gonna interfere with any other procedures (in units scripts they are all run at the same tick so you cant possibly wait for them since it would take more than 1 tick to execute), so you need to fork a thread and wait there, then do whatever you want after.

after you fork a thread you can wait freely until the thread is killed and you have code going back across that boundary (like if your thread calls sth like OnKilled)

it should be noted that forking works like the name suggests, the rest of the code can run after the fork is done and you get things running in parallel. so dont expect you to fork a thread wait 5s in it then resume your function. you will get your whole function done without delay and then have this empty useless thread terminate after 5s.

so really you want:

Code: Select all
self.DamnThread = self:ForkThread(self.AmazingThread)

    AmazingThread= function(self)
        --damn im blown away by this code
    end,

so you stick that forkthread line wherever, then perform things in the thread, and it either ends or you can kill it prematurely from some other function via:
Code: Select all
KillThread(self.DamnThread )

note that self.DamnThread is an entry that stores the self.AmazingThread, and you kill DamnThread and not AmazingThread.



you can also do it like this:
Code: Select all
self:ForkThread(    function()
        --damn im blown away by this code
    end)


this is for only one instance of the thread if you want to call it form only one spot, and you cant really interact with it while its running, best for one shot kind of stuff

i haven't looked into your code that far but it seems like you might want to put this return code into the teleport thread since that's there already so you can wait in there and stuff, so you dont fork a thread just to wait, that would be strange. also probably wont work.

hope this helps
User avatar
Exotic_Retard
Contributor
 
Posts: 1470
Joined: 21 Mar 2013, 22:51
Has liked: 557 times
Been liked: 626 times
FAF User Name: Exotic_Retard

Re: Teleport Help

Postby Dudekahedron » 04 Nov 2016, 01:10

Thanks to both of you for your help, although I haven't tried the fork thread method just yet

I attempted to simplify the procedure of returning by just calling the warp function on ReturnPos after the TeleportThread ends:
Code: Select all
        self:CleanupTeleportChargeEffects()
        self.TeleportThread = self:ForkThread(self.InitiateTeleportThread, teleporter, location, orientation)
       
        if Return == true then
            Warp(self, ReturnPos, orientation)
        end

Except it would never happen, and no errors were logged.
I noticed though that the teleport order never actually appears to complete. The image below is taken after the ACU teleported to the present location and I'm holding down the shift key:
Image
I'm guessing that the TeleportThread never ends on its own so it doesn't proceed to my if statement?
This behavior happens even without my mod being active!
Check out my mod (in development): /viewtopic.php?f=41&t=13326
User avatar
Dudekahedron
Avatar-of-War
 
Posts: 118
Joined: 10 Apr 2016, 22:01
Has liked: 42 times
Been liked: 54 times
FAF User Name: Dudekahedron

Re: Teleport Help

Postby Exotic_Retard » 04 Nov 2016, 02:30

pretty sure that since the thread is forked, most of what you quoted is running in parallel, so the code kinda breaks since it wants to warp back and there, possibly at the same time. you want to ensure that when you fork a thread you dont have any conflicting behaviors.

this is kinda a guess though, im a bit tired atm xD
User avatar
Exotic_Retard
Contributor
 
Posts: 1470
Joined: 21 Mar 2013, 22:51
Has liked: 557 times
Been liked: 626 times
FAF User Name: Exotic_Retard

Re: Teleport Help

Postby Dudekahedron » 04 Nov 2016, 02:41

I think the teleport function just doesn't terminate properly. Without my mod active, the teleport icon and the bar connecting unit to icon will remain after the unit teleports. It is just never an issue in game because the unit is quickly told to do something else, cancelling the teleport thread!
Although, I am also tired!
This will delay my new unit :/
Check out my mod (in development): /viewtopic.php?f=41&t=13326
User avatar
Dudekahedron
Avatar-of-War
 
Posts: 118
Joined: 10 Apr 2016, 22:01
Has liked: 42 times
Been liked: 54 times
FAF User Name: Dudekahedron

Re: Teleport Help

Postby Androish » 04 Nov 2016, 20:05

It will be fixed next patch (most likly)

https://github.com/FAForever/fa/issues/1659

It works in beta. so you can test your mod there.
Androish
Avatar-of-War
 
Posts: 115
Joined: 18 Mar 2013, 23:40
Has liked: 9 times
Been liked: 14 times
FAF User Name: Androish

Re: Teleport Help

Postby Dudekahedron » 06 Nov 2016, 17:35

Do you know when this patch will come?
Check out my mod (in development): /viewtopic.php?f=41&t=13326
User avatar
Dudekahedron
Avatar-of-War
 
Posts: 118
Joined: 10 Apr 2016, 22:01
Has liked: 42 times
Been liked: 54 times
FAF User Name: Dudekahedron

Next

Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest