Is this change possible with our level of access?

Soul_RiderSoul_Rider Mod Bean Join Date: 2004-06-19 Member: 29388Members, Constellation, Squad Five Blue
I am looking to edit the base component structure. I would like to create code that will implicitly allow a get(variable) and set("variable", value) command on any variable created in a component. I want to do this to save the time of writing a get and set function multiple times in every component and script I write :)

Is it possible to do this in lua, or would/should this be an engine level change?

I haven't seen any exposed code relating to components directly, so assume this would need to be done at an engine level. If so, would it be possible to make such a change?

As an example, a team_number component has the following code:
--- number (0,8)
local teamNumber = 0

local function SetTeamNumber(setTeamNumber)
    teamNumber = setTeamNumber
end

function OnNewTeamNumber(message)
if message.teamnumber >= 0 and <= 8
    SetTeamNumber(message.teamnumber)
end

function GetTeamNumber()
    return teamNumber
end

With an implicit get() and set() on every member variable the code for this component becomes simply:
--- number (0,8)
local teamNumber = 0

function OnNewTeamNumber(message)
if message.teamnumber >= 0 and <= 8
    set("teamNumber", message.teamnumber)
end

This would be a great help to me and save a valuable amount of time if it could be implemented, or if I could be pointed to somewhere where I can implement it, either would be good :)

