Calling a function when gamemode finishes loading


Another stupid question sadly, but anyways, I am trying to call a function in my addon after a gamemode starts. I tried using this and it didn’t work. The error was

[ERROR] addons/dye/lua/autorun/server/main.lua:27: attempt to index global 'GM' (a nil value)
  1. unknown - addons/dye/lua/autorun/server/main.lua:27

This is how I was trying to do the function:

function GM:Initialize()
	print("Function stuff")

function getAllMoney(ply,cmd,args)
    //if ply:IsSuperAdmin() ~= true then return end
	PrintMessage(HUD_PRINTCONSOLE,"The amount of money that all players on this server have is: " .. globalMoney)
	PrintMessage(HUD_PRINTCONSOLE,"The previous supply was: " .. prevGlobal)
	PrintMessage(HUD_PRINTCONSOLE,"The inflation rate is: " .. inflationRate)
	PrintMessage(HUD_PRINTCONSOLE,"This will be used for the Dynamic Economy addon.")
    for k,v in pairs(player.GetAll()) do
hook.Add( "Initialize", "get_all_money", getAllMoney )

Not sure if I am doing this wrong or if there is a proper way to do it. Couldn’t find anything on Google or the GMod Wiki.

Appreciate any help, thank you!

I believe it has something to do with calling GM outside of a gamemode’s directory. You can only hook on to the Initialize function as you’ve done below. Someone should confirm this.

If it was like that, how would I go calling the function then after gamemode load? It’s needed to make a variable work

the way gmod loads your gamemode code into memory is by running the gamemode files in the gamemode’s folder (which assigns various code to a table called GM, such as Initialize), and then copies GM to a special location and sets GM to nil

the word GM isn’t special and won’t attach Initialize to the gamemode unless ran during gamemode loading (as a gamemode folder file ran/included by cl_init or init)

the way to add code to utilize GM functions post-load is indeed by using hook.Add; just remove the first three lines and it will work


nevermind fixed

Doesn’t appear you defined the inflationRate variable anywhere, create it outside of your functions.

Ah thank you so much! I thought I had defined it.

[editline]28th December 2016[/editline]

Right now I have it so salary changes based on the inflationRate. Would it be possible to make it so specific shipment prices use the inflationRate for its price? This was just an example I tried doing:

gunPrice = math.floor( 5000 * (1 + inflationRate))

I tried using the gunPrice variable darkrp_customthings/shipments.lua like this:

DarkRP.createShipment("HK USP", {
    model = "models/weapons/w_pist_fokkususp.mdl",
    entity = "m9k_usp",
    price = gunPrice,
    amount = 10,
    separate = true,
    pricesep = 110,
    noship = false,
    shipmodel = "models/items/item_item_crate.mdl",

I end up getting an error like this though:

[DarkRP] A runtime error has occurred in "addons/customdarkrp/lua/darkrp_customthings/shipments.lua" on line 23.
The best help I can give you is this:

Corrupt shipment: HK USP!
The price must be an existing number or (for advanced users) the getPrice field must be a function.

        - No hints, sorry.

The responsibility for the error above lies with (the authors of) one (or more) of these files:
        1. addons/customdarkrp/lua/darkrp_customthings/shipments.lua on line 23
        2. function 'doInclude'
        3. gamemodes/darkrp/gamemode/libraries/modificationloader.lua on line 137
        4. gamemodes/darkrp/gamemode/libraries/modificationloader.lua on line 148
        5. gamemodes/darkrp/gamemode/cl_init.lua on line 53
------- End of Simplerr error -------

  1. error - [C]:-1
   2. error - gamemodes/darkrp/gamemode/libraries/simplerr.lua:530
    3. createShipment - gamemodes/darkrp/gamemode/modules/base/sh_createitems.lua:530
     4. unknown - addons/customdarkrp/lua/darkrp_customthings/shipments.lua:23

I thought that I could access the variable from any file in addons, but I don’t think it’s working. What am I doing wrong? Was told that I could use a variable from my addon in another addon, but I guess it doesn’t work.

If it’s a local variable or defined in a function it will not work. If you want to access variables across addons and files just don’t add local in front of it and keep them outside functions, these are global variables.

The functions and variables are already global. How come I get that error when I am assigning gunPrice to price in the shipments.lua?

Make sure gunPrice is defined somewhere that is shared between the server and client.

What do you mean by this? I’m a bit new to the Lua/GLua language, sorry.

This is branching off much further than your initial problem. I would recommend going through these Lua tutorials before attempting whatever it is you’re trying to do.

Once you understand the differences between client/server/shared then it should become clear.

I’ll look into this tomorrow. Thank you for the assistance! It’s almost 3AM here and I’ve been working on this majority of my day so I am going to go sleep now.
Will reply back if I have any more questions tomorrow

So I’ve been reading up on this, but it’s a bit confusing. Is there a reason as to why the variable should be shared between the client when it’s just a server thing?

Taking a guess here, but it’s probable that the shipments file is loading before the file you defined the global variable in. If that’s the case then you either need to move that variable into the file, or you need to call that function after it has been defined. Considering I don’t know how DarkRP handles these things, I don’t know if doing it outside that file is acceptable.

Nope, I’m guessing he thought that function was running on both states and not just server.

You can define global variables anywhere, even in functions. The only thing that defines a function as local is putting local in-front of it, or it being within a list of function arguments.

[editline]28th December 2016[/editline]

After looking at the DarkRP wiki, it is possible to define a getPrice field that would contain a function that returns the price. I’m guessing you could use that to return the global as it’d be defined at that point.

I don’t know for sure as the wiki makes it seem the price field is required, however the error makes it seem like it’s not, can’t hurt to try.

Yea the error did say something about getPrice. Not sure if I could remove the price field and do that. Also, about the shipments file loading before I defined the global variable, it seems that when I join the server, I get the error about the shipments thing and I am pretty sure that the variable is defined when the server starts because I added that.

Back to the getPrice thing, if I wanted to make it a function, how would it know which number to use? Seems pretty strange.

Are you getting this error on the client or server? If you’re getting it on the client then @KermitTheFrog was right about it needing to be defined on both the server and client.

What do you mean? Just add the field as a function that returns whatever that global is set to.

Yes I am getting the error both server side and client side. So if I make a function that gets the gunPrice, it would work fine?

If you make the code that handles the variable shared as well, then yeah, it should work.

Would I have to include the server file when I make the shared lua file?