Sorata_Kanda

OOP vs element data

Recommended Posts

59 minutes ago, IIYAMA said:

Yes,

 

elementData should be considered as something similar to attributes.


<race title="example" />

 

It defines the specifications of elements.

 

 

Before you continue, rendering based on elements is not the method with the most performance. So keep that in mind.

But! The benefits:

  • There is less (validation- and maintenance)code required. If an element is deleted, then you do not have to clean it from a table.
  • 100% control serverside.
  • Client elements can be set as children of server elements. So these can be auto cleaned as well.
  • The tables from the queries are formatted as arrays, so looping through them doesn't require much time.

 

 

 

The rendering happens only when you're near the Race elements. So that shouldn't be a problem?

But I still need to have a reference to the object when looping through Race elements. And as far as I found out, they don't update either. :/

 

Share this post


Link to post

I'm more than sure that on(Client)ElementDataChange is async.

  • Like 1

Share this post


Link to post
11 hours ago, Sorata_Kanda said:

The rendering happens only when you're near the Race elements. So that shouldn't be a problem?

But I still need to have a reference to the object when looping through Race elements. And as far as I found out, they don't update either. :/

 

Keep in mind that a table should not be a part of a race element. Because tables become a new object when they are saved inside of elementData.

But a race element can be a part of a table. There is a function called isElement after all.

Another way to reference from a raceElement to a table is by using another table. They should be both in the same environment.

reference = {}

table = {}
element = [[...]]

reference[element] = table

 

 

5 hours ago, Pirulax said:

I'm more than sure that on(Client)ElementDataChange is async.

 

The event system is indeed async.

 

 

 

 

  • Like 1

Share this post


Link to post
16 minutes ago, IIYAMA said:

Keep in mind that a table should not be a part of a race element. Because tables become a new object when they are saved inside of elementData.

But a race element can be a part of a table. There is a function called isElement after all.

Another way to reference from a raceElement to a table is by using another table. They should be both in the same environment.


reference = {}

table = {}
element = [[...]]

reference[element] = table

 

 

 

The event system is indeed async.

 

 

 

 

So as I thought, I won't be coming around with storing those objects in a table.

I probably have to fill those reference tables at resource start + with event or something.

(Aside: Imagine having those objects "stored" in MySQL. I sort of try to avoid double queries for those Race elements as I think it's not necessary)

Edited by Sorata_Kanda

Share this post


Link to post
1 hour ago, Sorata_Kanda said:

So as I thought, I won't be coming around with storing those objects in a table.

I probably have to fill those reference tables at resource start + with event or something.

(Aside: Imagine having those objects "stored" in MySQL. I sort of try to avoid double queries for those Race elements as I think it's not necessary)

So I sort of managed it like this

-- Server
races = {}

addEventHandler('onResourceStart', resourceRoot, function()
    -- Query all races from DB
    for _, race in ipairs(queryResult) do
      local raceObject = Race.new(race)
      races[raceObject:getElement()] = raceObject
    end
    
    addEventHandler('onPlayerJoin', resourceRoot, function()
        triggerClientEvent(source, 'raceUpdateClientTable', this, races)
    end)
end)

-- Client
races = {}

addEvent('raceUpdateClientTable', true)
addEventHandler('raceUpdateClientTable', resourceRoot, function(raceTbl)
    for _, race in pairs(raceTbl) do
      attachRaceClasses(race) -- See @IIYAMA's example
    end
    races = raceTbl
end)

Any suggestions for improvements?

Share this post


Link to post

@Sorata_Kanda

The event 'onPlayerJoin' triggers faster than a client has loaded his client resources.

 

onClientResourceStart > triggerServerEvent > races > triggerClientEvent

This also works for clients that are already in the server.

 

Edited by IIYAMA

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.