Author Topic: [Scripting] Suggestion & Question's  (Read 4645 times)

Tanksy

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    10
    • View Profile
on: January 26, 2017, 11:41:51 PM
I know there's a scripts folder inside the main directory, but I'd like to know where to make things like materials, and modify weapons.

I'd also like to know how to run mods that I make. Where do I put them and how do I make them work within the game?



ThunderDodge

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    46
    • View Profile
    • Steam
on: January 27, 2017, 03:39:29 AM
Yeah, i'd like to mod too. But i cant get my files getting loaded, or i dont know how to call my function in the file.
its something with "invokeClientFunction( Player( index ), "myFunction, myargs )".

Let's assume i have my file in: data/scripts/player

Script:
Code: [Select]
if onServer() then return end

package.path = package.path .. ";data/scripts/player/?.lua"

--require ("randomext")
--require ("utility")
--require ("stringutility")

function writeText()
displayChatMessage("You just ran ur script! YAY", "Script", 0)
end

Now i tried running my script with following entry in the chatbox:
/run invokeClientFunction( Player( 1 ), "writeText" )

returns = nothing


Final questions ( i want to start scripting... ):
How to we run Files -/ Function's
Can one file be shared. So it be Client & Server at same?
When we want to use other files function, do we need to "require" it?



- My suggestions ( if possible ) -

We have Server, Client & a Shared folder.

Each file in this get auto-detected and imported to the game to his group ( Server, Client or Shared ).

Lua scripts should get Autorefreshed. ( much easier to develop then )

We can create global function just by defining them with "local" or nothing for global ( local function blah() ), so we can call it from each file we want without 'package.path = package.path .. ";data/scripts/player/?.lua" '. Or is it already possible???


I really dont get into this scripting logic for the game.
I've already scripted for some games, but this is something new to me.



#### Another guy who asked almost the same ####
http://www.avorion.net/forum/index.php/topic,777.0.html
« Last Edit: January 27, 2017, 03:48:56 AM by ThunderDodge »



ThunderDodge

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    46
    • View Profile
    • Steam
on: January 27, 2017, 03:41:13 AM
« Last Edit: January 27, 2017, 03:50:10 AM by ThunderDodge »



koonschi

  • Developer
  • *
  • Status:
    Offline
    Posts:
    1644
    • View Profile
    • Avorion
on: January 27, 2017, 04:17:15 AM
Basically what you've got with Avorion is a basic sandbox engine, with basic gameplay elements that are predefined, and scripts to program that engine. Predefined things are, for example, the sector grid structure, basic shot properties (beams vs. projectiles), the components that make up entities, such as rotational speeds, position, hangars, physics components, velocity components, etc.

Avorion uses an Entity-Component-model for game logic, you can read more about that here: https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system

