#Object with a physical representation (Part, Model) + custom properties

31 messages · Page 1 of 1 (latest)

ocean fog
#

Greetings,

**Context **: I want to create what I call Citizens, which require a physical representation (a part), but also custom properties (ex : GridPosition, Age, CurrentTask...).

What I tried :

  • Creating a Citizen object, and creating a Model property containing a Part object (obtained from ReplicatedStorage). *Issue *: When setting the parent of the part to workspace (to move the citizen), there is no reference to the citizen object (from the part), which is an issue. Code :
Citizen.__index = Citizen

function Citizen.new()
  local newCitizen = {}
  setmetatable(newCitizen, Citizen)

  newCitizen.Model = game.ReplicatedStorage.Models.Citizen -- a Part Object
  -- custom properties... (newCitizen.Age...)
  
  return newCitizen
end```

- Creating a Citizen object, which is the actual Part object from ReplicatedStorage, and adding custom attributes to it. *Issue *: the Part is an instance and not a table, I can therefore not use setmetatable (or do not know how to obtain a table from an instance). Code :
```local Citizen = {}
Citizen.__index = Citizen

function Citizen.new()
  local newCitizen = game.ReplicatedStorage.Models.Citizen -- Part Object
  setmetatable(newCitizen, Citizen) -- invalid argument : table expected

  -- custom properties
  newCitizen:SetAttribute("GridPosition", Vector2.new(0, 0)
  newCitizen:SetAttribute("Age", 0)
  
  return newCitizen
end```

**Question **: Is there any way to do something like this in a clean way? Is it possible to inherit from a class like Part/BasePart? I basically want to create an object but have a physical representation (Part/Model) linked to it, and other custom properties, while being able to manipulate all of these easily with methods of mine (like Citizen:Move, Citizen:SetTask...).
If you need any other precision please ask me.
#

Object with a physical representation (Part, Model) + custom properties

karmic nymph
#

but you could assign IDs to each citizen

#

make a dictionary of the tables for each citizen

#

just set that ID as a attribute of said citizen

#

u can easily get it by using GetAttribute and can easily reference it in the table by just looking at your dictionary of citizen objects

#

@ocean fog hope it helps, gn

ocean fog
#

I'll try that out, thanks for your time

karmic nymph
#

well i actually think OOP is a model example for this

#

what i meant is keeping some dictionary of all current citizen tables

#

assigning them an ID

#

assigning the citizen model the same id

#

depends on what you expect to use it for

#

if its just a few constant properties you are better off with no tables but if its some functions and whatnot u can just use the OOP way

#

u gotta think about that to build your constructor function properly

ocean fog
#

what I understand you propose is this :

local Citizen = {}
Citizen.__index = Citizen

function Citizen.new()
    local newCitizen = {}
    setmetatable(newCitizen, Citizen)
    
    local ID = 0 -- example, should be unique
    newCitizen.ID = ID
    ReplicatedStorage.Models.Citizen:Clone():SetAttribute("ID", ID)

    -- other properties, custom
    newCitizen.CurrentTask = nil
    newCitizen.Age = 0
    newCitizen.Spawned = false

    return newCitizen
end

Also yes, I need methods to manipulate the object, should it be physically via the part/model or virtually via properties such as, in this situation, the citizen's age, current task...

karmic nymph
#

also no

#

i meant more like this

#

{[0] = {...}, [1] = {...}, ...}

ocean fog
#

and inside for example table[0], the table represents a citizen with a "model" key, and other properties

signal escarp
#

studio** You are now Level 1! **studio

karmic nymph
#

that being said for your intentions i think OOP wouldnt be the best

#

i mean do you plan to have different functions for different citizens

#

thats where OOP comes in handy

ocean fog
#

yes, which could require some sort of inheritance

karmic nymph
#

in that case yeah

ocean fog
#

I'll think about it, thanks for your time