Forum

> > CS2D > Scripts > table and menu detect system
Forums overviewCS2D overview Scripts overviewLog in to reply

English table and menu detect system

3 replies
To the start Previous 1 Next To the start

old table and menu detect system

muslim
User Off Offline

Quote
I was working on a Zombie Plague script when I encountered a problem I couldn't solve.

I created a table to store zombie classes, their names, health, speed, etc. In the menu, you get the zombie classes based on their names in the table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
zombieClassList = {
	[1] = {
		name = "Classic Zombie",
		health = 250, -- normal health
		speed = 0, -- normal speed
		armor = 0, -- no armor
		weapon = {78}, -- claw
		knockback = 7, -- normal knockback
		fastBuilding = 0, -- change to 1 to make building faster
		description = "Basic zombie with normal health and speed"
	},
	[2] = {
		name = "Fat Zombie",
		health = 800, -- very high health
		speed = -12, -- very low speed
		armor = 202, -- medium armor
		weapon = {78},
		knockback = 4, -- low knockback
		fastBuilding = 0,
		description = "Fat zombie with extremely high health and low knockback"
	},
	[3] = {
		name = "Runner Zombie",
		health = 100, -- very low health
		speed = 4, -- very high speed
		armor = 0,
		weapon = {78},
		knockback = 10, -- high knockback
		fastBuilding = 0,
		description = "Fast-moving zombie with low health but high speed"
	},
	[4] = {
		name = "Brute Zombie",
		health = 400, -- high health
		speed = -2, -- low speed
		armor = 0,
		weapon = {69}, -- machete
		knockback = 7,
		fastBuilding = 0,
		description = "Tough zombie with a deadly machete"
	},
	[5] = {
		name = "Shield Zombie",
		health = 100,
		speed = 0,
		armor = 0,
		weapon = {41}, -- shield
		knockback = 7,
		fastBuilding = 0,
		description = "Zombie equipped with a shield for added protection"
	},
	[6] = {
		name = "Glock Zombie",
		health = 250, 
		speed = -4,
		armor = 0,
		weapon = {2}, -- glock pistol
		knockback = 7,
		fastBuilding = 0,
		description = "Armed zombie with a pistol for ranged attacks"
	},
	[7] = {
		name = "Builder Zombie",
		health = 100, -- very low health
		speed = 0,
		armor = 0,
		weapon = {74}, -- wrench
		knockback = 7,
		fastBuilding = 0,
		description = "Zombie that constructs defense using a wrench"
	},
	[8] = {
		name = "Chainsaw Zombie",
		health = 250,
		speed = 0,
		armor = 0,
		weapon = {85}, -- chainsaw
		knockback = 7,
		fastBuilding = 0,
		description = "Basic zombie with normal health and speed"
	}
}

addhook("serveraction","_serveraction")
function _serveraction(id,action)
	if action == 1 then
		menu(id,"Main Menu,Choose Class|(Humans),Choose Class|(Zombies)")
	end
end

addhook("menu","_menu")
function _menu(id,title,button)
	if title == "Main Menu" then
		if button == 2 then
			local zombieMenuOptions = "Zombies Classes"
			for i, zombieClass in ipairs(zombieClassList) do
				zombieMenuOptions = zombieMenuOptions .. "," .. zombieClass.name
			end
			menu(id, zombieMenuOptions)
		end
	end

	if title == "Zombies Classes" then
		_zombiesMenu(id,title,button)
	end
end

function _zombiesMenu(id,title,button)
	if button > 0 then
		local class = zombieClassList[button]
		if fixZombieClassID[id] == button then
			errorMessage[id] = "You are already "..class.name.."!"
			_errorMessage(id)
		else
			fixZombieClassID[id] = button
			serverMessage[id] = "Your new class is "..class.name.."."
			_serverMessage(id)
		end
	end
end
However, I couldn't figure out how to implement a system that would automatically detect if the table exceeded 8 classes. I wanted it to display a 'Next Page' button that would take us to the following page, where we can find the remaining classes from the table.

Note that the table only contain 8 classes, I didn't add more because I couldn't figure out how to do that.

If anyone could provide an example of how this works, it would help me understand the concept better. Thanks.

old Re: table and menu detect system

