Scripts / How to work with variable values?

Author
Message
Teabone
Forum Support
17
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 7th Oct 2014 10:30
Hi all I'm quite new to LUA. I'm just trying to get through the basics of playing around with global variables and local ones.

I'm assuming its as easy as:

globalscore = 100

local localscore = 100


My next question is how do I add to a global variable? I get several errors when I attempt something like

globalscore = globalscore + 1

or

globalscore + 1

or

globalscore = + 1


What's the best practice to simply add a value to a global variable or a local one?

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce 420 GT
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 7th Oct 2014 18:39 Edited at: 7th Oct 2014 18:40
Quote: "globalscore = globalscore + 1"




you just need to make sure you have already defined globalscore in a running script (either this 1 or any called in the map) and that it is a number value



i.e.







in this case globalscore will continuously be adding up while the script is running in the map and can be accessed from any script.

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
Teabone
Forum Support
17
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 7th Oct 2014 21:27
Thanks very much! I see the problem. I didn't define it yet and make a zero value to it as a base to begin with. I wonder if Reloaded will have a global script editor built in so that we can easily add variables to our levels or project of levels.

I'm very curious how the future workflow will be when we get more then one level in a project.

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce 420 GT
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 8th Oct 2014 18:46
yh because they (variables) dont technically need to be numbers it is nil (rather than 0) by default... and
Quote: "nil + 1"
makes no sense - hence the error

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
Teabone
Forum Support
17
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 20th Nov 2014 07:55 Edited at: 20th Nov 2014 08:22
Not sure where I went wrong. Just trying to create a simple coin collecting script.









EDIT:

Oh wow I figured out the issue... and its a weird one... scripts only work when they are in the root folder of scriptbank. Rather than say scriptbank\examples\



I've successfully been able to create a cash system where you have to collect a certain amount of coins in order to open a door. I guess from here its very easy to make weapons and ammo purchasable too now from a vendor. Be nice to try all this out with some HUDs for an inventory type system with Buy and Sell options.

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce 420 GT
Gtox
3D Media Maker
9
Years of Service
User Offline
Joined: 5th Jun 2014
Location: South Africa
Posted: 20th Nov 2014 09:26
Teabone - AuShadow & DVader made some scripts similar to what you are trying to do. They were on the FPSC reloaded Wiki page, but I'm not sure where they are now. There were cash, farming, levelling up and vending scripts, if I remember correctly. All very simple and easy to use as well.

Login to post a reply

Server time is: 2024-05-02 07:02:16
Your offset time is: 2024-05-02 07:02:16