Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: LUA Scripting help!  (Read 1663 times)

0 Members and 1 Guest are viewing this topic.

Offline 009

  • *
  • +1% chance to make useful post
  • 009 has no influence.
LUA Scripting help!
« on: April 02, 2011, 04:55:57 AM »
After a long and painful procrastination period, I have decided to get back working with Smod Revolution. My first priority is to fully implement the body armor system. However, before I could get anywhere, I stumble upon some problems. First of, I can't seems to properly sets up a loop, it always ends up crashing no mater what I tried. Secondly, does anybody have any idea on how to store and use data based on IDs? Like for example if I have 2 metrocops with an ID of 1 and 2 respectively, how can I store their health in a way that when I run a part of the LUA code, it would use their respected stored data (ID1 use dataID1 and ID2 use dataID2) and execute the script to both the metrocops.

Also here's the code I'm working on

Quote
local   HEALTH = 1
local   NEWHEALTH = 1
local   FINALHEALTH = 1
local   CITY = 0




function Init()
   print ("   II  Intializing LUA code  II")
   HL2.HookKilledEvent ("Start")
--   local SUPPLY_COUNT = HL2.RandomInt(2,3)

--   while SUPPLY_COUNT ~= 0 do

   local CITY = CITY + 1
--   local SUPPLY_COUNT = SUPPLY_COUNT - 1

   local a_city = HL2.FindEntityByName(nil, CITY)
   if a_city ~= nil then
   
   local Entinfo = HL2.GetEntInfo(a_city)
   local starthealth = Entinfo.Health

   HEALTH = starthealth


   HL2.SetMoney(HEALTH)

--      end

   end

end

Offline IneptGuard

  • *
  • *
  • +5% chance to make useful post
  • IneptGuard barely matters.IneptGuard barely matters.
Re: LUA Scripting help!
« Reply #1 on: April 04, 2011, 09:56:41 PM »
Alright, before I go off on a rant let it be known that I am not familiar with the LUA language.

first things first, your loop. I take it you mean this:

Code: [Select]
--   local SUPPLY_COUNT = HL2.RandomInt(2,3)

--   while SUPPLY_COUNT ~= 0 do

   local CITY = CITY + 1
--   local SUPPLY_COUNT = SUPPLY_COUNT - 1

OK, here's what I see:

First off, it looks like you initialized the "CITY" var twice:
Also, it looks like you did the same for "SUPPLY_COUNT":
------------------------------------------------------------------------------|
local   HEALTH = 1
local   NEWHEALTH = 1
local   FINALHEALTH = 1
local   CITY = 0




function Init()
   print ("   II  Intializing LUA code  II")
   HL2.HookKilledEvent ("Start")
--   local SUPPLY_COUNT = HL2.RandomInt(2,3)

--   while SUPPLY_COUNT ~= 0 do

   local CITY = CITY + 1
--   local SUPPLY_COUNT = SUPPLY_COUNT - 1

   local a_city = HL2.FindEntityByName(nil, CITY)
   if a_city ~= nil then
   
   local Entinfo = HL2.GetEntInfo(a_city)
   local starthealth = Entinfo.Health

   HEALTH = starthealth


   HL2.SetMoney(HEALTH)

--      end

   end

end
------------------------------------------------------------------------------|
Got a question:

It looks like your using a random number method to give "SUPPLY_COUNT" a random integer between 2 & 3 or 1 & 2 (I don't know if Lua counts 0 as the first number, most languages do), I can't seem to figure out why you are making a loop just to increment "CITY", why not just set "CITY" too a random int between 2 or 3 or whatever? Wouldn't that have the same effect?
« Last Edit: April 04, 2011, 09:58:47 PM by IneptGuard »

Offline 009

  • *
  • +1% chance to make useful post
  • 009 has no influence.
Re: LUA Scripting help!
« Reply #2 on: April 08, 2011, 01:43:25 PM »
Sorry for being a little late.

Here's a little explanation on how the code works:

Quote
--   while SUPPLY_COUNT ~= 0 do

   local CITY = CITY + 1
--   local SUPPLY_COUNT = SUPPLY_COUNT - 1

   local a_city = HL2.FindEntityByName(nil, CITY)
   if a_city ~= nil then


+The Supply_count is first used to intiate the loop. I used a random int command simply because I copy and pasted the loop from the smod wiki to see if it works (which it didn't)

+The "City" var is used to find the metrocop with the similar target name to the values in the "City"
Example: If "City" have the value of "1", it'll find the metrocop with the targetname of "1". If the "City" have the value of "2", it'll find the metrocop with the targetname of "2", etc.

+The other parts of the script just simply get the health value of the metrocop and put it in HEALTH, the HL2.Setmoney part is only for debugging.

Offline IneptGuard

  • *
  • *
  • +5% chance to make useful post
  • IneptGuard barely matters.IneptGuard barely matters.
Re: LUA Scripting help!
« Reply #3 on: April 08, 2011, 10:03:50 PM »
I don't think a "While" loop statement is the best way to go in this situation. I recommend a "For" loop.

Code: [Select]
CITY = 0
-- Say we have 22 metro cops in our map

for i = 0, 22, 1 do
print("Executing code")
--Put your health code here
CITY = City + 1

end

Here's a Break down. Basically what this is saying:

For (As long as my control "0") --> (does not equal "22") --> (Add "1" to my control "i") --> (then do the shit I say below and repeat this loop)

This loop will repeat until i = 22 then it will stop. also unless you have a metro cop named "0" it won't work on the first pass. you can always set your control too "1" instead.

Also, every thing I just learned about lua I learned in the past half hour reading through this website, this link will bring you to the page that explains loops in more detail: http://lua.gts-stolberg.de/en/schleifen.php

Recommend you book mark this site, it could prove very useful in your future endeavors.