+ Reply to Thread
Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Kreiri

Thread: I hate LUA so much :E

  1. #1
    Plane Touched Verea's Avatar
    Join Date
    Feb 2011
    Location
    Netherlands
    Posts
    200

    Default I hate LUA so much :E

    I just can't get it to work, what I'm trying to do is rather simple. In C, C++ or any other SENSIBLE language that is actually easy to read, I could do this just fine. but in LUA it has to be one hell of a nasty thing.

    What I'm trying to do here, is keep track of a list of players. My code is much like this atm, I've left away some stuff:

    Code:
    -- AddPlayer(id)
    --  Adds a player to the players table
    function Timelapse.AddPlayer(id)
    	-- Loop through the entire thing
    	for i = 1, table.getn(Players), 1 do
    		if Players[i] ~= nil then
    			-- Check if the player exists, or this is inhabited by a player
    			if Players[i].playerId ~= 0 then
    				print("Existing player: " .. Players[i].playerId)
    			else if Players[i].playerId == id then
    				return false
    			end
    		end
    	end
    	
    	-- Pretty sure this player doesn't exist now.
    	indice = table.getn(Players) + 1
    	Players[indice].playerId = id
    end end
    Not even sure why I need the double end near the last line of code, I don't see what it belongs to, but whatever the case, it's not working, it's driving me nuts. Please help me.

    ps: I am expecting to see the id added to the list, but obviously, nothing happens.
    Verae, level 60 Cleric @ Blightweald, Guild Master of Tea Club
    I want to fly like an eagle, to the sea.
    I want to fly like an eagle, let my spirit carry me.

  2. #2
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    grr double post
    Last edited by Adelea; 02-21-2012 at 12:14 PM.
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  3. #3
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    Quote Originally Posted by Verea View Post
    I just can't get it to work, what I'm trying to do is rather simple. In C, C++ or any other SENSIBLE language that is actually easy to read, I could do this just fine. but in LUA it has to be one hell of a nasty thing.

    What I'm trying to do here, is keep track of a list of players. My code is much like this atm, I've left away some stuff:

    Code:
    -- AddPlayer(id)
    --  Adds a player to the players table
    function Timelapse.AddPlayer(id)
    	-- Loop through the entire thing
    	for i = 1, table.getn(Players), 1 do
    		if Players[i] ~= nil then
    			-- Check if the player exists, or this is inhabited by a player
    			if Players[i].playerId ~= 0 then
    				print("Existing player: " .. Players[i].playerId)
    			else if Players[i].playerId == id then
    				return false
    			end
    		end
    	end
    	
    	-- Pretty sure this player doesn't exist now.
    	indice = table.getn(Players) + 1
    	Players[indice].playerId = id
    end end
    Not even sure why I need the double end near the last line of code, I don't see what it belongs to, but whatever the case, it's not working, it's driving me nuts. Please help me.

    ps: I am expecting to see the id added to the list, but obviously, nothing happens.
    Code:
    else if Players[i].playerId == id then
    You need an end to match this if. Try using elseif if that is what you really mean.

    Your code is doing this

    Code:
    -- AddPlayer(id)
    --  Adds a player to the players table
    function Timelapse.AddPlayer(id)
    	-- Loop through the entire thing
    	for i = 1, table.getn(Players), 1 do
    		if Players[i] ~= nil then
    			-- Check if the player exists, or this is inhabited by a player
    			if Players[i].playerId ~= 0 then
    				print("Existing player: " .. Players[i].playerId)
    			else
    				if Players[i].playerId == id then
    					return false
    				end
    			end
    		
    			-- Pretty sure this player doesn't exist now.
    			indice = table.getn(Players) + 1
    			Players[indice].playerId = id
    	end
    end
    But using elseif instead, you get

    Code:
    -- AddPlayer(id)
    --  Adds a player to the players table
    function Timelapse.AddPlayer(id)
    	-- Loop through the entire thing
    	for i = 1, table.getn(Players), 1 do
    		if Players[i] ~= nil then
    			-- Check if the player exists, or this is inhabited by a player
    			if Players[i].playerId ~= 0 then
    				print("Existing player: " .. Players[i].playerId)
    			elseif Players[i].playerId == id then
    				return false
    			end
    		end
    	end
    	
    	-- Pretty sure this player doesn't exist now.
    	indice = table.getn(Players) + 1
    	Players[indice].playerId = id
    end
    So the last two lines execute in a different place.
    Last edited by Adelea; 02-21-2012 at 12:16 PM.
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  4. #4
    Rift Disciple
    Join Date
    Feb 2011
    Posts
    129

    Default

    You don't have an extra end, you just have your indentation wrong:

    Code:
    -- AddPlayer(id)
    --  Adds a player to the players table
    function Timelapse.AddPlayer(id)
    	-- Loop through the entire thing
    	for i = 1, table.getn(Players), 1 do
    		if Players[i] ~= nil then
    			-- Check if the player exists, or this is inhabited by a player
    			if Players[i].playerId ~= 0 then
    				print("Existing player: " .. Players[i].playerId)
    			else 
    				if Players[i].playerId == id then
    					return false
    				end
    			end
    		end
    	
    	-- Pretty sure this player doesn't exist now.
    	indice = table.getn(Players) + 1
    	Players[indice].playerId = id
    	end 
    end
    elseif and else if are two different things.

    For the problem itself, "return false" ends the entire function when it finds the player ID. If you just want to end the loop use "break".

  5. #5
    Plane Touched Verea's Avatar
    Join Date
    Feb 2011
    Location
    Netherlands
    Posts
    200

    Default

    For the problem itself, "return false" ends the entire function when it finds the player ID. If you just want to end the loop use "break".
    That's intended.

    This just really proves this language is nonsensical. What's the point in using end anyway? This is much cleaner:

    Code:
    if (player[i].playerId == 1) {
    
    } else if (player[i].playerId ~= nil) {
    
    }
    Thanks for the help. But maybe you can answer a different question for me, too. Basically, I'm trying to get the names of all people in the raid (if I am in a raid). How would I go around doing that then? Basically, I want to run over each member of the raid, and store their names in an array.
    Last edited by Verea; 02-21-2012 at 12:29 PM.
    Verae, level 60 Cleric @ Blightweald, Guild Master of Tea Club
    I want to fly like an eagle, to the sea.
    I want to fly like an eagle, let my spirit carry me.

  6. #6
    Rift Disciple
    Join Date
    Feb 2011
    Posts
    129

    Default

    Quote Originally Posted by Verea View Post
    This just really proves this language is nonsensical. What's the point in using end anyway? This is much cleaner:

    Code:
    if (player[i].playerId == 1) {
    
    } else if (player[i].playerId ~= nil) {
    
    }
    Both are really nonsensical when you could just use line breaks and indentation which majority of programers already use. But the use of end uses the statements themselves as the opening bracket.

    Thanks for the help. But maybe you can answer a different question for me, too. Basically, I'm trying to get the names of all people in the raid (if I am in a raid). How would I go around doing that then? Basically, I want to run over each member of the raid, and store their names in an array.
    group01 through group20 are the specifiers for group/raid members, then grab their info with Inspect.Unit.Detail().
    Last edited by Xioden; 02-21-2012 at 12:44 PM.

  7. #7
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    Code:
    local allplayers = {}
    local d = Inspect.Unit.Detail("player")
    allplayers[d.name] = true
    
    for x = 1, 20 do
      local s = string.format("group%02d", x)
      d = Inspect.Unit.Detail(s)
      if d then
        allplayers[d.name] = true
      end
    end
    
    dump(allplayers)
    group01-group20 only seems to work if you are grouped, which is why the detail("player") is in there.
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  8. #8
    Plane Walker Kreiri's Avatar
    Join Date
    Feb 2011
    Posts
    402

    Default

    "Baaaaw, Lua is just a dumbed down C, why doesn't it obey me?"

    1. Use for loop with pairs or ipairs. Example:

    Code:
    for key, value in pairs(mytable) do
         print(key)
         myFunc(value)
    end
    2. Your loop logic is flawed.
    Code:
    if Players[i].playerId ~= 0 then
    	print("Existing player: " .. Players[i].playerId)
    else
            if Players[i].playerId == id then
    	      return false
           end
    end
    It will return false only if both .playerId and id equal 0. Is it really what you want?

    3.
    Code:
    indice = table.getn(Players) + 1
    Players[indice].playerId = id
    Tables in Lua do not work this way.
    a) table.getn is deprecated in Lua 5.1. Use #() to get the size of a table with numeric keys without gaps.

    b) You will get an "attempt to index nil value" error. If #(aTable) is S, then aTable[S+1] is nil.
    Feminism is the radical notion that women are people.

  9. #9
    Plane Walker Kreiri's Avatar
    Join Date
    Feb 2011
    Posts
    402

    Default

    Assuming that your Players table is an array:

    Code:
    function TimeLapse.AddPlayer(id)
        for _, player in pairs(Players) do
    		if player.playerId == id then return false end
       end
       
       local newPlayer = CreateNewPlayer(id)
       table.insert (Players, newPlayer)
    end
    As for me, I'd rather make players table a dictionary (and add a meaningful return value):
    Code:
    function TimeLapse.AddPlayer(id)
    	if Players[id] then
    	   	return false
    	else
    		Players[id] = CreateNewPlayer(id)
    		return Players[id]
    	end
    end
    Feminism is the radical notion that women are people.

  10. #10
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    Quote Originally Posted by Verea View Post
    This just really proves this language is nonsensical. What's the point in using end anyway?
    It's really not nonsensical.

    This is much cleaner:

    Code:
    if (player[i].playerId == 1) {
    
    } else if (player[i].playerId ~= nil) {
    
    }
    Actually, it's not. Look, I like C. (I was on the standards committee for C9X for about a decade.) I like C a lot. But the way C handles if/else-if is ugly; it just looks clean because we tend to ignore what it really does.

    You see:
    Code:
    if (x) {
    } else if (y) {
    }
    But what's really happening is:

    Code:
    if (x) {
    } else {
      if (y) {
      }
    }
    It's just that C allows you to omit the block markers for a single statement (even a compound one).

    Thanks for the help. But maybe you can answer a different question for me, too. Basically, I'm trying to get the names of all people in the raid (if I am in a raid). How would I go around doing that then? Basically, I want to run over each member of the raid, and store their names in an array.
    Okay, here's my thought: Your problem is that you're trying to use Lua as though it's a C-like language. It's not. So you're trying to force C idioms onto Lua, and that's not matching at all well onto the native style of Lua.

    If you write C in Lua, it'll be like writing Fortran in C++. You can do it, but it'll be ugly and you'll be fighting the language every step of the way. Spend a little time getting comfortable with the natural idioms of Lua, and you'll find it a lot less frustrating.

    Couple of things to think about:
    • Get used to the idea that 0 is true in Lua. If you want a "not-present" value, use nil.
    • Don't iterate on indices, iterate on pairs. "for idx, value in ipairs(table)" is your friend.
    • Learn to love non-integer table keys.

    This has gotten me thinking, and I may write up a more serious attempt to explain Lua for C programmers. They are VERY different languages. Honestly, if I were doing something where I didn't need pointers and fancy memory tricks, I would tend to prefer Lua (or Ruby) to C for most writing, despite C being by far my most fluent programming language.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  11. #11
    Rift Disciple
    Join Date
    Apr 2011
    Posts
    120

    Default

    learn Scheme first. then Lua will seem totally sensible. this is not a dig against Scheme, by the way.

    in many ways, Lua is a much more powerful language than C. this despite the fact that it has a simpler syntax.

    -ken
    Snowreap Yellowtail Preyz Taralin
    < The Purge > Guardian Seastone

  12. #12
    Ascendant Credo's Avatar
    Join Date
    Jan 2011
    Posts
    4,172

    Default

    As long as it isn't Java I'm happy
    Even free cost too much now

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts