Page 1 of 1

Modifying "player" base entity

Posted: Wed Feb 01, 2017 3:33 pm
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.

Re: Modifying "player" base entity

Posted: Wed Feb 01, 2017 3:49 pm
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))

Re: Modifying "player" base entity

Posted: Wed Feb 01, 2017 5:22 pm
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.

Re: Modifying "player" base entity

Posted: Wed Feb 01, 2017 5:26 pm
by Nexela
Sorry it should be defines.events.on_player_created

Re: Modifying "player" base entity

Posted: Wed Feb 01, 2017 5:35 pm
by BlackParagon
NVM,

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