Scripts are attached to different objects in the game. For example, you have scripts attached to the server (server.lua), the sectors, players and entities. Upgrades are wrappers for scripts that are attached to entities. There are other scripts, too, such as scripted commands, sector generation scripts and utility scripts like the factions.lua (which is used by the faction database to determine the inventory of players and factions when they're created).

Lots of these scripts use common components, which is why you will see a lot of calls like require("utility").

In order to "run" a script (you don't really run it), you have to attach it to something. Mind you, when attaching to a player, sector or entity, there has to be a client version of the script, too. Right now Avorion doesn't yet support script synchronization, but we'll add that of course.
The script then basically becomes a component that belongs to that scriptable object (I'll call that object just a "Scriptable" from now on.)

Scriptables will call different functions of the scripts that are attached to them during the game loop, depending on what type of object it is (sector, entity, etc) and whether it's on the client or not. For example, client scriptables will call UI functions, while server scriptables will call functions for securing and restoring values that may be saved into the database.
Those calls are, for example, a function initialize() which is the very first function that is ever called in every entity, sector or player script. This function is called when the script is created. Other functions, like function update(timeStep) are called periodically during the gameloop. Documentation and signature to these functions is not yet in the script documentation.

Scriptables have to be synchronized between client and server. They will exist on both, if the scriptable exists on both. That means, they have the ability to invoke a function on their client or server version. They will do a remote procedure call (RPC). This RCP is asynchronous and the function will return immediately, and will not return a value. The server version (that's the version you've already found) is the invokeClientFunction(...) function. You give it the function that should be called on the client as a string, and it will do the remote procedure call. If you need a result, the client will be able to do a invokeServerFunction(...) call to send values to the server.

You can add scripts to scriptables at nearly all stages of the game. Sector scripts are usually added when the sector is being generated. Player scripts are usually added when the player logs into the server. Entity scripts are added on demand, but mostly when the entity is created.

Depending on what you want to do, you have to add the script to the correct object. Missions, for example, are attached to players. A hyperspace blocker script is attached to an entity, events are attached to sectors. For example, if you want a script or its logic to follow the player, you'll have to attach it to the player. If it belongs to an entity, add it to an entity.

Keep in mind that some actions only make sense on the server, and others only make sense on the client. There's also different classes available on the server and the client. Effects or UI is only available on the client, database and management stuff is only available on the server.

You should check out the sector generation scripts for more info on sector scripts. There's also the SectorGenerator class, that has a lot of calls that create entities and add various scripts to entities. If you want a god-mode script, attach lib/enttiydbg.lua to an entity. If you interact with it, there's going to be a ~dev option. Have fun with it ;) Check out this script if you need to know different stuff, there's really plenty of it. To attach it, run the following in the chat window:
Code: [Select]
/run Entity():addScript("lib/entitydbg.lua")
This will add the entitydbg.lua script to your currently flown ship. Check out the jigsaw puzzle piece at the top right screen corner for the UI of the script, or interact with your own ship. A little explanation on the command: All scripts run in some kind of context, meaning they have access to different objects of that context. Entity scripts, for example, run in a context where they usually have access to the Entity they're attached to, and the sector they're in. Player scripts have access to their respective player and the sector the player is in. The /run command creates a new script, fills it with your command, and runs it. The /run command's context has access to the current entity, player that runs the script and sector the player is in. Now what does the command actually do?
Entity() creates an interface to the immediate entity in the current context (It does not create the entity, use Sector() for that). In the case of an entity script, it's the entity the script is attached to. In the case of the command, it's the entity of the player who runs the command. addScript("lib/entitydbg.lua") simply attaches a new script to the entity.
#define TRUE FALSE // happy debugging suckers

If you need help, please post in the forum, don't PM me. If there's something that only I can help you with, contact me via mail.



ThunderDodge

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    46
    • View Profile
    • Steam
on: January 27, 2017, 04:35:11 AM
Thank you for this helpful post!
Thats all i need to know.



thedamngod

  • Moderator
  • *****
  • Status:
    Offline
    Posts:
    392
  • Who is this guy anyway?
    • View Profile
on: January 27, 2017, 09:25:25 AM
I have merged the threads now. If you want your redirect post to be deleted, just hit the report button :)
FAQ    Infos for bug reports

Want a post moved, locked, deleted? Think something is wrong or not appropriate? Then use the "Report to moderator" button



Tanksy

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    10
    • View Profile
on: January 27, 2017, 01:38:27 PM
Thanks for the great reply, koonschi!

I'm going to save this post for future reference.

Are the default materials (Iron, Titanium, etc) built into the game, or are they defined through an editable lua file?



Tanksy

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    10
    • View Profile
on: January 30, 2017, 12:15:40 AM
Why did someone elses thread get merged onto mine?



HumanFate

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    1
    • View Profile
on: May 25, 2019, 01:20:30 PM
Hey im  new to scripting at all and want to make a mod which generates some kind of passive income without breaking the game:

 for example only generate more minerals in a low amount  after u had discovered a material (had more than 0 in the player stash)

Low amount like 0.25 every second or so

i want it to be applied to the player and to be deleted when he leaves the ships

How do i add ressouces to a player?
 i looked in the new documentation but it was quite confusing and to much information .