Solved modding the chase camera mode - "self" variable not populated

Discussion in 'Mod Support' started by torsion, Jul 29, 2018.

  1. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    Simply copying "chase.lua" to "chase2.lua" causes a problem with the camera mode. The variable/data-structure "self" seems to be missing a bunch of values in chase2 that are present in chase. Input sanitization keeps the camera mode from crashing, but the difference is immediately noticeable.

    I've attached a packaged mod as a demonstrator. If you have not changed the default game settings and have no other camera mode mods then you can switch to both cameras using the keyboard using the top row of number keys (not the numpad). "6" should switch to "chase" and "9" should switch to "chase2". Otherwise to compare you'll need to go into the game settings and change the camera priority.

    Can anyone help me understand how these values are normally filed and why it's not happening in chase2?

    EDIT: I loosely understand "self" as a thing passed using the colon operator and implied inside the function... https://www.lua.org/pil/16.html

    ... oh, maybe I see the problem. :-( There is a table of named cameras inside camera.lua. I think that somehow that table is not getting extended with the names of additional cameras, and then somehow when we switch to cameras which are not in the table the values are not getting passed/set. I will investigate further.

    EDIT2: I've attached 3 more files showing the difference between what's available to the file named "chase.lua" vs a file named something else. As we can see, something does get passed but some data is missing (right at the top)!

    Interestingly the "otherCameras" section is only populated with chase,common,onboard,orbit,relative.
     

    Attached Files:

    #1 torsion, Jul 29, 2018
    Last edited: Jul 29, 2018
  2. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    Well, I found the problem. Chase, Orbit, etc use defines in the jbeam which they get access to arbitrarily based on matching the name of the definition area in the jbeam and the name of the camera.

    Specifically this function seems to be where the magic happens:
    Code:
      local function initCam(vdata, camConfig, camFile, camName)
        local obj = tableMerge(deepcopy(vdata.jbeamConfig.common), deepcopy(camConfig))
        -- if the jbeamConfig contains a numbered list (of cameras or whatever else), merge them too
        if #camConfig > 0 then
          for k,v in ipairs(camConfig) do obj[k] = v end
        end
        obj.otherCameras = deepcopy(vdata.jbeamConfig)
        local camera = require(cameraFiles[camFile])(obj)
        camera.focused = false -- make sure its set to inactive on start
        vdata.cameras[camName] = camera
        return camera.hidden ~= true
      end
    
    I don't think that this is a wonderful approach, but making the "otherCameras" data available does make it better.

    It seemed like it would be simple enough to make chase2.lua work correctly by making some changes to the init process. Specifically I thought I'd just add a little to the top of the init() function:
    Code:
        if self.otherCameras and self.otherCameras.chase ~= nil then
          print("merging...")
          self = tableMerge(deepcopy(self), deepcopy(self.otherCameras.chase))
        end
    
    And this DOES produce exactly the same contents for "self" that the normal chase camera gets at the end of init.

    Unfortunately somehow as soon as the update() function is called there is a problem. Specifically it trips up as soon as it tries to set "self.camRot.x = 0" and I don't understand why.

    EDIT:
    I got it. Using deepcopy() that way was breaking the references. Instead of
    Code:
    self = tableMerge(deepcopy(self), deepcopy(self.otherCameras.chase))
    
    I should have used
    Code:
    tableMerge(self, deepcopy(self.otherCameras.chase))
    
    In fact, even if I did this it would break the reference (although I don't really understand why in this case):
    Code:
    self = tableMerge(self, deepcopy(self.otherCameras.chase))
    
    So I think I've got myself sorted out now. I'll update again in a bit.

    Yup, that works fine. I've attached an example file "chase2.lua" which works the same as the "chase" camera except it has a different name. It uses the function mentioned above to merge in the chase camera data from the jbeam.
     

    Attached Files:

    #2 torsion, Jul 29, 2018
    Last edited: Jul 29, 2018
    • Like Like x 1
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice