Jump to content

How to register Galaxy callback for a namespaced mod?


Rinart73

Recommended Posts

I'm a bit confused with this new system of namespaces. I'm trying to change vanilla files as less as possible and trying to use namespaces at the same time.

 

I read others code and see that working with Entities or Sector is still similar - just do as usual and it will work:

addScriptOnce("path/to/your/namespaced/mod.lua")

 

However there is no way to attach script to the Galaxy. So I'm seeing 2 variants:

 

1. Just put everything in vanilla server.lua - Obviously bad idea

 

2. My current way, which is not working:

server.lua

package.path = package.path .. ";data/scripts/lib/?.lua"
package.path = package.path .. ";data/scripts/server/?.lua"
require ("factions")
require ("stringutility")

package.path = package.path .. ";mods/mymod/scripts/server/?.lua"
require("MymodServer")

function onStartUp()
    Server():registerCallback("onPlayerLogIn", "onPlayerLogIn")
    Server():registerCallback("onPlayerLogOff", "onPlayerLogOff")
    Galaxy():registerCallback("onPlayerCreated", "onPlayerCreated")
    Galaxy():registerCallback("onFactionCreated", "onFactionCreated")
    
    MymodServer.init()
end

 

MymodServer.lua

package.path = package.path .. ";data/scripts/lib/?.lua"
require ("galaxy")
require ("utility")
require ("faction")
require ("randomext")

-- Don't remove or alter the following comment, it tells the game the namespace this script lives in. If you remove it, the script will break.
-- namespace MymodServer
MymodServer = {}

function MymodServer.init()
    print("Mymod: init")
    Galaxy():registerCallback("onAllianceCreated", "onAllianceCallback")
end

function MymodServer.onAllianceCallback()
    print("Mymod: alliance created")
end

 

As a result I get "Mymod: init" but no "Mymod: alliance created" when I create an alliance.

 

Can someone give me a hint?

 

UPD: I understand that I can just make onAllianceCallback in the server.lua. But why my way doesn't work?

Link to comment
Share on other sites

the best method ive found to having your own server.lua for your mod and changing as little as possible in the original file is to do this:

 

local s, b = pcall(require, 'mods/MoveUI/scripts/server/server')
if s then if b.onPlayerLogIn then local a = onPlayerLogIn; onPlayerLogIn = function(c) a(c); b.onPlayerLogIn(c); end end else print(b); end

 

As you can see this is designed to attach itself to the onPlayerLogin event, but this can be adjusted to any of the other functions.

 

see my mod here: http://www.avorion.net/forum/index.php/topic,3834.0.html

 

This mod uses this example.

Here is what the mods server.lua looks like

 

local MoveUI = {}

function MoveUI.onPlayerLogIn(playerIndex)
  -- Adding script to player when they log in
  local player = Player(playerIndex)
  player:addScriptOnce("mods/MoveUI/scripts/player/MoveUI.lua")
end

return MoveUI

 

as you can see I have my own onPlayerLogin event in my own server.lua file, here i can do ALOT more then just add a script without worrying about messing up the original server.lua

 

not to mention its easy for installation, with instructions saying "Drop these two lines at the bottom of server.lua"

thats it you dont need to worry about explaining where to put a specific line or any of that nonesense.

 

I hope this helps.

 

 

 

Link to comment
Share on other sites

and to help clarify why your method doesnt work, your "Loading" your file into the original server.lua at which point the c engine has already parsed server.lua and did not detect any namespace, since the "Loading" of your mod is done after the fact.

 

The namespaeing isnt true namespacing as other programming languages would have it. Whats happening is the game engine is parsing the files before their even loaded detecting the namespace, and launching the scripts in its own isolated lua engine.

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...