#make unit update cursor position while in cast phase

1 messages · Page 1 of 1 (latest)

rugged holly
#

I have been struggling to get unit to update cast position in 'real time' before OnSpellStart().
The thing is, spell remembers only position of the cursor when spell is being called, and I can still access it in OnAbilityPhaseStart. Yet it is immutable and just stays what it is from the beginning.
The GetCursorPosition inside OnSpellStart always returns Vector[0,0,0] so does the OnCahnnelThink and OnProectileThink
The walkaround I'm trying to use is sending custom event from panorama directly to the spell to update cursor position and it looks like this:

||`juggernaut_ability3 = class({})
LinkLuaModifier( "modifier_juggernaut_ability3", LUA_MODIFIER_MOTION_NONE )
local Cursor;
local pId
local caster;
local origin
local listen = false

function juggernaut_ability3:Spawn()
if IsServer() then
pId = self:GetCaster():GetPlayerID()
CustomGameEventManager:RegisterListener( "updateCursor",Dynamic_Wrap(juggernaut_ability3, "updateCursor"))
self:SetLevel(1)
caster = self:GetCaster()
origin = self:GetCaster():GetOrigin()
end
end

function juggernaut_ability3:updateCursor(event)
if listen == true then
if event.PlayerID == pId then
Cursor = Vector(event.positionX,event.positionY,event.positionZ)
local cur = Cursor-origin
caster:SetForwardVector(cur)
end
end
end

function juggernaut_ability3:OnAbilityPhaseStart()
caster = self:GetCaster()
origin = self:GetCaster():GetOrigin()
listen = true
return true
end

function juggernaut_ability3:OnSpellStart()
if IsServer() then
listen = false

`||
**My problem is: if there are multiple juggernauts on the server, then only 1 player can set the cursor, while cursor of the others is tied to that spot. **

#

Also it breaks all other abilities on other heroes tied to the same updateCursor event.

hallow garden
#

@rugged holly so there is something I'm noticing that might cause it.

You are declaring all of those variables:

local Cursor;
local pId
local caster;
local origin
local listen = false

Those are now variables that are local to the entire script, since that's the scope you're currently in (it's not in any function, for instance).
That means that whenever you change the value in any of the calls, the value of the variable changes for the entire script, without it being exclusive per ability instance.
This could cause the behavior you're seeing.

Instead of doing those "global locals", use self.param = x or local x = self.param to set or get a value.

To show how it looks in the code, it would be:

juggernaut_ability3 = class({})
LinkLuaModifier( "modifier_juggernaut_ability3", LUA_MODIFIER_MOTION_NONE )
----------------------------------------------
function juggernaut_ability3:Spawn()
    if IsServer() then
        self.pId = self:GetCaster():GetPlayerID()
     CustomGameEventManager:RegisterListener( "updateCursor",Dynamic_Wrap(juggernaut_ability3, "updateCursor"))
              self:SetLevel(1)
              self.caster = self:GetCaster()
              self.origin = self:GetCaster():GetOrigin()
    end
    self.listen = false
end
-----------------------------------------------
function juggernaut_ability3:updateCursor(event)
if self.listen then
if event.PlayerID == self.pId then
self.Cursor = Vector(event.positionX, event.positionY, event.positionZ)
local cur = self.Cursor - self.origin
caster:SetForwardVector(cur)
end
end
end
------------------------------------------------
function juggernaut_ability3:OnAbilityPhaseStart() 
  self.caster = self:GetCaster() -- this is redundant
  self.origin = self:GetCaster():GetOrigin() -- this is redundant
  self.listen = true
    return true
end
----------------------------------------------
function juggernaut_ability3:OnSpellStart()
  if IsServer() then
    self.listen = false
#

and so on

#

to explain further: self refers to the instance of the ability that has triggered the call. Setting a property in self will make it visible throughout the entire instance of the ability. It will not those values on a different instance of the ability.

#

Also, CustomGameEventManager:RegisterListener( "updateCursor",Dynamic_Wrap(juggernaut_ability3, "updateCursor")) can probably be changed to CustomGameEventManager:RegisterListener( "updateCursor", self:updateCursor), though it's been a while since I last coded in lua and I may be getting the syntax wrong. Regardless, Dynamic_Wrap is no longer necessary.

rugged holly
#

Thanks a lot for the reply, actually I tried self originally. Sadly, no custom function has access to self. With this code u wrote I will get error wich I got earlier, something like (upgradeCursor tried to call global self a _nil value)
also, without dynamic wrap any event data from the event will be missing upon arrival and will not be read by the script. I had to use it, beacuse it doesn't work otherwise

#

and not having access to self is also the reason why I have to create a bunch of local variable at the start and then fill them withing Spawn and OnAbilityPhaseStart.

hallow garden
#

self is assigned to what you're calling this in. If you're in a class, self refers to that class instance.

#

if you'll try calling it outside the class, self is indeed globally called

#

Methods declared on the class will work just fine with self.

rugged holly
#

That's what happens

hallow garden
rugged holly
#

Well, thanks for the hint, I found out why it has no access bcoz of that example!

hallow garden
#

what was it?

mystic geode
# rugged holly That's what happens

Last time I had this error I replaced self and did: juggernaut_ability3:GetCaster():GetTeamNumber()and it worked fine. I think it's something related to Dynamic_Wrap.

#

Very weird error indeed.

hallow garden
#

@rugged holly was it because of the dynamic wrap after all then?

rugged holly
#

Yes and no at the same time. It was my first time creating event listener insde of the spell so I just tried to do it same way I did in addon_game_mode, but, obviously, things have to be different here. I didn't quite figure out how to do it with dynamic wrap although I am certain there is a way. I just found a solution that worked and it looks like this:
CustomGameEventManager:RegisterListener( "updateCursor", function(__,event) self:updateCursor(event) end)

#

it also came to my mind, thing I read before, that using : is just calling a function with the first arguement of being self, so I just called a function with first arguement being self and it worked too)