muslim
User Off Offline

Quote
I figured out how to do it, I didn't get any help from any of those files you sent, to be honest. They are so complicated.

I spent hours going through every single step, and if anything went wrong, I had to redo the entire task because I'm stupid. It was tough time, but it was worth it.

Here's a sample script with comments for any newbie like me. I made it easier, you can thank me later...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
weapons = {
  {id = 32, name = "M4A1"},
  {id = 30, name = "AK47"},
  {id = 35, name = "AWP"},
  {id = 38, name = "Galil"},
  {id = 20, name = "MP5"},
  {id = 24, name = "UMP45"},
  {id = 22, name = "P90"},
  {id = 34, name = "Scout"},
  {id = 31, name = "SG552"},
  {id = 39, name = "Famas"},
  {id = 36, name = "G3SG1"},
  {id = 37, name = "SG550"},
  {id = 33, name = "Aug"},
  {id = 40, name = "M249"},
  {id = 11, name = "XM1014"},
  {id = 90, name = "M134"},
  {id = 45, name = "Laser"},
  {id = 46, name = "Flamethrower"},
  {id = 47, name = "RPG Launcher"},
  {id = 85, name = "Chainsaw"},
  {id = 69, name = "Machete"}
}

menuItemsPerPage = 8  -- Number of menu items per page
currentPage = 1       -- Current page number

addhook("serveraction","_serveraction")
function _serveraction(id,action)
  if action == 1 then -- If the player presses F2
    currentPage = 1 -- Reset current page to 1

    -- Calculate start and end index for current page
    local startIndex = (currentPage - 1) * menuItemsPerPage + 1
    local endIndex = math.min(currentPage * menuItemsPerPage, #weapons)

    -- Create menu options for current page
    local menuOptions = {"Weapons Menu [Page "..currentPage.."]"}
    for i = startIndex, endIndex do
      table.insert(menuOptions, weapons[i].name)
    end

    -- Add "Next Page" option if there are more pages
    if currentPage < math.ceil(#weapons / menuItemsPerPage) then
      table.insert(menuOptions,"Next Page")
    end

    -- Display the menu
    menu(id, table.concat(menuOptions,","))
  end
end

addhook("menu","_menu")
function _menu(id,title,button)
  if string.match(title,"Weapons Menu") then
    local startIndex = (currentPage - 1) * menuItemsPerPage + 1
    local endIndex = math.min(currentPage * menuItemsPerPage, #weapons)

    if button > 0 and button <= endIndex - startIndex + 1 then
      -- Calculate the selected weapon index
      local weaponIndex = startIndex + button - 1

      -- Give and set the selected weapon
      parse("equip "..id.." "..weapons[weaponIndex].id)
      parse("setweapon "..id.." "..weapons[weaponIndex].id)
    elseif button == endIndex - startIndex + 2 and currentPage < math.ceil(#weapons / menuItemsPerPage) then
      currentPage = currentPage + 1  -- Move to the next page

      -- Calculate start and end index for the next page
      local startIndex = (currentPage - 1) * menuItemsPerPage + 1
      local endIndex = math.min(currentPage * menuItemsPerPage, #weapons)

      -- Create menu options for the next page
      local menuOptions = {"Weapons Menu [Page "..currentPage.."]"}
      for i = startIndex, endIndex do
        table.insert(menuOptions, weapons[i].name)
      end

      -- Add "Next Page" option if there are more pages
      if currentPage < math.ceil(#weapons / menuItemsPerPage) then
        table.insert(menuOptions,"Next Page")
      end

      -- Display the menu for the next page
      menu(id, table.concat(menuOptions,","))
    end
  end
end

old Re: table and menu detect system

Bowlinghead
User Off Offline

Quote
Thanks for sharing your progress so others can learn.

Your original question was how to detect that there are over 8 items in the classes array. You use the # for that. It will count the amount of items in the array.
1
2
3
if (#zombieClassList > 8) then
	-- need a second menu atleast
end

Just 1 offtopic thing in your 2nd post´s weapons array:
You can just store the ids, and then use the cs2d lua cmd itemtype command to get the name
1
2
3
for i,v in pairs(weapons) do
	weapon[i].name = itemtype(v, "name")
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview