meta data for this page
InitVanillaEntityType(class, entityTypeName)
Inits a new entity type. Always use this function in vanilla game code (less overhead when synchronizing the game). Never call this function from a mod.
31 function InitVanillaEntityType(class, entityTypeName) 32 if g_isHotReload then 33 if EntityIds.entityTypesByName[entityTypeName] then 34 return; 35 end; 36 -- otherwise we are registering a new entity type, which is okay during runtime (as long as the id has been added at the very bottom of the list) 37 else 38 assert(g_scenario == nil, "Cannot add a new entity type while the game is running."); 39 end; 40 41 local id = assert(VanillaEntityTypes[entityTypeName], "Invalid vanilla entity type: '" .. tostring(entityTypeName) .. "'"); 42 43 -- make sure that the entity type id is not yet used 44 assert(EntityIds.entityTypesByName[entityTypeName] == nil, string.format("Entity type '%s' is already in use", entityTypeName)); 45 46 -- assign them 47 EntityIds.vanillaEntityTypes[id] = class; 48 --EntityIds.entityTypesByName[entityTypeName] = class; 49 50 51 if not g_isHotReload or class.entityTypeId == nil then 52 EntityIds:assignEntityTypeId(class, id); 53 end; 54 end; 55
InitModEntityType(class, name)
Inits a new entity type (called by a mod). Exclusively use this function for any non-vanilla content (DLCs, mods, etc.).
59 function InitModEntityType(class, name) 60 assert(g_scenario == nil, "Cannot add a new entity type while the game is running."); 61 62 -- just add it as entity type by name, will be registered when starting the game 63 EntityIds.entityTypesByName[name] = class; 64 end; 65
networkGetEntityId(entity)
Returns the entity id of the entity.
68 function networkGetEntityId(entity) 69 assert(type(entity) == "table", "argument 1 must be a table"); 70 return entity.entityId; 71 end; 72
streamWriteEntityId(entity)
Writes the entity id of the given entity to the stream.
76 function streamWriteEntityId(entity) 77 assert(type(entity) == "table" and entity.entityId ~= nil, "Invalid entity specified"); 78 streamWriteUInt16(entity.entityId); 79 end; 80
streamReadEntityId()
Fetches the correct entity back from the stream.
84 function streamReadEntityId() 85 local entityId = streamReadUInt16(); 86 87 return g_networkGame:getEntity(entityId); 88 end; 89
EntityIds:getTypeId(entity)
Returns the id of the entity's type.
93 function EntityIds:getTypeId(entity) 94 assert(type(entity) == "table", "argument 1 must be a table"); 95 return entity.entityTypeId; 96 end; 97
EntityIds:getTypeById(id)
Returns the entity that has entity type id.
100 function EntityIds:getTypeById(id) 101 -- get the according entity type 102 local entityType = EntityIds.entityTypes[id or 0]; 103 104 assert(entityType ~= nil, "Entity type " .. tostring(entityType) .. " does not exist on this client."); 105 return entityType; 106 end; 107
EntityIds:assignAllEntityTypeIds()
Assigns valid entity type ids to all mod entity types. Vanilla entity types will not be changed.
110 function EntityIds:assignAllEntityTypeIds() 111 -- assign event ids upon the start of the game 112 -- must be called on the server before the game starts! 113 for name, class in pairs(self.entityTypesByName) do 114 self:assignEntityTypeId(class, EntityIds.lastEntityTypeId + 1); 115 end; 116 end; 117
EntityIds:assignEntityTypeId(class, id)
Assigns the entity type id id to the specified class.
121 function EntityIds:assignEntityTypeId(class, id) 122 assert(id ~= nil, "Cannot add entity type with entity id 'nil' (second argument must be an integer)"); 123 assert(EntityIds.entityTypes[id] == nil, string.format("Entity id %d is already in use", id)); 124 125 -- get type id of that class (and not of a parent class) => rawget 126 if rawget(class, "entityTypeId") == nil then 127 -- we need to register this one 128 EntityIds.entityTypes[id] = class; 129 EntityIds.lastEntityTypeId = math.max(EntityIds.lastEntityTypeId, id); 130 131 -- assign entity type id to that class 132 class.entityTypeId = id; 133 end; 134 135 return id; 136 end; 137
EntityIds:revertAssigning()
Reverts all assigned ids. Call this upon closing the game.
141 function EntityIds:revertAssigning() 142 -- reset any assigned entity types upon closing the game 143 -- revert all assigned ids 144 for className, class in pairs(self.entityTypesByName) do 145 if class.entityTypeId ~= nil and self.vanillaEntityTypes[class.entityTypeId] == nil then 146 -- unregister the entity type id of that class 147 rawset(class, "entityTypeId", nil); 148 end; 149 end; 150 151 -- remove the old list of assigned ids 152 self.entityTypes = TableUtil.deepCopy(EntityIds.vanillaEntityTypes); 153 154 -- also determine the highest entity id in that table 155 local highestId = 0; 156 157 for id, class in pairs(self.entityTypes) do 158 highestId = math.max(highestId, id); 159 end; 160 161 self.lastEntityTypeId = highestId; 162 end; 163
EntityIds:writeResyncEntityIds()
Writes all entity ids to the other player.
167 function EntityIds:writeResyncEntityIds() 168 -- synchronize all dynamic entity ids 169 -- note that EntityIds:assignAllEntityTypeIds() must be called beforehand! 170 171 local entityTypeId; 172 173 -- get the number of entity types beforehand 174 local entityTypeCount = count(self.entityTypesByName); 175 streamWriteUInt16(entityTypeCount); 176 177 for name, class in pairs(self.entityTypesByName) do 178 entityTypeId = rawget(class, "entityTypeId"); 179 streamWriteString(name); 180 streamWriteUInt16(entityTypeId); 181 end; 182 end; 183 184
EntityIds:readResyncEntityIds()
Counterpart of above function, reads what EntityIds:writeResyncEntityIds() has written to the stream.
188 function EntityIds:readResyncEntityIds() 189 local entityTypeCount = streamReadUInt16(); 190 191 local className, classId; 192 for i=1, entityTypeCount, 1 do 193 className = streamReadString(); 194 classId = streamReadUInt16(); 195 196 -- register these two 197 local class = self.entityTypesByName[className]; 198 199 if class == nil then 200 print("Error while synchronizing entity type ids: Entity type '" .. tostring(className) .. "' exists on the server, but not on this client"); 201 else 202 self:assignEntityTypeId(class, classId); 203 204 if g_debugNetworking then 205 print("Entity Type '" .. tostring(className) .. "' has id " .. classId); 206 end; 207 end; 208 end; 209 end; 210 211 local lastId = 0; 212 local nextId = function() 213 lastId = lastId + 1; 214 return lastId; 215 end; 216 217 VanillaEntityTypes = {}; 218 219 VanillaEntityTypes.Vehicle = nextId(); 220 VanillaEntityTypes.Player = nextId(); 221 VanillaEntityTypes.RWRopeway = nextId();
Copyright
All contents of this page may be used for modding use for Winter Resort Simulator - Season 2 only. Any use exceeding this regulation is not permitted.
Copyright (C) HR Innoways, 2021. All Rights Reserved.