Modifying "player" base entity

Place to get help with not working mods / modding interface.
BlackParagon
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 01, 2017 3:23 pm
Contact:

Modifying "player" base entity

Post by BlackParagon »

Hey there. I am fairly new to lua and this games class/object structure, so I am not 100% sure if I am doing something wrong or if this is simply not possible.

I am trying to insert a new variable into the player entity. I need this to store and track a simple integer value, nothing fancy. I could do it outside of player as a global, but having it tied to each player individually would relief me of the need to maintain a table with all players and the value I need.

I figured this should normaly work:

Code: Select all

data.lua 

table.insert(data.raw.player["player"], {experience = 0})

This loads fine into the game, but when I try to print it to my screen with game.player.print(game.player.experience) I get an error telling me this key doesn't exist in game.player .

If I get this wrong or someone has any idea why this isn't working, please tell me. I've already asked google and the search function here for 4 hours now and I still have no clue.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Modifying "player" base entity

Post by Nexela »

You can't modify prototypes like that. It loads just fine because anything not required by the prototype is ignored
you will need to save it in the global variable in control.lua

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
--Create players table if it doesn't exist
global.players = global.players or {} 
--Create a table indexed on player index and store experience into it.
global.players[event.player_index] = {
experience = 0
}
end)
In your other events (if they pass player_index) you would do

Code: Select all

script.on_event(defines.events.the_event_name, function(event)
--make local ref to player table
local playerData = global.players[event.player_index] 
playerData.experience = playerData.experience + 300
end)
to be able to get the experience from the command line you will also need a remote interface

Code: Select all

remote.add_interface("my_mod", {get_xp = function(player) return global.players[player.index].experience end})
in game retrieve it with
/c game.print(remote.call("my_mod", "get_xp", game.player))
Last edited by Nexela on Wed Feb 01, 2017 5:28 pm, edited 1 time in total.
BlackParagon
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 01, 2017 3:23 pm
Contact:

Re: Modifying "player" base entity

Post by BlackParagon »

Thank you for the quick response! That was already really helpful, would have been much easier to just manipulate the prototype directly but it will work out the other way too I guess.

I tried it with your example and get ': attempt to index field 'event' (a nil value)' as an error message when I try to start a new game (SP).

Any idea why? I am utterly confused, on_player_created is obviously called, but why is event nil? It's all I have running for now, there is no other code being executed.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Modifying "player" base entity

Post by Nexela »

Sorry it should be defines.events.on_player_created
BlackParagon
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Feb 01, 2017 3:23 pm
Contact:

Re: Modifying "player" base entity

Post by BlackParagon »

NVM,

saw your post. Yeah, makes sense to pay attention to spelling x.x . I didn't see it either.
Post Reply

Return to “Modding help”