Comments

  • MaxMax Technical Director, Unknown Worlds Entertainment Join Date: 2002-03-15 Member: 318Super Administrators, Retired Developer, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, Subnautica Developer, Pistachionauts, Future Perfect Developer
    My recommendation would be to just omit the getters and setters:
    local teamNumber = 0
    
    function OnNewTeamNumber(message)
        if message.teamnumber >= 0 and <= 8 then
            teamNumber = message.teamnumber
        end
    end
    

    The main reason to use getters/setters is to create an abstraction between the implementation and the code that's using the implementation. This allows the the implementation to change without affecting the other code. In our scripting model, that abstraction already exists in the form of the message handler.

    You may want the same abstraction within a script. For example, if you set the team number from a bunch of different locations within the script. In this particular case you might also want to do the validation that the team number is between 0 and 8 every time you set it, so it makes sense to create a SetTeamNumber function to handle that. This can be done on a case-by-case basis though. And in this particular case, there's no real reason to use a getter.

    While I definitely wouldn't recommend this, as a point of curiosity, the answer to your original question is that it could be done something like this:
    varTeamNumber = 0
    
    function CreateGettersAndSetters()
    
        local vars = { }
        for k,v in pairs(_G) do
            local name = string.match(k, "var(%a+)")
            if name then
                table.insert(vars, name)
            end
        end
    
        for i=1,#vars do
            local name = vars[i]
            _G["Set" .. name ] = function (value) _G["var" .. name] = value end
            _G["Get" .. name ] = function () return _G["var" .. name] end
        end
    
    end
    
    CreateGettersAndSetters()
    
  • Soul_RiderSoul_Rider Mod Bean Join Date: 2004-06-19 Member: 29388Members, Constellation, Squad Five Blue
    Thanks for the advice :)

    I am still trying to see why messaging works better than calling get or set methods? The component is still separate, and you are still needing to update/retrieve the information. As a relative low-end programmer, I can't see how they differ, except messages seem to throw out data for anyone to read.


  • ezekelezekel Join Date: 2012-11-29 Member: 173589Members, NS2 Map Tester
    edited December 2014
    Not that this post is relevant, but I just finished my programming one course in java and see some similar lines of code, of course I'm not really sure what's going on with it.. but I hope by the end of two years I can come back to this thread and understand all of it even though it's a different language! :)

    I do see a for loop, or at least I think I do
  • bERt0rbERt0r Join Date: 2005-03-23 Member: 46181Members
    If you are just learning java you should not try to pick up lua simultanously. When learning to program you should stick to one language. After you have a decent grasp on what you are doing, learning other languages becomes much easier.

    What max tried to explain for example was that ther is no need for setters and getters in lua as in java. I'm not any good in lua but there are things called tables and they perform pretty much how soul_rider wants to interact with variables.
  • Soul_RiderSoul_Rider Mod Bean Join Date: 2004-06-19 Member: 29388Members, Constellation, Squad Five Blue
    @ezekel‌ - Programming is programming. If you understand the logic of code, then you can better read code, no matter what language. While not knowing the syntax does cause some hindrance, an understanding of code in general enables you to see the patterns and recognise the logic that is held within.

    Essentially, the logics of writing a program don't change, just the words used to do them.

    Lua is very simple to understand because the the keywords are quite obvious - If, for, while, do, and, true, false, etc.

    The way my brain works personally, learning one language, but looking at and trying to interpret another, always helps me further my code understanding.

    I am still a fairly low end programmer, but I can read a lot of code that I couldn't yet even dream of writing, and can read code reasonably well in multiple languages. Some of those languages I wouldn't even attempt to code in currently though.
  • bERt0rbERt0r Join Date: 2005-03-23 Member: 46181Members
    edited December 2014
    @Soul_Rider it seems like you enjoy to contradict me and that's fine. It is great if you were able to pick up multiple languages at the same time but I doubt this skill is prevailent in the general public. In fact there are about 10% of people who inherently understand how to program, another 80% who will understand how to program after a certain amount of studying and about 10% who will never ever understand code.
    These 10% are in no way more or less intelligent than the others, their brains are just wired that way. For example people who have 0 affinity for programming can be geniouses in biology or chemistry.

    The important thing when learning to code is to understand the general concept. Its not just syntax, languages consist of syntax, grammar and semantics. The fact that you can read lots of code but not write it may mean that you dont understand the code. I can also read a spanish newspaper and have no idea what it's about.

    Especially in Java, there are things like object orientation which are inherent to get a grasp of the language LUA handles this very differently (apparently through the abovementioned tables) and mixing these two concepts up will not do you very much good.
  • Soul_RiderSoul_Rider Mod Bean Join Date: 2004-06-19 Member: 29388Members, Constellation, Squad Five Blue
    Contradict your opinion? HAHAHA, you have much too big of an opinion of yourself. I have Aspergers, meaning my brain works differently from 'Normal' people, which is why I explained the way I learned, as you never know someone's situation. It is whatever works for them. Rather than being a right and wrong way, which you seem to be insinuating.

    And your comment is the most ridiculous thing I have ever heard.

    If you do not understand the spanish, you are not reading it. Reading requires Comprehension....

    Or can I just flick through the pages of a Hebrew book for example, without understanding any of it and claim to have read it, which is what you are claiming.

    Look, I don't know why you think my comments are anything to do with you. The last one was not, this one is a direct reply to you as you mentioned me. But I have already given up having a conversation with you, as you are a person who is always right, as you showed in the other thread.

    Personally, I don't want to spend my time having a discussion with someone who is always right, because I don't learn anything, and nor do they..

    So if you understand, my replies before and after you in a topic are not aimed at you unless I specifically name you, you can forget I exist and stop thinking about me. I was doing a great job of ignoring you on the forums until you mentioned me above. You really are not that important in my life, and I really generally couldn't care about what you post.

    If you'll do me the same courtesy and just leave me alone, unless you genuinely have something useful to contribute, that would be great.
  • bERt0rbERt0r Join Date: 2005-03-23 Member: 46181Members
    Perhaps I enjoy pointing it out when people are wrong a bit too much, but I try to only talk about facts when I'm pretty sure what I'm talking about is proven.
    Your answer of "hey I can learn multiple languages at once" seemed to me like a mockery to my advice for ezekel. I don't know if you can program and I don't know how you may have learned it but I know that it is a really tough piece of work for some people. I frequent a programming board from time to time and there are tons of beginners asking questions because the learn the language in the wrong way.

    When you say reading requires comprehension, I say take a technical text in a field you have no idea what it is about (try reading the latest bills of your government). You will be able to perfectly read it out loud, understand each and every word but you wont know what it is about, at least until you read it a few times - that's the semantics. When you said you can read any code but you are not able to write that code you read I assumed this is a similar situation.

    Finally, if you dont want to spend time with my posts, don't reply to them. I did not attack you in any form, there is no reason to be so defensive.
Sign In or Register to comment.